节点--experimental-modules,请求的模块未提供名为 [英] node --experimental-modules, requested module does not provide an export named

查看:400
本文介绍了节点--experimental-modules,请求的模块未提供名为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了Node 8.9.1(v10.5.0中发生了同样的问题).

I've installed Node 8.9.1 (same problem happens in v10.5.0).

我正在尝试在.mjs

import { throttle } from lodash;

我跑步:

node --experimental-modules index.mjs

我得到:

SyntaxError:所请求的模块"lodash"未提供名为"throttle"的导出 在ModuleJob._instantiate(internal/modules/esm/module_job.js:80:21)

SyntaxError: The requested module 'lodash' does not provide an export named 'throttle' at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)

--experimental-modules应该在v10 LTS中停止实验 ,那么为什么没有更多的模块作者加入潮流呢?

--experimental-modules are supposed to stop being experimental in v10 LTS, so why haven't more module authors jumped on the bandwagon?

推荐答案

编辑的新内容(更好)答案

Node团队的速度很慢.同时,带给我们Lodash的那个家伙(John-David Dalton)设想了一个辉煌解决方案,他的想法是在2019年获得完全ES6模块支持的最佳方法.

The Node team is ... slow. Meanwhile, the same guy who brought us Lodash (John-David Dalton) imagined a brilliant solution, and his idea is the best way to get full ES6 module support in 2019.

(实际上,我删除我以前的答案,但出于历史目的,我已经保留了它.)

(In fact, I want to delete my earlier answer, but I've left it for historical purposes.)

新解决方案超级简单.

步骤1:

npm i esm

(( https://www.npmjs.com/package/esm 了解软件包的详细信息)

(https://www.npmjs.com/package/esm for package details)

步骤2:

node -r esm yourApp.js

这就是全部:实际上就是那么容易.只需将-r esm添加为Node arg,一切就可以神奇地工作(它的输入内容比--experimental-modules还要少!)谢谢约翰·戴维·道尔顿(John-David Dalton)!

That's the entirety of it: it's really just that easy. Just add -r esm as a Node arg, and everything just magically works (it's even less typing than --experimental-modules!) Thank you John-David Dalton!!!

正如我在原始答案中所说的,大概某天节点最终将释放对ES6的完全支持,但是在这种情况下,采用它就像从一些脚本中删除"-r esm"一样容易: D

As I said in my original answer, presumably someday Node will finally release full ES6 support, but when that happens adopting it will be as easy as removing "-r esm" from a few scripts :D

最后,要归功于应收款项,虽然我没有从他的回答中找到答案,但是@Divyanshu Rawat实际上在我进行此更新之前就已经提供了该库的前身的答案.

Finally, to give credit where due, while I didn't find it through his answer, @Divyanshu Rawat actually provided an answer with the precursor to this library long before I made this update.

原始答案

--experimental-modules尚不支持命名导出:

-实验模块不支持从commonjs模块(节点自身的内置组件除外)导入命名导出.

--experimental-modules doesn't support importing named exports from a commonjs module (except node's own built-ins).

  • https://github.com/apollographql/graphql-tools/issues/913
    • https://github.com/apollographql/graphql-tools/issues/913
    • 这就是为什么您无法使用语法的原因:

      This is why you are unable to use the syntax:

       import { throttle } from 'lodash';
      

      相反(至少现在),您必须破坏所需的东西:

      Instead (for now at least) you have to destruct what you need:

       import lodash from 'lodash';
       const { throttle } = lodash;
      

      大概有朝一日,Node将增加对所有ES Module功能的支持.

      Presumably someday Node will add support for all of the ES Module features.

      这篇关于节点--experimental-modules,请求的模块未提供名为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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