镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

--es-module-specifier-resolution=node not working from v13.0.1 #30520

Description

@jaydenseric

--es-module-specifier-resolution=node is documented for v13.x:

Screen Shot 2019-11-18 at 7 16 48 pm

Here is the specific example that works in v13.0.0, but not in v13.0.1 or v13.1.0:

node --experimental-modules --es-module-specifier-resolution=node index

Screen Shot 2019-11-18 at 7 29 02 pm

As you can see in the screenshot above, the error is:

internal/modules/cjs/loader.js:895
    throw err;
    ^

Error: Cannot find module '/Users/jaydenseric/Desktop/test/index'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.Module._load (internal/modules/cjs/loader.js:785:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1143:12)
    at internal/main/run_main_module.js:16:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Activity

  1. guybedford commented on Nov 19, 2019

    @guybedford
    Contributor

    The fix for this would be to ensure that .mjs is included in require.extensions when this flag is enabled.

    This is because the CJS loader is doing the main entry resolution, and does not include .mjs resolution by default in its require.extensions list.

    A way to get it to work would be to write:

    extensions.js

    require._extensions['.mjs'] = require._extensions['.js'];

    which would allow:

    node -r extensions.js --es-module-specifier-resolution=node index
    

    A PR to add this when the flag is set is welcome.

  2. pd4d10 commented on Nov 21, 2019

    @pd4d10
    Contributor

    @guybedford Thanks for the explanation.

    I tried to create a PR to fix this issue: #30574. Please correct me if I understand it wrong.

  3. dandv commented on Feb 8, 2020

    @dandv
    Contributor

    Has this flag been renamed to experimental-specifier-resolution?

  4. MylesBorins commented on Feb 19, 2020

    @MylesBorins
    Contributor

    @dandv we have renamed the flag, but we continue to support --es-module-specifier-resolution=node which silently aliases to --experimnetal-specifier-resolution

  5. bolinfest commented on Mar 11, 2020

    @bolinfest

    @guybedford when I create extensions.js as you describe and run:

    $ cat extensions.js
    // https://github.com/nodejs/node/issues/30520
    require._extensions['.mjs'] = require._extensions['.js'];
    $ node -r extensions.js --es-module-specifier-resolution=node --experimental-modules lib/server/main.js
    

    I get:

    internal/modules/cjs/loader.js:979
      throw err;
      ^
    
    Error: Cannot find module 'extensions.js'
    Require stack:
    - internal/preload
        at Function.Module._resolveFilename (internal/modules/cjs/loader.js:976:15)
        at Function.Module._load (internal/modules/cjs/loader.js:859:27)
        at Module.require (internal/modules/cjs/loader.js:1036:19)
        at Module._preloadModules (internal/modules/cjs/loader.js:1292:12)
        at loadPreloadModules (internal/bootstrap/pre_execution.js:448:5)
        at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:71:3)
        at internal/main/run_main_module.js:7:1 {
      code: 'MODULE_NOT_FOUND',
      requireStack: [ 'internal/preload' ]
    }
    

    what am I missing?

  6. trusktr commented on Nov 2, 2020

    @trusktr
    Contributor

    @dandv we have renamed the flag, but we continue to support --es-module-specifier-resolution=node which silently aliases to --experimnetal-specifier-resolution

    @MylesBorins I'm in Node 14.8 and trying to use --experimnetal-specifier-resolution gives me

    node: bad option: --experimnetal-specifier-resolution
    

    however using --es-module-specifier-resolution=node works.

  7. ljharb commented on Nov 2, 2020

    @ljharb
    SponsorMember

    @trusktr experimental, not experimnetal?

  8. trusktr commented on Nov 2, 2020

    @trusktr
    Contributor

    Oops, I copy-pasted from above. xD

  9. trusktr commented on Nov 2, 2020

    @trusktr
    Contributor

    Ironically fixing the typo wasn't all. It requires a value: --experimental-specifier-resolution=node

  10. Santosh-13 commented on Jan 8, 2021

    @Santosh-13

    getting this error node: bad option: --es-module-specifier-resolution=node
    Node version: 10.19.0

  11. wojtekmaj commented on Jan 22, 2021

    @wojtekmaj

    @Santosh-13 That flag is working on Node 12 and up.

  12. viT-1 commented on Dec 23, 2021

    @viT-1

    node v16.13.1, Windows 10

    getting this error

    Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/C:/[local]/dist-gh-pages/src/common/GreeterUse.js' imported from C:\[local]\dist-gh-pages\src\main.js
        at new NodeError (node:internal/errors:371:5)
        at finalizeResolution (node:internal/modules/esm/resolve:405:11)
        at moduleResolve (node:internal/modules/esm/resolve:932:10)
        at defaultResolve (node:internal/modules/esm/resolve:1044:11)
        at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
        at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
        at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
        at link (node:internal/modules/esm/module_job:75:36) {
      code: 'ERR_MODULE_NOT_FOUND'
    }
    

    with script
    "greetnode": "node --experimental-modules --es-module-specifier-resolution=node ./src/main.js"
    and package.json

    "type": "module",
    "imports": {
    	"#common/*": "./src/common/*.js"
    },
    

    Now it is the expected behaviour?
    How can I run script without errors?

  13. jaybe78 commented on Jan 12, 2022

    @jaybe78

    @viT-1 I've been getting the same issue for days. I'm also using node v16.13.1

    Imports of ES^ module in my app are not resolved

    - This thread says that you have to add the '.js' extension to solve the problem but that didn't work for me.

    That's my ts.config.server.json

    {
      "extends": "./tsconfig.json",
      "compilerOptions": {
        "jsx": "react",
        "baseUrl": ".",
        "module": "esnext",
        "target": "esnext",
        "paths": {
          "~/*": ["*"],
          "@today/shared/*": ["../../node_modules/@today/shared/dist/*"]
        },
        "outDir": ".next/",
        "downlevelIteration": true,
      },
      "include": ["server"]
    }
    
    

    ts-node command

    NODE_OPTIONS="--loader ts-node/esm --es-module-specifier-resolution=node" NODE_ENV=development NEXT_PUBLIC_RUNNING_LOCAL_INSTANCE=true ts-node -r tsconfig-paths/register --project ./tsconfig.server.json server/index.mjs --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts'
    

    fails with

     Cannot find module '/node_modules/@todaytix/shared/config/server/ssl.js' imported from /websites/lt-nytg/server/index.mjs
    
  14. viT-1 commented on Jan 19, 2022

    @viT-1

    @jaybe78 For ts-project I solved aliases 2 years ago.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions