繁体   English   中英

Chai 导入时出现意外令牌

[英]Chai Unexpected token when importing

所以我有一个非常简单的虚拟测试:

import {expect}  from 'chai';

describe('calculate', function () {
    it('add', function () {
        let result = 2 + 5;
        expect(result).equal(7);
    });
});

在这里我运行它时出现以下错误:

(function (exports, require, module, __filename, __dirname) { import { expect } from 'chai';
                                                                     ^

SyntaxError: Unexpected token {

谁能告诉我这是怎么回事?

您需要转译您的代码以使用 commonjs 导入。 您可以将 babel 用于此类任务,请参阅文档https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs

通过 cli 你可以运行babel --plugins @babel/plugin-transform-modules-commonjs script.js然后简单地运行你的脚本。

如果你不希望使用任何transpilation(使用巴别塔或其他)或CommonJS的,你可以使用加载器,如esmhttps://www.npmjs.com/package/esm

从文档中,运行yarn add esm您可以只使用node -r esm main.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM