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

ERR_MODULE_NOT_FOUND error has undefined code #1862

Open
ldiqual opened this issue Aug 12, 2022 · 1 comment
Open

ERR_MODULE_NOT_FOUND error has undefined code #1862

ldiqual opened this issue Aug 12, 2022 · 1 comment

Comments

@ldiqual
Copy link

ldiqual commented Aug 12, 2022

Search Terms

code. Found this but not sure if related.

Expected Behavior

When wrapping an import with try/catch, and when pointing the import to a TS file that does not exist, the error caught should have code = ERR_MODULE_NOT_FOUND.

ts-node seems to properly throw this error here:

throw new ERR_MODULE_NOT_FOUND(
fileURLToPath(new URL('.', packageJSONUrl)), fileURLToPath(base));

Actual Behavior

err.code is undefined in ESM context, but defined in CommonJS context, when caught in the importing script.

Steps to reproduce the problem

ESM

// error.mjs, call with `ts-node ./error.mjs`
try {
  await import('./test.ts') // Does not exist
} catch (err) {
  console.log(err.stack) // Has the proper message & stack (Cannot find module...)

  // Undefined, should be ERR_MODULE_NOT_FOUND
  // Note that if you run the same code with `node` instead, code is defined.
  console.log(err.code)
}

CommonJS

// error.cjs, call with `ts-node ./error.cjs`
try {
  require('./test.ts') // Does not exist
} catch (err) {
  console.log(err.stack) // Has the proper message & stack (Cannot find module...)
  console.log(err.code) // MODULE_NOT_FOUND
}

Specifications

  • ts-node version: v10.9.1
  • node version: v16.16.0
  • TypeScript version: v4.7.4
  • tsconfig.json, if you're using one:
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "target": "es2021",
    "lib": ["es2021"],
    "module": "node16",
    "moduleResolution": "node16",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "allowSyntheticDefaultImports": true,
    "allowJs": true,
    "useUnknownInCatchVariables": false,
    "composite": true,
    "declaration": true,
    "removeComments": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUnusedLocals": true,
    "resolveJsonModule": true,
    "preserveSymlinks": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "incremental": true
  },
  "ts-node": {
    "transpileOnly": true,
    "swc": true,
    "esm": true
  }
}
  • Operating system and version: macOS 12.5
@thetutlage
Copy link
Contributor

Spent an hour debugging the same. Can we please look into this?

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