简体   繁体   中英

Chai Unexpected token when importing

So i have this very very simple dummy test:

import {expect}  from 'chai';

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

Here i get the following error when i run it:

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

SyntaxError: Unexpected token {

Can anyone tell me whats going on?

You need to transpile your code to use commonjs imports. You can use babel for such a task, see the docs https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs .

Via cli you can run babel --plugins @babel/plugin-transform-modules-commonjs script.js and then simply run your script.

If you don't want to use either transpilation (using babel or whatever) or commonjs you could use a loader such as esm : https://www.npmjs.com/package/esm .

From the docs, after running yarn add esm you can just node -r esm main.js .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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