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