I am learning JavaScript today. I have created two files 'a.js' 'b.js' in the same directory.
a.js code
export default class User {
constructor(n) {
this._a = n;
}
}
export function f(n) {
console.log("Funct");
}
b.js code:
import User, {f} from './a.js';
var u = new User("hey");
console.log(u, u._a);
f();
when I run node b.js, hits me up with this error like below:
(node:47205) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. /Users/rammurthys/Documents/Angular Tute/JS/b.js:1 import User, {f} from './a.js'; ^^^^^^
SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:1055:16) at Module._compile (internal/modules/cjs/loader.js:1103:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1159:10) at Module.load (internal/modules/cjs/loader.js:988:32) at Function.Module._load (internal/modules/cjs/loader.js:896:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47
If I run node a.js, it throws error like below.
(node:47321) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. /Users/rammurthys/Documents/Angular Tute/JS/a.js:1 export default class User { ^^^^^^
SyntaxError: Unexpected token 'export' at wrapSafe (internal/modules/cjs/loader.js:1055:16) at Module._compile (internal/modules/cjs/loader.js:1103:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1159:10) at Module.load (internal/modules/cjs/loader.js:988:32) at Function.Module._load (internal/modules/cjs/loader.js:896:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47
I am using node v13.5.0. Executing this on VS code.
Please help me understand what am I missing. Thanks
-
The first line of the error message tells you two things you can do to fix this. Have you tried them?Quentin– Quentin2020年04月06日 13:50:55 +00:00Commented Apr 6, 2020 at 13:50
-
Do we need package.json just to simply execute a JS file ?RAMMURTY– RAMMURTY2020年04月06日 13:53:20 +00:00Commented Apr 6, 2020 at 13:53
-
The first line of the error message tells you two things you can do to fix this. If you don't like one, then try the other.Quentin– Quentin2020年04月06日 13:53:54 +00:00Commented Apr 6, 2020 at 13:53
-
oh yes, renaming with .mjs works! thanks . I figure out why is that required.RAMMURTY– RAMMURTY2020年04月06日 13:58:02 +00:00Commented Apr 6, 2020 at 13:58
3 Answers 3
renaming .mjs from .js did the trick. Thanks @Quentin!
.mjs is the one of the two ways to play around export-import javascript apart from require {} stuff.
Detailed explanation: https://medium.com/passpill-project/files-with-mjs-extension-for-javascript-modules-ced195d7c84a
Comments
It's important to understand that NodeJS is not ES6. What you're trying to do is part of the ES6 spec. Not everything is available in NodeJS by default as of now that's why .mjs extension is required. Once ES6 finalizes things in near future, NodeJS will be able to add the support as well.
I wouldn't recommend using .mjs extension. It would be better go with a transpiler like Babel. Babel compiles your ES6 code into NodeJS without having to change the extension of your file.
Similar info can be found here: https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c
1 Comment
NodeJS is not ES6. Set "type": "module" in the package.json as shown in the following image enter image description here