Skip to content

ES Modules does not work with namespaced library #27408

Description

@madmoizo
  • Version: 12.0.0
  • Platform: Windows 10

I'm using experimental-modules since v8. I tried to use "type": "module" in v12 and it's fine except for namespaced library (uuid, lodash, ...)

This kind of imports:

import uuid from 'uuid/v4'
import countBy from 'lodash/countBy'

Causes this error:

internal/modules/esm/default_resolve.js:69
  let url = moduleWrapResolve(specifier, parentURL);
            ^
Error: Cannot find module 'd:\[...]\node_modules\uuid\v4' imported from d:\[...]\[...].mjs
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:69:13)
    at Loader.resolve (internal/modules/esm/loader.js:70:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:143:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
    at link (internal/modules/esm/module_job.js:42:36)

Activity

  1. devsnek commented on Apr 25, 2019

    @devsnek
    Member

    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.

  2. added
    esmIssues and PRs related to the ECMAScript Modules implementation.
    experimentalIssues and PRs related to experimental features.
    on Apr 25, 2019
  3. bnoordhuis commented on Apr 28, 2019

    @bnoordhuis
    Member

    Closing as answered.

  4. aadamsx commented on May 5, 2019

    @aadamsx

    Had this same issue, @devsnek solution worked, thanks!

  5. devsnek commented on May 5, 2019

    @devsnek
    Member

    this is sort of an ongoing feedback thing for modules team so i'm gonna re-open it

    cc @bnoordhuis

  6. reopened this on May 5, 2019
  7. damianobarbati commented on May 23, 2019

    @damianobarbati

    @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.
    
  8. valoricDe commented on Sep 18, 2019

    @valoricDe

    Regarding

    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')
    
  9. devsnek commented on Sep 18, 2019

    @devsnek
    Member

    no, we have to declare the export names before evaluation, so we don't know what module.exports looks like.

  10. TrevTheDev commented on Dec 14, 2019

    @TrevTheDev

    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'

  11. ench0 commented on Mar 9, 2020

    @ench0

    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'

  12. damianobarbati commented on Mar 9, 2020

    @damianobarbati

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

  13. ench0 commented on Mar 10, 2020

    @ench0

    @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'
    
  14. 5 remaining items

  15. damianobarbati commented on Apr 11, 2020

    @damianobarbati

    @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?

  16. guybedford commented on Apr 11, 2020

    @guybedford
    Contributor
  17. damianobarbati commented on Apr 15, 2020

    @damianobarbati

    @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?

  18. ljharb commented on Apr 15, 2020

    @ljharb
    SponsorMember

    @damianobarbati in https://unpkg.com/browse/[email protected]/, 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)

  19. damianobarbati commented on Apr 15, 2020

    @damianobarbati

    @ljharb thank you! I'll give it a try soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.experimentalIssues and PRs related to experimental features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions