node --experimental-modules, requested module does not provide an export named

邮差的信 提交于 2019-11-30 11:28:41
machineghost

--experimental-modules does not have support for named exports yet:

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

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;

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

You have to use .mjs extension.

Once this has been set, files ending with .mjs will be able to be loaded as ES Modules.

reference: https://nodejs.org/api/esm.html

Update:

Looks like you haven't export the method yet.

Suppose i have hello.mjs with content

export function sayHello() {
    console.log('hello')
}

i can use it in index.mjs like this

import {sayHello} from './hello.mjs'
sayHello()

If lodash had been written as modules, and lodash/index.mjs exported throttle: export const throttle = ...;, then you'd be able to import { throttle } from lodash;

The problem here is that in commonjs there's no such thing as a named export. Which means that in commonjs modules export one thing only.

So think that lodash exports an object containing a property named throttle.

For the second part of the question, I believe people will slowly start adopting ES Modules once it's not experimental anymore. At the time of this writing, it still is (Node.js v11.14).

For me loading lodash as ES Library did the job, here is the NPM Package for the same.

The Lodash library exported as ES modules. https://www.npmjs.com/package/lodash-es

Then you can import utils in normal way.

import { shuffle } from 'lodash-es';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!