我收到此错误:
D:\nginx\ibdrweb\webapps\ibdr-document-2\doc-app\sources\forms\dynamic-form\lus\lus.js:1
import { Tools } from "@ShareUtils/tools";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:721:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
当我尝试执行以下命令时:
ts-mocha -p doc-app/tests/tsconfig.json doc-app/tests/lus-tests.ts
此问题在此处https://github.com/Microsoft/TypeScript/issues/26018中进行了讨论,并且已经关闭。 尝试在tsconfig.json上更改“ module”:“ commonjs” 无济于事。 许多人写道问题仍然存在。
我的项目看起来像这样
|- doc-app
| |- < many direcotiries used at tests.ts >
| |- tests
| lus-tests.ts
| tsconfig.json
|
|- node_modules
|- tsconfig.json
注意:节点“ v10.16.0”
tsconfig.json 在测试目录中
{
"compilerOptions": {
"target": "es5",
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"lib": ["es6", "dom"],
"typeRoots": [
"node_modules/@types"
]
},
"awesomeTypescriptLoaderOptions": {
"useWebpackText": true,
"useTranspileModule": true,
"doTypeCheck": true,
"forkChecker": true
},
"include": [
"./"
],
"exclude": [
"node_modules"
]
}
package.json的有用部分
"devDependencies": {
"@types/chai": "4.1.7",
"@types/mocha": "5.2.7",
"@types/sinon": "7.0.13",
...
"chai": "4.2.0",
...
"mocha": "6.1.4",
...
"sinon": "7.3.2",
...
"ts-mocha": "6.0.0",
"ts-node": "8.4.1",
"typescript": "2.9.2", //if change it to "3.6.3" it dosen't help
}
如果我在tsconfig.json中指定“ module”:“ exnext”,则错误为
D:\nginx\ibdrweb\webapps\ibdr-document-2\doc-app\tests\lus-tests.ts:1
import "mocha";
^^^^^^^
SyntaxError: Unexpected string
at Module._compile (internal/modules/cjs/loader.js:721:23)
注意: :从“ @ ShareUtils / tools”导入{工具}; node_modules中的模块“工具”
答案 0 :(得分:1)
第一个错误是由于TypeScript未编译您的JavaScript引起的。您将需要allowJs
才能使该文件按预期工作。第二个错误是有道理的,当您从CommonJS更改模块输出时,node.js无法理解您的ES模块代码。