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

yarn pnp not working with ts-node and ESmodule #1709

Closed
Darkbladecr opened this issue Apr 7, 2022 · 8 comments
Closed

yarn pnp not working with ts-node and ESmodule #1709

Darkbladecr opened this issue Apr 7, 2022 · 8 comments
Labels

Comments

@Darkbladecr
Copy link

Darkbladecr commented Apr 7, 2022

Search Terms

ts-node pnp

Expected Behavior

Able to run ts-node with yarn pnp for a simple typescript file.

Actual Behavior

Breaking on initiation of the esm_loader => ts-node/child-loader
yarn ts-node ./src/index.ts

node:internal/process/esm_loader:94
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/stefan/ts-node/.yarn/__virtual__/ts-node-virtual-645473c738/0/cache/ts-node-npm-10.7.0-ef39b1d45e-2a379e43f7.zip/node_modules/ts-node/child-loader.mjs' imported from /Users/stefan/ts-node/
Did you mean to import /Users/stefan/ts-node/.yarn/__virtual__/ts-node-virtual-645473c738/0/cache/ts-node-npm-10.7.0-ef39b1d45e-2a379e43f7.zip/node_modules/ts-node/child-loader.mjs?
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:418:11)
    at moduleResolve (node:internal/modules/esm/resolve:983:10)
    at defaultResolve (node:internal/modules/esm/resolve:1080:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
    at ESMLoader.import (node:internal/modules/esm/loader:332:22)
    at initializeLoader (node:internal/process/esm_loader:74:49)
    at loadESM (node:internal/process/esm_loader:87:11)
    at runMainESM (node:internal/modules/run_main:51:21) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Steps to reproduce the problem

yarn init -2
yarn install -D typescript ts-node tslib @types/node
package.json - updated to have "type": "module" - for ESmodule setup
touch tsconfig.json -- see below
mkdir src
touch src/index.ts -- see below
yarn ts-node src/index.ts

Minimal reproduction

Repo prepared here:
https://github.com/Darkbladecr/ts-node-pnp

package.json

  "name": "ts-node",
  "packageManager": "[email protected]",
  "type": "module",
  "main": "./dist/index.js",
  "exports": {
    ".": "./dist/index.js",
    "./*": "./dist/*"
  },
  "typesVersions": {
    "*": {
      "main.d.ts": [
        "dist/index.d.ts"
      ],
      "*": [
        "dist/*"
      ]
    }
  },
  "devDependencies": {
    "@types/node": "^17.0.23",
    "ts-node": "^10.7.0",
    "tslib": "^2.3.1",
    "typescript": "~4.3"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "ES2020",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "lib": ["es2020", "DOM"],
    "skipLibCheck": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "declaration": true,
    "baseUrl": ".",
    "removeComments": true,
    "downlevelIteration": true,
    "resolveJsonModule": true
  },
  "ts-node": {
    "esm": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.test.ts"]
}

src/index.ts

import path from "node:path";
console.log(path.resolve("."));

Specifications

  • ts-node version: "^10.7.0"

  • node version: "v16.14.2"

  • TypeScript version: have tried ~4.3, ~4.4, ~4.5, ~4.6

  • tsconfig.json, if you're using one (see above)

  • Operating system and version: OSX 12.3

@cspotcode
Copy link
Collaborator

This is probably a yarn limitation; I recommend checking with them.

@Darkbladecr
Copy link
Author

Yarn pnp works fine with ts-node, but not when the project is setup as a ESmodule. This is the key distinction here. I can change the package to
nodeLinker: node-modules in the .yarnrc.yml and everything works again.
Will bring up the issue with yarn as well.

@Darkbladecr Darkbladecr changed the title yarn pnp not working with ts-node yarn pnp not working with ts-node and ESmodule Apr 7, 2022
@cspotcode
Copy link
Collaborator

Yarn pnp works fine with ts-node, but not when the project is setup as a ESmodule.

Yes, that implies to me that this is a limitation of node and yarn with ESmodules. Keep in mind, node's support for certain esmodule features is experimental and incomplete:
https://nodejs.org/dist/latest-v17.x/docs/api/esm.html#loaders
https://nodejs.org/dist/latest-v17.x/docs/api/documentation.html#stability-index
https://github.com/nodejs/loaders/issues

@Darkbladecr
Copy link
Author

Appears to be an issue directly with node and its loaders
nodejs/loaders#56

@shixinhuang99
Copy link

you can use yarn unplug ts-node, it is works for me.

@ferm10n
Copy link

ferm10n commented Feb 14, 2023

For future visitors to this issue, I fixed this by updating my yarn version from 3.2.3 to 3.4.1 (current latest)

@yuriy-yarosh
Copy link

@shixinhuang99
FYI node --loader ts-node/esm --loader ./.pnp.loader.mjs ./Script.ts

Works just fine, even with "type": "module"
No need to unplug.

@shixinhuang99
Copy link

@shixinhuang99 FYI node --loader ts-node/esm --loader ./.pnp.loader.mjs ./Script.ts

Works just fine, even with "type": "module" No need to unplug.

Thanks for the advice but I've moved on to pnpm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants