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

[Node 19 + ESM + PNP] ERR_MODULE_NOT_FOUND #1972

Closed
rtritto opened this issue Mar 18, 2023 · 3 comments
Closed

[Node 19 + ESM + PNP] ERR_MODULE_NOT_FOUND #1972

rtritto opened this issue Mar 18, 2023 · 3 comments

Comments

@rtritto
Copy link

rtritto commented Mar 18, 2023

Search Terms

I'm starting a new project with yarn berry (Plug'n'Play enabled) and using ESM to test Node 19 features.

Expected Behavior

No ERR_MODULE_NOT_FOUND error

Actual Behavior

C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:757
  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
        ^
CustomError: Cannot find package 'undici' imported from C:\<PROJECT_PATH>\test-pnp-node\example.ts
    at packageResolve (C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:757:9)
    at moduleResolve (C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:798:18)
    at Object.defaultResolve (C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\dist-raw\node-internal-modules-esm-resolve.js:912:11)
    at C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\src\esm.ts:218:35
    at entrypointFallback (C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\src\esm.ts:168:34)
    at C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\src\esm.ts:217:14
    at addShortCircuitFlag (C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\src\esm.ts:409:21)
    at resolve (C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\src\esm.ts:197:12)
    at resolve (C:\<PROJECT_PATH>\test-pnp-node\.yarn\__virtual__\ts-node-virtual-6c4c0611aa\3\Users\<USER>\AppData\Local\Yarn\Berry\cache\ts-node-npm-10.9.1-6c268be7f4-9.zip\node_modules\ts-node\src\child\child-loader.ts:15:39)
    at nextResolve (node:internal/modules/esm/hooks:654:28)

Steps to reproduce the problem

  • yarn init -y
  • yarn set version canary
  • yarn add undici typescript ts-node @types/node
  • in package.json
    • add "type": "module"
    • change main to example.ts (optional/useless step)
  • in .yarnrc.yml add nodeLinker: pnp (optional/useless step, Plug'n'Play is already enabled by default)
  • create tsconfig.json file:
{
  "compilerOptions": {
    "esModuleInterop": true,  // optional/irrelevant
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "nodenext"  // irrelevant using node or bundler
  },
  "ts-node": {
    "esm": true
    // "experimentalSpecifierResolution": "node"  // optional/irrelevant
  }
}
  • create example.ts file
import { request } from 'undici'

request('')
  • yarn ts-node example.ts

Minimal reproduction

https://github.com/rtritto/ts-node-repros/tree/test-pnp-node

Specifications

  • ts-node version: 10.9.1

  • node version: 19.8.1

  • TypeScript version: 5.0.2

  • package.json:

{
  "name": "test-pnp-node",
  "version": "1.0.0",
  "main": "index.ts",
  "type": "module",
  "license": "MIT",
  "packageManager": "yarn@4.0.0-rc.40",
  "dependencies": {
    "@types/node": "^18.15.3",
    "ts-node": "^10.9.1",
    "typescript": "^5.0.2",
    "undici": "^5.21.0"
  }
}
  • Operating system and version:
    Windows
  • If Windows, are you using WSL or WSL2?:
    No
  • Correctly work with nodeLinker: node-modules
@rtritto
Copy link
Author

rtritto commented Mar 18, 2023

Fixed with:
yarn node --loader ts-node/esm --loader ./.pnp.loader.mjs example.ts

Source

Related change

@rtritto rtritto closed this as completed Mar 18, 2023
@rtritto
Copy link
Author

rtritto commented Mar 18, 2023

@cspotcode should ts-node autoinclude double loader (.pnp.loader.mjs when project is yarn berry based) when PnP is enabled and when command ts-node example.ts is used?

I think that initial post of #1007 should be updated with PnP case.

@bafu
Copy link

bafu commented May 26, 2023

Yarn PNP has not supported ESM. There are some temporary solutions:

  1. Rollback to node_modules by adding nodeLinker: node-modules in .yarnrc.yml.

    For the details, you can check the explanation from the Yarn upstream.

  2. Enable experimental ESM loader by adding pnpEnableEsmLoader: true in .yarnrc.yml.

    For the details, you can check these comments: comment 1, comment 2

  3. Enable experimental ESM loader by adding "type": "module" in package.json.

After any modification above, run yarn install again to ensure Yarn generates the necessary assistant files for you.

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

No branches or pull requests

2 participants