You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ES Modules does not work with namespaced library #27408
the modules implementation currently disables extension resolution. you'll need to use --es-module-specifier-resolution=node to 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 more
Reacted by Brian Kim, Arthur Luiz and Bogdan Bele
Reacted by Brian Kim and Arthur Luiz
Reacted by William Pansky, Volodymyr Ishchenko and Siyuan
@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.js does 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.
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')
Have the same issue. And then when I use --es-module-specifier-resolution=node it breaks CommonJS modules in the namespaced library project SyntaxError: 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=node it breaks CommonJS modules in the namespaced library project SyntaxError: 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'
@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 a date-fns/whatever which 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 a date-fns/whatever which 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'
@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?
Reacted by Brian Kim
Reacted by Brian Kim
Reacted by Brian Kim
Reacted by Brian Kim, Vladyslav Dalechyn and GeorgeAliatimis
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.
@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?
@damianobarbati in https://unpkg.com/browse/es-get-iterator@1.1.0/, i'm using the exports field combined with main to load index.js as CJS and in browsers, node.js in CJS node, and node.mjs in ESM node. I'm also allowing ./package.json and ./package (which only works for require atm, but would work for import in the future if JSON ESM became a thing)
Activity
devsnek commentedon Apr 25, 2019
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.bnoordhuis commentedon Apr 28, 2019
Closing as answered.
aadamsx commentedon May 5, 2019
Had this same issue, @devsnek solution worked, thanks!
devsnek commentedon May 5, 2019
this is sort of an ongoing feedback thing for modules team so i'm gonna re-open it
cc @bnoordhuis
damianobarbati commentedon May 23, 2019
@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:Error =>
valoricDe commentedon Sep 18, 2019
Regarding
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
node12.js:
devsnek commentedon Sep 18, 2019
no, we have to declare the export names before evaluation, so we don't know what module.exports looks like.
TrevTheDev commentedon Dec 14, 2019
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'ench0 commentedon Mar 9, 2020
Have same issue with date-fns. Try using this syntax, works for me:
import format from 'date-fns/format'damianobarbati commentedon Mar 9, 2020
@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 commentedon Mar 10, 2020
Tried, this is my full list of imports, no issues at all:
5 remaining items
damianobarbati commentedon Apr 11, 2020
@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
exportsfield in thepackage.json?guybedford commentedon Apr 11, 2020
damianobarbati commentedon Apr 15, 2020
@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?
ljharb commentedon Apr 15, 2020
@damianobarbati in https://unpkg.com/browse/es-get-iterator@1.1.0/, 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)damianobarbati commentedon Apr 15, 2020
@ljharb thank you! I'll give it a try soon.
Link to node --experimental-specifier-resolution
Add exports within package.json to enable scoped package loading.
Add exports within package.json to enable scoped package loading.
feat(esm): Add exports within package.json to enable scoped package l…
feat(esm): Add test case for esm imports.
feat(esm): Add test case for esm imports.
feat(esm): Add exports within package.json to enable scoped package l…
Use `--es-module-specifier-resolution=node`
Esm fixes (#34)
Changed imports to include index as specified in nodejs/node#27408 (c…