Node version:
node -v
v12.13.0
I read this documentation about using ES modules in Node.js:
Node.js will treat as
CommonJS
all other forms of input, such as.js
files where the nearest parentpackage.json
file contains no top-level"type"
field
This is my package.json
file:
{
"type": "module",
"name": "sandbox",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ./src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ramda": "^0.26.1"
}
}
this is my ./src/index.js
:
import R from "ramda";
const src = {name:"Bob",age:7};
const _src = R.clone(src);
console.log(_src);
I try to start my code via npm start
or node ./src/index.js
but I get the error:
import R from "ramda";
^^^^^^SyntaxError: Cannot use import statement outside a module
Why does it happen? I.e. why node ignores my "type": "module"
setting?
At the same time it works fine:
node --experimental-modules .\src\index.js