【发布时间】:2020-05-09 17:47:36
【问题描述】:
我是一个新的打字稿,我写了一个 SDK,我的 .tsconfig 看起来有点像这样
{
"compilerOptions": {
"moduleResolution": "node",
"experimentalDecorators": true,
"module": "esnext",
"noImplicitReturns": true,
"noUnusedLocals": true,
"sourceMap": true,
"strict": true,
"target": "es2015",
"resolveJsonModule": true,
"esModuleInterop": true,
"noImplicitAny": false,
"outDir": "./lib",
},
"compileOnSave": true,
"include": [
"src"
],
"exclude": ["node_modules"]
}
我使用tsc 命令构建它。现在我创建了 localtest.js 文件,我将在其中导入此文件
import getWorkspace from './lib/index'
const randomFunc = async () => {
// some code
}
randomFunc()
然后在我的终端node localtest.js 中使用以下命令运行它,这会引发以下错误
function (exports, require, module, __filename, __dirname) { import getWorkspace from './lib/index'
^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
关于如何修复它以及为什么会出现上述错误的任何想法
【问题讨论】:
-
这甚至不是合法的 ES 6:导入和导出必须是顶级语句。您不能在函数内部执行此操作。为什么它必须是 ES 6 模块?
标签: javascript node.js typescript