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
How can I debug the path resolution? #160
Comments
I'm having a similar issue. I am trying to debug in vscode. If I launch from there or attempt to run from the command line it does not resolve. I have the correct In my case, I am trying to import import {OrganizationService} from '/opt/nodejs/services/organization.service' tsconfig: {
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"/opt/nodejs/*": [ "api-layer/nodejs/*", "application-layer/nodejs/*", "domain-layer/nodejs/*", "data-layer/nodejs/*", "libraries/nodejs/*"]
},
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"allowJs": true
},
"include": ["utilities/**/*", "libraries/**/*", "api-layer/**/*", "domain-layer/**/*", "data-layer/**/*", "application-layer/**/*", "**/package.json", "handlers/**/*"],
"exclude": ["node_modules", "**/*.spec.ts", "**/*.test.ts"]
} launch.json: {
"type": "node",
"name": "Launch Create Organization",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"runtimeArgs": [
"-r",
"tsconfig-paths",
"-r",
"ts-node/register",
],
"args": [
"${workspaceFolder}/handlers/organization/create-organization/organization.ts"
],
"sourceMaps": true,
"smartStep": true,
"env": {
"NODE_ENV": "development"
}
} Running from the command line: $ node -r ts-node/register -r tsconfig-paths/register handlers/organization/create-organization/organization.ts
Error: Cannot find module '/opt/nodejs/services/organization.service'
Require stack:
- /Users/mtalbert/Projects/Vubi/vubi-api/handlers/organization/create-organization/organization.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._resolveFilename (/Users/mtalbert/Projects/Vubi/vubi-api/node_modules/tsconfig-paths/lib/register.js:75:40)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/Users/mtalbert/Projects/Vubi/vubi-api/handlers/organization/create-organization/organization.ts:6:1)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Module.m._compile (/Users/mtalbert/Projects/Vubi/vubi-api/node_modules/ts-node/src/index.ts:1056:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Object.require.extensions.<computed> [as .ts] (/Users/mtalbert/Projects/Vubi/vubi-api/node_modules/ts-node/src/index.ts:1059:12) |
Issue 128 looks like the culprit in my case. |
I discovered the issue for the vscode execution as well. I had missed the |
FWIW when I need to debug module resolution I manually edit // ...
// Patch node's module loading
// tslint:disable-next-line:no-require-imports variable-name
const Module = require("module");
const originalResolveFilename = Module._resolveFilename;
const coreModules = getCoreModules(Module.builtinModules);
// tslint:disable-next-line:no-any
Module._resolveFilename = function (request: string, _parent: any): string {
console.log(`Module._resolveFilename called`, request, _parent); // <----- added this line (can be pretty noisy)
const isCoreModule = coreModules.hasOwnProperty(request);
if (!isCoreModule) {
const found = matchPath(request);
if (found) {
const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
console.log(`Module._resolveFilename found match!`, found, modifiedArguments); // <----- added this line
// tslint:disable-next-line:no-invalid-this
return originalResolveFilename.apply(this, modifiedArguments);
}
}
// tslint:disable-next-line:no-invalid-this
return originalResolveFilename.apply(this, arguments);
};
// ... then I see output like this when running:
|
Relating to #128, you can change your paths key from Cheers |
I'm using ts-node with tsconfig-paths. My tsconfig.json file has a
baseUrl
andpaths
. My module resolution isn't working and I'm looking for a way that I can debug my setup. Are the options for tsconfig-paths to show which paths it is resolving to?I use a command-line like this:
Where there is a
paths
section in the tsconfig.ts fileAnd then I try to import via it:
The text was updated successfully, but these errors were encountered: