ts-node
ts-node copied to clipboard
ERR_MODULE_NOT_FOUND error has undefined code
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: https://github.com/TypeStrong/ts-node/blob/8b09d49b26c1c25e34133b857c073729dd691f11/raw/node-internal-modules-esm-resolve-v15.3.0.js#L258-L259
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
Spent an hour debugging the same. Can we please look into this?