score:-1

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

score:0

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"
    }

score: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.

score:8

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"
  }
}

score:17

You just need to update package.json like this,

{"type": "module"}

It's worked for me, Thanks!

score:31

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 ....

score:76

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.


Related Query

More Query from same tag