简体   繁体   English

使用节点意外导入令牌

[英]Unexpected token import using node

I have a simple file numeral.js that I'm trying to test with mocha running through node. 我有一个简单的numerical.js文件,正在尝试通过节点运行的mocha进行测试。

An afternoon of searching however has left me at the first hurdle. 一个下午的搜寻却使我无处可去。

Numeral.js: Numeral.js:

"use strict";
exports.__esModule = true;
exports.numeral = function (id) {
    return "";
};

test.js test.js

import {numeral} from './numeral.js'

var assert = require('assert');

describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });

});

I navigate to the folder (that has mocha set up as test in the packages.json) and run npm test . 我导航到文件夹(在packages.json中将mocha设置为test)并运行npm test That throws out a: 这抛出了一个:

C:\Users\matt\Documents\mocha\test\test.js:1
(function (exports, require, module, __filename, __dirname) { import {numeral} from './numeral.js'
                                                              ^^^^^^
SyntaxError: Unexpected token import

How am I going so wrong? 我怎么会这么错?

Nodejs does not support import and export. Nodejs不支持导入和导出。

So, you have to use babel to covert your es6 code to es5 version if you like to use import/export. 因此,如果您想使用导入/导出,则必须使用babel将es6代码转换为es5版本。 take a look at this issue in github 看看github中的这个问题

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

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