简体   繁体   English

Node.js:语法错误:无法在模块外使用导入语句

[英]Node.js: SyntaxError: Cannot use import statement outside a module

I am getting this error SyntaxError: Cannot use import statement outside a module when trying to import from another javascript file.我收到此错误语法错误SyntaxError: Cannot use import statement outside a module 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 ,模块文件是mod.js

main.js:主.js:

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

mod.js: mod.js:

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

How can I fix this?我怎样才能解决这个问题? Thanks谢谢

In order to use the import syntax (ESModules), you need to set the following to your package.json at the top level:为了使用导入语法(ESModules),您需要在顶层为您的 package.json 设置以下内容:

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

If you are using a version of Node earlier than 13, you need to use the --experimental-modules flag when you run the program如果您使用的 Node 版本早于 13,则需要在运行程序时使用 --experimental-modules 标志

node --experimental-modules program.js

and add {"type": "module" } in package.json并在 package.json 中添加 {"type": "module" }

Hope it helps !希望能帮助到你 !

Use commonjs syntax instead of es module syntax:使用 commonjs 语法而不是 es 模块语法:

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否则,如果你想使用 es 模块,你必须按照 Achraf Ghellach 的回答建议

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" .在与您的模块相同的目录中创建一个 package.json 文件并添加"type":"module" Then use import {func} from "./myscript.js";然后使用 import {func} from "./myscript.js"; . . The import style works when run using node.使用节点运行时,导入样式有效。

In addition to the answers above, note by default(if the "type" is omitted) the "type" is "commonjs".除了上面的答案,请注意默认情况下(如果“type”被省略)“type”是“commonjs”。 So, you have explicitly specify the type when it's "module".因此,当它是“模块”时,您已经明确指定了类型。 You cannot use an import statement outside a module.您不能在模块外使用 import 语句。

I had this issue trying to run mocha tests with typescript.我在尝试使用 typescript 运行 mocha 测试时遇到了这个问题。 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.他使用了一个涉及cross-env的技巧,允许他以 commonjs 模块类型运行测试。 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"
  }
}

If you are in the browser (instead of a Node environment), make sure you specify the type="module" attribute in your script tag.如果您在浏览器中(而不是 Node 环境),请确保在script标记中指定type="module"属性。 If you want to use Babel, then it must be type="text/babel" data-plugins="transform-es2015-modules-umd" data-type="module" .如果你想使用 Babel,那么它必须是type="text/babel" data-plugins="transform-es2015-modules-umd" data-type="module"

For browser(front end) : add type = "module" inside your script tag ie对于浏览器(前端) :在脚本标签中添加 type = "module" 即

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

For nodejs : add "type": "module" , in your package.json file对于nodejs :在package.json文件中添加"type": "module"

SyntaxError: Cannot use import statement outside a module SyntaxError:不能在模块外使用 import 语句

I got the same issue but in another module (python-shell).我遇到了同样的问题,但在另一个模块(python-shell)中。 I replaced the code as follows:我将代码替换如下:

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

That solved the issue.这解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError:无法在 node.js 上的模块外部使用 import 语句 - SyntaxError: Cannot use import statement outside a module on node.js 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js 获取不能在 node.js 中的模块外使用导入语句 - Get Cannot use import statement outside a module in node.js JS 导入导出语法错误:不能在模块外使用导入语句 - JS import export SyntaxError: Cannot use import statement outside a module 语法错误:不能在模块 discord.js 之外使用导入语句 - SyntaxError: Cannot use import statement outside a module discord.js 纯 JS - SyntaxError:不能在模块外使用 import 语句 - Pure JS - SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module 不能使用 jest 在模块 node.js 之外使用 import 语句 - Cannot use import statement outside a module node.js using jest Javascript 模块错误语法错误:不能在模块外使用导入语句 - Javascript Module error SyntaxError: Cannot use import statement outside a module 尝试使用 three.js 时出现“未捕获的语法错误:无法在模块外使用 import 语句” - “Uncaught SyntaxError: Cannot use import statement outside a module” when trying to use three.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM