0

I'm learning a new language (Redux) but I have this problem

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

Here I added the contents of package.json

...
import express from 'express';
^^^^^^

SyntaxError: Cannot use import statement outside a module

As he asked me I added "type": "module", and I have this problem:

(node:6392) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 
...\sa\backend\data' imported from 
...\sa\backend\server.js
at finalizeResolution (internal/modules/esm/resolve.js:259:11)
at moduleResolve (internal/modules/esm/resolve.js:636:10)
at Loader.defaultResolve [as _resolve] 
(internal/modules/esm/resolve.js:726:11)
at Loader.resolve (internal/modules/esm/loader.js:97:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:243:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:47:40)
at link (internal/modules/esm/module_job.js:46:36) {
code: 'ERR_MODULE_NOT_FOUND'
}

It will be useful to specify that in the frontend React import works normally.

package.json

{
 "name": "tryredux",
 "version": "1.0.0",
 "type": "module",
 "description": "\"# tryredux\"",
 "main": "index.js",
 "dependencies": {
   "@babel/cli": "^7.10.5",
   "@babel/core": "^7.11.0",
   "@babel/node": "^7.10.5",
   "@babel/preset-env": "^7.10.4",
   "express": "^4.17.1",
   "nodemon": "^2.0.4",
   "react-router-dom": "^5.2.0"
 },
 "devDependencies": {
   "@babel/cli": "^7.8.4",
   "@babel/core": "^7.8.4",
   "@babel/node": "^7.8.4",
   "@babel/preset-env": "^7.8.4",
   "eslint": "^6.8.0",
   "eslint-config-airbnb": "^18.1.0",
   "eslint-plugin-import": "^2.20.1",
   "eslint-plugin-jsx-a11y": "^6.2.3",
   "eslint-plugin-react-hooks": "^2.5.1",
   "nodemon": "^2.0.2"
  },
  "scripts": {
    "start": "nodemon --watch backend --exec babelnode backend/server.js"
  },
  "author": "",
  "license": "ISC"
}
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

The answer here explains it quite concise:

https://stackoverflow.com/a/45854500/2361500

In node the default is the commonjs syntax for module handling. You might consider switching to require()

lotype
  • 593
  • 3
  • 8
-1

Append .js to the end of data - it should be data.js.

halfer
  • 19,824
  • 17
  • 99
  • 186
basir
  • 38
  • 7
  • 1
    sir I remind you that we are in node.js technology so when we import we write ../data no data.js –  Aug 01 '20 at 16:59