132

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?

Rebbeca
  • 1,587
  • 2
  • 10
  • 12

4 Answers4

171

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.

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126
  • 8
    @Alex "type": "module" is for ES modules. I believe you should remove your comment. Check also https://developer.okta.com/blog/2019/12/04/whats-new-nodejs-2020 – Gad D Lord Dec 07 '20 at 22:46
  • 6
    I think what Alex was trying to say is that omitting `type` from `package.json` has the same effect as setting it to CJS. (This is what the spec says: "If the nearest parent package.json lacks a "type" field, or contains "type": "commonjs", .js files are treated as CommonJS. If the volume root is reached and no package.json is found, Node.js defers to the default, a package.json with no "type" field.") – jacobq May 20 '21 at 01:04
  • 1
    Ah what a difference a comma makes! No, "type": module means.... is incorrect, but No type:module means.... is correct haha. Please clarify @alex – Collin White Jul 27 '21 at 18:10
  • Somthing to be noted: it will apply to all exports. So I don't know how to mix commonjs and ESM yet. – Eric Burel Jan 14 '22 at 14:40
  • 1
    can i still use require syntax after add type module in package.json file? – alfianrehanusa Jun 28 '22 at 06:16
29

Update as of mid-2022

If you're using Node.js v16, 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:

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

Or you can do one of those instead if on a modern version of Node already:

  • add "type": "module" in your package.json
  • rename your file with an .mjs extension, end result will look like this node app.mjs
kissu
  • 40,416
  • 14
  • 65
  • 133
  • 5
    Unless I'm missing something, you still need to either (a) declare `"type": "module"` in package.json or (b) use .mjs file extensions when using Node 14 (tested with version 14.17.3). Otherwise you get an error from Node like `Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.` and `SyntaxError: Cannot use import statement outside a module`. This also applies to Node 16.5.0. – superEb Jul 29 '21 at 20:33
  • @superEb Nvm, you're right, `type: module` is still required. My bad: https://blog.logrocket.com/es-modules-in-node-today/ – kissu Jul 30 '21 at 13:59
3

@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!

Jason R Stevens CFA
  • 2,232
  • 1
  • 22
  • 28
0

When you switch from const express = require("express") to import { Express } from "express"; you will have to insert "type": "module" in your package.json. It is basically used to enable the ES6 modules import/export syntax.