使用--experimental-modules在Node中导入ES6

时间:2018-02-08 10:05:33

标签: javascript node.js npm

尝试在带有-experimental-modules标志的节点中使用ES6导入。具体做法是:

mkdir ma
cd ma
npm init
npm i --save moving-averages
touch index.mjs

现在将以下代码放在index.mjs中:

import {
ma, dma, ema, sma, wma
} from 'moving-averages'

ma([1, 2, 3, 4, 5], 2) 

结果是:

file:///home/ole/ma/index.mjs:2
  ma, dma, ema, sma, wma
  ^^
SyntaxError: The requested module does not provide an export named 'ma'
    at ModuleJob._instantiate (internal/loader/ModuleJob.js:86:19)
    at <anonymous>

思想?

1 个答案:

答案 0 :(得分:1)

移动平均线目前只有一个默认导出。您将需要导入整个模块,然后您可以选择解构参数。

import movingAverages from 'moving-averages'

const {ma, dma, ema, sma, wma} = movingAverages;