Repository navigation
ES Modules does not work with namespaced library #27408
Description
Activity
the modules implementation currently disables extension resolution. you'll need to use
--es-module-specifier-resolution=nodeto get the good behaviour or go into the lodash and uuid folders and check what the extensions of those files are and add that to the import.Reacted by madmoizo, German Prostakov, Aaron Adams, Adel Bruno Grimm, Franck Freiburger, jkhsjdhjs, Ensar, Christopher Hauschild Schott, William Pansky, Volodymyr Ishchenko and 6 moreReacted by Brian Kim, Arthur Luiz and Bogdan BeleReacted by Brian Kim and Arthur LuizReacted by William Pansky, Volodymyr Ishchenko and SiyuanReacted by Brian Kim and Dan Dascalescu- addedesmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.experimentalIssues and PRs related to experimental features.Issues and PRs related to experimental features.
on Apr 25, 2019 Closing as answered.
Reacted by Dan Dascalescu, Zack Eckersley Pallett, Ben McCann and Herve NkurikiyimfuraHad this same issue, @devsnek solution worked, thanks!
this is sort of an ongoing feedback thing for modules team so i'm gonna re-open it
cc @bnoordhuis
Reacted by Jordan Harband, Muhammad Soban, Dan Dascalescu, Ben McCann and Herve NkurikiyimfuraReacted by Brian Kim@aadamsx how are you using lodash-es + experimental-modules flag on node?
This
node --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node index.jsdoes not work for me with the following code://index.js import func from './func.js'; console.log(func(1, 1.2));// func.js import { round } from 'lodash-es'; console.log(round); export default (a, b) => round(a + b);Error =>
file:///Users/damiano/Desktop/node-jest/func.js:1 import { round } from 'lodash-es'; ^^^^^ SyntaxError: The requested module 'lodash-es' does not provide an export named 'round' at ModuleJob._instantiate (internal/modules/esm/module_job.js:93:21) at async ModuleJob.run (internal/modules/esm/module_job.js:108:20) at async Loader.import (internal/modules/esm/loader.js:128:24) error Command failed with exit code 1.Reacted by Andrew Shankie and SoperMatadoreRegarding
Currently only the “default export” is supported for CommonJS files or packages
Is node or the modules loader able to see that the file is exported via
module.exports? If so, would it not be possible to export the hole object as default and every object entry as named export?
eg.commonjs.js
module.exports = { a: 1, b: 2, c: 3, }node12.js:
import abc, { a as foobar } from './commonjs.js' abc.a === 1 && foobar === 1 && console.log('Yippi')no, we have to declare the export names before evaluation, so we don't know what module.exports looks like.
Have the same issue. And then when I use
--es-module-specifier-resolution=nodeit breaks CommonJS modules in the namespaced library projectSyntaxError: The requested module 'date-fns' does not provide an export named 'format'Have the same issue. And then when I use
--es-module-specifier-resolution=nodeit breaks CommonJS modules in the namespaced library projectSyntaxError: The requested module 'date-fns' does not provide an export named 'format'Have same issue with date-fns. Try using this syntax, works for me:
import format from 'date-fns/format'Reacted by Brian Kim@ench0 @TrevTheDev be careful:
import format from 'date-fns/format'works only because the format.js file is not importing other files, otherwise death ☠️.Try to import a
lodash-es/whatever, or also adate-fns/whateverwhich imports other modules in the same library.@ench0 @TrevTheDev be careful:
import format from 'date-fns/format'works only because the format.js file is not importing other files, otherwise death ☠️.Try to import a
lodash-es/whatever, or also adate-fns/whateverwhich imports other modules in the same library.Tried, this is my full list of imports, no issues at all:
// @ts-ignore import toDate from 'date-fns/toDate' import addDays from 'date-fns/addDays' import addHours from 'date-fns/addHours' import addMinutes from 'date-fns/addMinutes' import getYear from 'date-fns/getYear' import getMonth from 'date-fns/getMonth' import getDate from 'date-fns/getDate' import startOfDay from 'date-fns/startOfDay' import endOfDay from 'date-fns/endOfDay' import isBefore from 'date-fns/isBefore' import isAfter from 'date-fns/isAfter' import isWithinInterval from 'date-fns/isWithinInterval' import differenceInSeconds from 'date-fns/differenceInSeconds'5 remaining items
@guybedford I understand, but what is the intended path for all the ecosystem to move to ESM then?
Do you expect all package mantainers to add theexportsfield in thepackage.json?Reacted by Brian KimReacted by Brian KimReacted by Brian KimReacted by Brian Kim, Vladyslav Dalechyn and GeorgeAliatimisReacted by Brian Kim and RichReacted by Brian KimReacted by Brian Kim and Arto Bendiken- As a package consumer, just including extensions for any resolution not governed by main or exports. And this will ensure compatibility in other JS environments too.…On Sat, Apr 11, 2020 at 00:23 Damiano ***@***.***> wrote: @guybedford <https://github.com/guybedford> I understand, but what is the intended path for all the ecosystem to move to ESM then? Do you expect all package mantainers to add the exports field in the package.json? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#27408 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAESFSX7V2VUDL65FJC5R3DRMALF5ANCNFSM4HILSXPQ> .Reacted by Brian Kim and Vladyslav Dalechyn
@guybedford I'm not sure I understood, my bad!
Do you know a library who is handling both esm and cjs correctly at the moment so I can get an idea on how to move?Reacted by Brian Kim@damianobarbati in https://unpkg.com/browse/[email protected]/, i'm using the exports field combined with
mainto loadindex.jsas CJS and in browsers,node.jsin CJS node, andnode.mjsin ESM node. I'm also allowing./package.jsonand./package(which only works for require atm, but would work for import in the future if JSON ESM became a thing)Reacted by Brian Kim@ljharb thank you! I'll give it a try soon.
- added a commit that references this issue
on Jan 5, 2021 - added 5 commits that reference this issue
on Mar 29, 2021 - added a commit that references this issue
on Mar 30, 2021 - added 2 commits that reference this issue
on Mar 15, 2023
I'm using
experimental-modulessince v8. I tried to use"type": "module"in v12 and it's fine except for namespaced library (uuid, lodash, ...)This kind of imports:
Causes this error: