typescript 打字稿:不能在模块外使用导入语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58273824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript: Cannot use import statement outside a module
提问by Zerumi
I have a .ts file in node js (latest version of node.js for 07.10.19) app with importing node-module without default export. I use this construction: import { Class } from 'abc';
When i run the code, i have this error: Cannot use import statement outside a module
.
我在 node js(07.10.19 的最新版本 node.js)应用程序中有一个 .ts 文件,它导入了没有默认导出的节点模块。我使用这种结构:import { Class } from 'abc';
当我运行代码时,我有这个错误:Cannot use import statement outside a module
。
In the network i see many solutions for this problem (for .js), but it not helps to me, maybe because i have typescript file. Here's my code:
在网络中,我看到了许多针对此问题的解决方案(针对 .js),但对我没有帮助,可能是因为我有打字稿文件。这是我的代码:
import { Class } from 'abc';
module.exports = { ...
execute(a : Class ,args : Array<string>){ ...
Here's my tsconfig.json:
这是我的 tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true
}
}
回答by Zwiers
Adding “type”: “module”
to package.jsonwill tell Node you are using ES2015 modules, which should get rid of the error, but then you will need to tell Typescript to generate this type of module by setting “module”: “es2015”
instead of “commonjs”
in tsconfig.json.
添加“type”: “module”
到 package.json会告诉 Node 你正在使用 ES2015 模块,这应该会消除错误,但是你需要告诉 Typescript 通过设置“module”: “es2015”
而不是“commonjs”
在 tsconfig.json 中生成这种类型的模块。
This however causes a problem with the current code because although you are using an ES6 import {}
statement you are exporting using the commonJS module.exports = {}
syntax, and Node's ES module loader will have an issue with it. There are two ways to deal with it:
然而,这会导致当前代码出现问题,因为尽管您使用的是 ES6import {}
语句,但您使用的是 commonJSmodule.exports = {}
语法导出,并且 Node 的 ES 模块加载器会遇到问题。有两种处理方法:
- Keep the
module.exports
but tell Node to interpret this file as commonJS by giving it a .cjs extension. - Change the export statement to ES2015 syntax:
export function execute(…)..
- 保留
module.exports
但告诉 Node 通过给它一个 .cjs 扩展名来将这个文件解释为 commonJS 。 - 将导出语句更改为 ES2015 语法:
export function execute(…)..
The first option could get a bit tricky because the compiler will output .js files and you'd have to change it to .cjs all the time (as far as I know). With the second option you should be able to run the file with Node (including the --experimental-modules flag for versions < 13.8).
第一个选项可能会有点棘手,因为编译器将输出 .js 文件,而您必须一直将其更改为 .cjs(据我所知)。使用第二个选项,您应该能够使用 Node 运行文件(包括版本 <13.8 的 --experimental-modules 标志)。
If you absolutely need to use commonJS, perhaps it is better to install the type definitions for Node: @types/nodeand change the import to commonJS format: require('abc')
and keep the rest of the settings as they are (though you can add “type”: “commonjs” to package.json to be explicit).
如果您绝对需要使用 commonJS,也许最好安装 Node 的类型定义:@types/node并将导入更改为 commonJS 格式:require('abc')
并保持其余设置不变(尽管您可以添加“类型” :“commonjs”到 package.json 是明确的)。