Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  122] [ 5]  / answers: 1 / hits: 189203  / 4 Years ago, sat, june 20, 2020, 12:00:00

I am getting this error SyntaxError: Cannot use import statement outside a module when trying to import from another javascript file. This is the first time I'm trying something like this. The main file is main.js and the module file is mod.js.


main.js:


import * as myModule from "mod";
myModule.func();

mod.js:


export function func(){
console.log("Hello World");
}

How can I fix this? Thanks


More From » node.js

 Answers
17

In order to use the import syntax (ESModules), you need to add the following to your package.json at the top level:


{
// ...
"type": "module",
}

If you are using a version of Node earlier than 13, you additionally need to use the --experimental-modules flag when you run the program:


node --experimental-modules program.js

[#50864] Saturday, June 6, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;