137

I'm trying to import myArr from hello.js into index.js. However I get an error of

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

File hello.js

export let myArr = ['hello', 'hi', 'hey'];

File index.js

import { myArr } from './hello.js';
console.log(myArr);

Where am I going wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elitezen
  • 6,551
  • 6
  • 15
  • 29
  • 2
    What version of node.js? If your file does not have a file extension `.mjs`, there is a [whole set of rules](https://nodejs.org/api/esm.html) for what you have to do to get `import` and `export` to work in node.js. When I experimented with it, I found it quite a pain. I'm sure there will come a day when it's just built-in and easy to use, but node.js isn't there yet. – jfriend00 May 08 '20 at 01:32

7 Answers7

85

Use version 2:

npm install node-fetch@2

node-fetch from v3 is an ESM-only module - you are not able to import it with require().

If you cannot switch to ESM, please use v2 which remains compatible with CommonJS. Critical bug fixes will continue to be published for v2.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mal
  • 1,755
  • 13
  • 12
  • 65
    I'm confused, what is the relation between the question and `node-fetch`? – Maxime Chéramy Jan 04 '22 at 18:01
  • 7
    This... doesn't answer the question. – Clonkex Feb 22 '22 at 03:59
  • 6
    @MaximeChéramy for me the error in the question comes up when trying to use `node-fetch@3` in typescript while debugging with `ts-node`. A web search brought me here, and I am guessing many others are seeing this – Felipe Feb 22 '22 at 15:41
  • 3
    Not the answer to this question, but i'm guessing most people who ended up here, got to this problem because of that package. This actually solved the problem for now. Not sure how to deal with the import even though I'm using node 14.9 – Bruno Feb 22 '22 at 20:38
  • 2
    This seems unrelated to the question but it did fix the error for me! – lizziepika Mar 16 '22 at 21:11
  • this is so broken... – masterxilo Apr 08 '23 at 12:38
63

I ran your code without any problems. Check for two things:

  1. Node.js version >= 14. It only works with the latest version of Node.js.
  2. Make sure your package.json includes a line for "type": "module". Without this line, Node.js assumes you want to use CommonJS modules rather than ESM.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
G Clark
  • 849
  • 7
  • 6
  • 1
    My file is called .mjs then why should it demand the "type": "module" in packages.json, this change breaks other scripts :< I will have to do some hack where I add it just before my script runs and then removes it again ^^ not pretty.. – OZZIE Dec 04 '20 at 07:20
  • I dunno what happened but I went to node 14 had to change to "type": "module" ran successfully, removed "type": "module" and now I can run it without it :D – OZZIE Dec 04 '20 at 12:55
  • 8
    Also remember to remove `node_modules` and re-do a `npm install` – Diego Betto Sep 14 '21 at 08:12
  • 1
    I have both. Node is 14.17 and package.json has `"type": "module"`. I do not know where I am going wrong. – Pramod Dec 30 '21 at 05:51
  • 1
    Leads to [this error](https://stackoverflow.com/questions/62096269/cant-run-my-node-js-typescript-project-typeerror-err-unknown-file-extension) – Stoyan Georgiev Jan 05 '22 at 20:02
  • Also remember to remove the `--require|-r` flag, e.g. `node -r ./server` – Ricky Boyce Sep 10 '22 at 03:05
25

I ran into a similar issue while building my React project.

Here's the error:

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/pradeep/Work/my_project/node_modules/@babel/runtime/helpers/interopRequireWildcard/_index.mjs

Then I realized that I am on a different Node.js version than the one used to install packages in this project.

I had two options:

  1. Change Node.js version to the one required by this project and build again.
  2. Stay on the Node.js version you have and remove the node_modules directory and package-lock.json file and do npm install again.

I chose the first approach and it worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pradeep Vig
  • 485
  • 6
  • 7
6

If you have Node.js version lower than 14, e.g., v12 - you must specify this flag:

node --experimental-modules your.mjs
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Garmoshka
  • 5,849
  • 39
  • 40
5

I use nvm to install the latest stable version of Node.js.

To delete the package-lock.json file and the node_modules folder, run npm. I then npm start to solve my problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

Rename hello.js to hello.mjs.

You are using CommonJS right now with the .js extension. To import ES6 modules, you will have to change the extension of the file that you want to import to .mjs so it knows that it is an ES6 module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Algo7
  • 2,122
  • 1
  • 8
  • 19
2

The problem is that Node.js does not currently support import and export natively yet. It is still experimental according to the documentation. I recommend you use Babel to compile your code and allow you to use import and export.

For example, you can install the @babel/node package and run your project using:

npx babel-node index.js

Here are the documentation for @babel/node. Like the documentation state, this command is only meant for local development. In production, they recommend a configuration like this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jjgg
  • 630
  • 5
  • 15
  • 2
    The flag isn't required since node 14 – Aluan Haddad May 08 '20 at 01:59
  • 2
    @AluanHaddad - Just for the sake of clarity, Node 14 is pretty new. It just went "current" last month and won't be "active" until something like this October. So, v14 is in the process of arriving. People can go grab it if they want to, but the point of the period between current and active is to make sure it's stable and to give library authors time to adapt to it (if necessary). And, the nodejs v14 doc says "Experimental support for ECMAScript modules is enabled by default. ". So, you don't need the flag any more in V14, but it is still in the "experimental" stage. – jfriend00 May 08 '20 at 02:54