Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 945
stackdave Avatar asked Nov 14 '17 04:11

stackdave


People also ask

Does the requested module does not provide an export named?

The error "The requested module does not provide an export named" occurs when mixing up default and named ES6 module imports and exports. To solve the error make sure to import default exports without using curly braces and import named exports with curly braces.

Does not provide an export named default in node?

To solve the error "The requested module does not provide an export named 'default'", use the default keyword when exporting a value from a file and don't wrap the corresponding import in curly braces. You can only have a single default export per file.

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.


1 Answers

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

  • 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; 

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

like image 105
machineghost Avatar answered Sep 27 '22 17:09

machineghost