63

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

I'm trying to use named imports from npm packages in a file with the .mjs

import { throttle } from lodash;

I run:

node --experimental-modules index.mjs

and I get:

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 are supposed to stop being experimental in v10 LTS, so why haven't more module authors jumped on the bandwagon?

2

6 Answers 6

76

EDITED NEW (AND MUCH BETTER) ANSWER

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.)

The new solution is SUPER simple.

Step #1:

npm i esm

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

Step #2:

node -r esm yourApp.js

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!!!

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

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.

ORIGINAL ANSWER

--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.

11
  • I disagree with the very first statement in this answer. @stackdave's import { throttle } from lodash; would work if lodash had been written as modules, i.e. lodash/index.mjs exported throttle like this export const throttle = ...;. Apr 20, 2019 at 21:25
  • 5
    I don't understand what you're disagreeing with: Lodash is written the way it's written. Your hypothetical talk about lodash being written some other way doesn't invalidate the answer. Apr 21, 2019 at 21:53
  • is this still the case? Will named exports be supported directly by ESM?
    – Zach Smith
    Jul 31, 2019 at 18:41
  • 1
    I'll just add that if you want to use that syntax in Typescript with strict mode, you should enable the "allowSyntheticDefaultImports" compiler option
    – Urigo
    Jan 17, 2020 at 19:03
  • 4
    Sadly, for Node.js > v12.12.0 (including all Node.js 13 and Node.js 14), this package does not work. The issue has been opened for several months now, so I wouldn't expect much from this package. Link to the issue: https://github.com/standard-things/esm/issues/868
    – TheMrZZ
    Apr 22, 2020 at 18:49
8

I just had this error with nodejs express *.mjs file and --experimental-modules flag enabled for googleapis.

import { google } from "googleapis";

SyntaxError: The requested module 'googleapis' does not provide an export named 'google'

Solution

//not working!
//import { google } from "googleapis";

//working
import googleapis from "googleapis";
const { google } = googleapis;

I do not understand why this is the case; if anyone knows why, please comment.

1
  • This is because ESM modules have a custom concept of "named exports" rather than just exporting an object and the import statement using destructuring. May 27, 2020 at 8:46
6

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()
4
  • thanks I've updated my answer with a new error, sorry that the propose of my answer is be able to run modules with node 8
    – stackdave
    Nov 14, 2017 at 5:23
  • updated. it's better if you can post the content of your code Nov 14, 2017 at 5:42
  • I've realize all files imported with modueles across the project must have .mts extension. See the the update on my answer.
    – stackdave
    Nov 23, 2017 at 16:49
  • 13
    OP was asking about importing from an npm module, not their own. Sep 26, 2018 at 4:55
6

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';
2
  • 2
    I just added what helped me and someone downvoted it can anyone explain. Sep 24, 2019 at 9:23
  • 2
    I don't know why this got downvoted originally; maybe you didn't do a good enough job of explaining it? If it's any consolation I've actually updated my (accepted) answer to mention the esm module, which I believe is the successor to the one you mentioned (and FWIW I upvoted your answer). Dec 3, 2019 at 19:17
1

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).

0

@machineghost answer works. I remember also adding 'type':'module' to package.json along with using esm with node v12(LTS) and it worked fine.## Heading ##

I updated my node to v14(current) and I got an error

C:\Users\andey\Documents\Project\src\app.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: 
C:\Users\andey\Documents\Project\src\app.js
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1217:13) {
code: 'ERR_REQUIRE_ESM'
}

To fix it I had to remove 'type':'module' from package.json.

source

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.