Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(node:9374) Warning: To load an ES module, set "type": "module"

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;
like image 224
Nisha_UX Avatar asked Aug 26 '20 00:08

Nisha_UX


People also ask

How do I load ES module set type module in the package json?

// ⛔️ To load an ES module, set "type": "module" in // package. json import moment from 'moment'; moment(). format(); To be able to load ES modules, set the type property to module in your project's package.

How do I enable ES modules in node JS?

To be able to load an ES module, we need to set “type”: “module” in this file or, as an alternative, we can use the . mjs file extension as against the usual . js file extension. Also, from Node version 12.7.


3 Answers

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.

like image 139
Bijin Abraham Avatar answered Oct 19 '22 23:10

Bijin Abraham


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 ....
like image 25
Arin Yazilim Avatar answered Oct 19 '22 23:10

Arin Yazilim


You just need to update package.json like this,

{"type": "module"}

It's worked for me, Thanks!

like image 20
Sudesh Ladusinghe Avatar answered Oct 19 '22 23:10

Sudesh Ladusinghe