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
