123

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

Serket
  • 3,785
  • 3
  • 14
  • 45

8 Answers8

157

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
Achraf Ghellach
  • 1,706
  • 1
  • 8
  • 6
30

Use commonjs syntax instead of es module syntax:

module.exports.func = function (){
    console.log("Hello World");
}

and

const myMod = require("./mod")
myMod.func()

Otherwise, if you want to use es modules you have to do as the answer by Achraf Ghellach suggests

gautam1168
  • 641
  • 7
  • 15
  • Assuming `./mod` is a file named `mod.js` containing part 1 of your answer, I still get `ReferenceError: module is not defined` – lacostenycoder Feb 22 '23 at 18:02
  • Assuming you have nodejs 16 or higher it should work. Are you maybe using the keyword `module` during require as a variable? Check this screenshot: https://pasteboard.co/D41OnBGMT81z.png – gautam1168 Feb 23 '23 at 13:23
  • unfortunately I'm using pretty old version... 12 :( – lacostenycoder Feb 23 '23 at 16:45
  • I don't know if this will help on that version, but you may try after adding the extension: `var mymod = require("./mod.js")`. This require style is pretty old so I would have expected it to work on node 12 as well. – gautam1168 Feb 24 '23 at 03:44
6

For browser(front end): add type = "module" inside your script tag i.e

<script src="main.js" type="module"></script>

For nodejs: add "type": "module", in your package.json file

{
  "name": "",
  "version": "",
  "description": "",
  "main": "",
  "type": "module",
   ....
}
kob003
  • 2,206
  • 2
  • 12
  • 19
4

I recently encountered this problem. This solution is similar to the top rated answer but with some ways I found worked for me.

In the same directory as your modules create a package.json file and add "type":"module". Then use import {func} from "./myscript.js";. The import style works when run using node.

ro_alli
  • 49
  • 2
1

In addition to the answers above, note by default(if the "type" is omitted) the "type" is "commonjs". So, you have explicitly specify the type when it's "module". You cannot use an import statement outside a module.

Lekia
  • 105
  • 1
  • 12
1

If you are in the browser (instead of a Node environment), make sure you specify the type="module" attribute in your script tag. If you want to use Babel, then it must be type="text/babel" data-plugins="transform-es2015-modules-umd" data-type="module".

Simone
  • 1,260
  • 1
  • 16
  • 27
0

I had this issue trying to run mocha tests with typescript. This isn't directly related to the answer but may help some.

This article is quite interesting. He's using a trick involving cross-env, that allows him to run tests as commonjs module type. That worked for me.

// package.json
{
  ...
  "scripts": {
    "test": "cross-env TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\" }' mocha -r ts-node/register -r src/**/*.spec.ts"
  }
}
johnnyBoy
  • 115
  • 2
  • 12
0

I got the same issue but in another module (python-shell). I replaced the code as follows:

import {PythonShell} from 'python-shell'; (original code)
let {PythonShell} = require('python-shell')

That solved the issue.

Snowcat
  • 470
  • 8
  • 16