RAMMURTY
RAMMURTY

Reputation: 61

Export-import in JavaScript --- SyntaxError: Cannot use import statement outside a module

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

Upvotes: 2

Views: 2586

Answers (3)

Michele Delle Donne
Michele Delle Donne

Reputation: 211

NodeJS is not ES6. Set "type": "module" in the package.json as shown in the following image enter image description here

Upvotes: 0

VPaul
VPaul

Reputation: 1013

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

Upvotes: 1

RAMMURTY
RAMMURTY

Reputation: 61

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

Upvotes: 2

Related Questions