111

I just started to learn React today. How do I get rid of that error message on my Console in the Terminal in Visual Studio.

(node: 9374)Warning: To load an ES module,
 set "type": "module" in the package.json or use the .mjs extension. 
/Users/nishihaider/workspace-ui/react-todo-app/src/App.js:1
import React from "react";
import "./App.css";

function App() {
  <>
  return (
  <h1>ToDo</h1>
  );
  </>
}

export default App;
Liam
  • 27,717
  • 28
  • 128
  • 190
Nisha_UX
  • 1,213
  • 2
  • 5
  • 5
  • Does this answer your question? [Node.js - SyntaxError: Unexpected token import](https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import) – Liam Mar 02 '21 at 09:03
  • Had the same problem when using ts-node in a monorepo (Angular + Express + single package.json). Explicitly specifying the path to the tsconfig.json fixed the problem: `--project express-server/tsconfig.json`. https://github.com/TypeStrong/ts-node#loading-tsconfigjson – nunoarruda Apr 21 '21 at 13:53

10 Answers10

123

First, install the latest version of Node.js. It has the latest and greatest features.

Second, add the "type": "module" line in your package.json file.

{

  "type": "module"

}

Third, use the --experimental-modules flag when invoking nodejs:

node --experimental-modules app.js

You should be good to go!

An alternative is to avoid adding the "type": "module" line in your package.json file and instead rename your app.js file to app.mjs.

Note that now the require() syntax will stop working.

Bijin Abraham
  • 1,709
  • 2
  • 11
  • 25
  • 11
    you don't need to add the flag `--experimental-modules` anymore, you can simply run `node app.js` – Antoine Weber Jul 03 '21 at 16:26
  • This may end up with **"ReferenceError: require is not defined"** error in cases you have declaration like: `require('dotenv').config();` – hoomi Aug 07 '21 at 08:34
  • 4
    Have a few packages needs `require`, how do I resolve it? Thanks – Jeb50 Aug 24 '21 at 21:53
  • @Jeb50 did you solve that? I'm looking for some answer – Jack Sep 16 '21 at 21:19
  • @Jack Don't remember what I did, as I check now still using `require`. Definitely not adding `"type": "module"`. – Jeb50 Sep 16 '21 at 21:48
  • @Jeb50 `"type":"module"` definitely isn't going to work for me either. Do you remember if you was having issues to install webpack before getting into this error? – Jack Sep 16 '21 at 22:06
  • @Jack I might have rolled back as TOO MUCH work to convert. Will let you know if it rings. :) – Jeb50 Sep 16 '21 at 23:32
  • 1
    @Jack See if helps. https://stackoverflow.com/a/68914705/5063031 – Jeb50 Sep 17 '21 at 16:07
  • @Jeb50 the issue is, a thrid-party library uses alot of `require`s and I cannot change that. I fixed the require in my own code then I got erros about the thrid-party ones. – Jack Sep 17 '21 at 16:10
  • Everyone recommends this but no one warns that since added `"type": "module"` each require() statement must be removed even in dependencies so this solution is not acceptable. Now some packages use require and another import, nightmare, no compatibility between it. – mikep Aug 26 '22 at 07:49
  • Is this still current? Does javascript still have 5 ways to use modules? – Alper Sep 12 '22 at 17:39
42

Here is my approach:

1 - Update package.json like:

  "main": "index.js",
  "type":"module",

2 - use.js when importing, like:

import {isPrime} from './isPrime.js';

3 - here is isPrime.js

export const isPrime ....
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Arin Yazilim
  • 939
  • 8
  • 6
  • 2
    I tried this, and it seems to work. Why do I have to add ".js" though? That doesn't seem right - I would expect it to automatically figure out that it needs the ".js" file without having to specify the extension. Edit: It's apparently an ongoing discussion: https://github.com/microsoft/TypeScript/issues/16577 – Lebbers Mar 18 '21 at 01:02
  • I was removing Babel from my project and this works for me – Álvaro Agüero Apr 26 '21 at 22:58
27

You just need to update package.json like this,

{"type": "module"}

It's worked for me, Thanks!

15

to add Type: module in package.json - helps :)

package.json contain:

{
  "name": "react_template",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^5.30.0",
    "webpack-cli": "^4.6.0"
  }
}
Lotpite
  • 336
  • 2
  • 4
6

For those using TypeScript with node.js, and is trying to use the import statement. I have found that setting the module to value es2015 in the tsconfig.json gives this error, while setting it to commonjs works.

tsconfig.json

    {
     "module": "commonjs"
    }
Jarrett
  • 486
  • 7
  • 13
3

Just Change this:

export default App;

To this:

module.exports = App;
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Lalit Kumar
  • 107
  • 1
  • 4
2

I have a parent file importing child components, so I get same problem about ES module. in my case, I must define more details when i import the component. like this:

import db from "../config/Database"; false
import db from "../config/Database.js"; true

Another example :

import Users from "../models/UserModel"; incorrect
import Users from "../models/UserModel.js"; correct

I don't know why it has to be like this, but when I try the problem is resolved. I hope this helps

1

you are also sorrounding the return statement with React Fragments which is not correct. Your return statement should look like the following:

 import React from "react";
 import "./App.css";

 function App() {
   return (
    <>
      <h1>ToDo</h1>
    </>
   );
 }

export default App;

I'm quite sure this was the source of your issues all along and not the module import/export issue.

dieguiviti
  • 19
  • 3
1

adding the "type": "module" line in your package.json file and instead rename your app.js file (or whatever) to app.mjs.

0

SIMPLE AND FAST SOLUTION FOR BEGINNERS

1 RENAME YOUR JS FILE MJS LIKE (hello.js > hello.mjs)

2 GIVE MJS FILE NAME IN YOUR EXPORT LIKE ( import { first } from './pg.mjs')

sainupangad
  • 115
  • 9