I upgraded the node and built the existing file.
But it didn't build, and there was an error.
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: │
│ ~~/nuxt.config.js │
│ require() of ES modules is not supported. │
│ require() of ~~/nuxt.config.js from │
│ ~~/config.js is an ES │
│ module file as it is a .js file whose nearest parent package.json contains "type": │
│ "module" which defines all .js files in that package scope as ES modules. │
│ Instead rename nuxt.config.js to end in .cjs, change the requiring code to use │
│ import(), or remove "type": "module" from │
│ ~~/package.json.
So I removed 'type: module' in package.json file.
Is it okay to remove it?
If you're using Node.js v14, and you should if you can since it's LTS, you only need to use type: module as explained here: https://blog.logrocket.com/es-modules-in-node-today/
If you're still stuck with a lower version of Node.js for some reasons, you can follow this blog post from Flavio: https://flaviocopes.com/how-to-enable-es-modules-nodejs/
And do the following:
"type": "module" in your package.json--experimental-modules when launching your app, eg: node --experimental-modules app.jsOr you can do that instead:
"type": "module" in your package.json.mjs extension, end result will look like this node app.mjs@AfsharMohebi's answer is excellent and covers the most useful points.
This answer is to add some color around CI/CD pipelines, where one may need to utilize adding a dynamic type parameter for executing code with node, written in ES6 JavaScript. Additionally, I am aware this is tangential to the OP's question but Google brought me here and so hopefully this is found useful by someone else.
In particular, we may use --input-type=module according to the node docs if we do not have a package.json including type: module.
For example, I use the command below to test that an npm package was uploaded successfully and is usable:
mkdir test-mypkg && cd test-mypkg
echo "import { myFunc } from '@myname/myPkg';" > test.js
npm i @myname/myPkg @babel/core @babel/node && cat test.js | node --input-type=module
Note:
babeldependencies are included for full ES6 to ES5 transpilation and may/may not be necessary. In addition, you should probably pin the version of the packagemyPkgyou're testing!