编译器选项混淆:模块,esnext与commonjs?"/>
打字稿“导入”语法与编译器选项混淆:模块,esnext与commonjs?
我有一个带有一些摩卡测试的打字稿项目,开始时是这样:
import { assert } from "chai";
import "@material/mwc-ripple"; //I want to test a script that uses this
describe("simple test", () => {
it("works", () => {
assert.isOk(true)
});
});
在摩卡tsconfig.test.json
中,如果设置为"module": "esnext"
,则会出现以下错误:
/home/ec2-user/environment/frontend/test-mocha/common/datetime/aaa_aaa_test.ts:1
import { assert } from "chai";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
...
但是如果将其设置为"module": "commonjs"
,则会出现此错误:
/home/ec2-user/environment/frontend/node_modules/@material/mwc-ripple/mwc-ripple.js:1
import { __decorate } from "tslib";
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
...
怎么回事,应该如何解决?
回答如下:啊哈,我想我找到了罪魁祸首!
如果我改变
import "@material/mwc-ripple";
to
import { Ripple } from "@material/mwc-ripple";
清除错误。我猜测ts-node的不同导入语法提示将导入文件解析为javascript或打字稿。也许?
更多推荐
打字稿“导入”语法与编译器选项混淆:模块,esnext与commonjs?
发布评论