Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jaydenseric opened this issue Nov 18, 2019 · 14 comments
Closed

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

jaydenseric opened this issue Nov 18, 2019 · 14 comments

Comments

@jaydenseric
Copy link
Contributor

--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: []
}
@guybedford
Copy link
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.

@pd4d10
Copy link
Contributor

pd4d10 commented Nov 21, 2019

@guybedford Thanks for the explanation.

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

@Trott Trott closed this as completed in f5ef7cd Nov 28, 2019
addaleax pushed a commit that referenced this issue Nov 30, 2019
Fixes: #30520

PR-URL: #30574
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
MylesBorins pushed a commit that referenced this issue Jan 12, 2020
Fixes: #30520

PR-URL: #30574
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
BethGriggs pushed a commit that referenced this issue Feb 6, 2020
Fixes: #30520

PR-URL: #30574
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
@dandv
Copy link
Contributor

dandv commented Feb 8, 2020

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

@MylesBorins
Copy link
Contributor

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

@bolinfest
Copy link

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

@trusktr
Copy link
Contributor

trusktr commented Nov 2, 2020

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

@ljharb
Copy link
Member

ljharb commented Nov 2, 2020

@trusktr experimental, not experimnetal?

@trusktr
Copy link
Contributor

trusktr commented Nov 2, 2020

Oops, I copy-pasted from above. xD

@trusktr
Copy link
Contributor

trusktr commented Nov 2, 2020

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

@Santosh-13
Copy link

Santosh-13 commented Jan 8, 2021

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

@wojtekmaj
Copy link

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

@viT-1
Copy link

viT-1 commented Dec 23, 2021

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?

@jaybe78
Copy link

jaybe78 commented Jan 12, 2022

@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

@viT-1
Copy link

viT-1 commented Jan 19, 2022

@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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.