Why is 'type: module' in package.json file?

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?

0

4 Answers

When you have "type": "module" in the package.json file, your source code should use import syntax. When you do not have, you should use require syntax; that is, adding "type": "module" to the package.json enables ES 6 modules. For more info, see here.

4

Update as of mid-2021

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:


If you're still stuck with a lower version of Node.js for some reasons, you can follow this blog post from Flavio:

And do the following:

  • add "type": "module" in your package.json
  • use --experimental-modules when launching your app, eg: node --experimental-modules app.js

Or you can do that instead:

  • add "type": "module" in your package.json
  • rename your file with an .mjs extension, end result will look like this node app.mjs
2

@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: babel dependencies are included for full ES6 to ES5 transpilation and may/may not be necessary. In addition, you should probably pin the version of the package myPkg you're testing!

theres a lot of issues being brought up recently - I think node rolled out an update and killed the application. Roll back node to a previous verion - that solved my issue

heres where to find older:

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like