打字稿“导入”语法与编译器选项混淆:模块,esnext与commonjs?

编程入门 行业动态 更新时间:2024-10-06 16:31:14

打字稿“导入”语法与<a href=https://www.elefans.com/category/jswz/34/1771241.html style=编译器选项混淆:模块,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?

本文发布于:2024-05-07 07:45:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编译器   语法   模块   选项   commonjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!