使用 --experimental-modules 在 Node 中导入 ES6 [英] ES6 imports in Node with --experimental-modules

查看:92
本文介绍了使用 --experimental-modules 在 Node 中导入 ES6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to use ES6 imports in node with the -experimental-modules flag. Specifically:

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

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

Now place the following code in 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>

想法?

推荐答案

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

Moving averages currently only has a single default export. You will need to import the whole module and then you have the option to de-structure the arguments.

import movingAverages from 'moving-averages'

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

这篇关于使用 --experimental-modules 在 Node 中导入 ES6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆