【发布时间】:2020-02-05 00:01:50
【问题描述】:
我在 node js(07.10.19 的 node.js 的最新版本)应用程序中有一个 .ts 文件,其中导入 node-module 而没有默认导出。我使用这个结构:import { Class } from 'abc'; 当我运行代码时,我有这个错误:Cannot use import statement outside a module。
在网络中,我看到了许多解决此问题的方法(对于 .js),但这对我没有帮助,可能是因为我有打字稿文件。这是我的代码:
import { Class } from 'abc';
module.exports = { ...
execute(a : Class ,args : Array<string>){ ...
这是我的 tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": true
}
}
【问题讨论】:
-
您是在浏览器中运行它吗?
import语句是文件中的第一行吗? -
您能发布您的 tsconfig.json 文件吗?在 Typescript 中编译时,您可以确定它生成的模块类型,有效类型可能会根据您使用的环境(浏览器/NodeJS)和其他模块(
requirevsimport)而有所不同。只是为了让您了解这有多复杂,Node 有一些 documentation 关于import与require以及如何使它们一起工作。 -
如果你使用
module.exports语法,你可能不在ES6模块中。 -
Evert,这是 node js 应用,tsconfig 将很快添加
-
好的,我不能使用require,因为在使用构造
execute(a : abc.Class...时找不到命名空间'abc'@