tsconfig-paths: tsconfig-paths doesn't work with node (works with ts-node)

$ node -r tsconfig-paths/register dist/index.js
module.js:550
    throw err;
    ^

Error: Cannot find module '@modules/webhooks'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._resolveFilename (/home/niko/WebstormProjects/guild-review/node_modules/tsconfig-paths/lib/register.js:73:40)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/niko/WebstormProjects/guild-review/server/dist/index.js:5:20)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)

Repro: https://github.com/darkbasic/guild-review yarn && yarn workspace server build && cd server && node -r tsconfig-paths/register dist/index.js

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 26
  • Comments: 33

Commits related to this issue

Most upvoted comments

As a workaround, you can do node -r ts-node/register -r tsconfig-paths/register dist/index.js. @kel-sakal-biyik @darkbasic

May I ask for an update on this issue?

So I end up using the following:

"start": "node -r ts-node/register/transpile-only -r tsconfig-paths/register dist/main.js"

Hope it doesn’t have any negative impact.

I managed to make it work by using the TS_NODE_PROJECT env variable to point to a tweaked tsconfig file. In practice this means:

Launch script

TS_NODE_PROJECT=tsconfig.prod.json node -r tsconfig-paths/register dist/index.js

tsconfig.prod.json

{
  "extends": "./tsconfig.js",
  "compilerOptions": {
    "baseUrl": "dist"
  }
}

For some reason I could not override the compilerOptions.paths to rewrite them from the new baseUrl, but this particular setup seems OK.

So I end up using the following:

"start": "node -r ts-node/register/transpile-only -r tsconfig-paths/register dist/main.js"

Hope it doesn’t have any negative impact.

Right, but the particularity about this temporary workaround is that I need to move ts-node to dependencies section. For example to deploy my app into a Docker container.

Ideal solution (again) would be just run the transpiled code:

{
  "prod": "node -r tsconfig-paths/register dist/main.js"
}

I ended up going with a modified solution based on @mkalam-alami’s approach. The issue is that the tsconfig.json at the top level sets a baseUrl that makes sense for the top level but not the compiled js in your dist/ or build/. My work around was just to copy the top level tsconfig.json into the tsc output dir and use that for the TS_NODE_PROJECT variable. That way you preserve your defaults without any custom files, and because you moved the file to that directory, you change the relative position of your baseUrl so that it again makes sense to the rest of the files you’re executing. Example package.json below contrasting dev vs prod runs

  "scripts": {
    "build": "tsc && cp ./tsconfig.json ./build/",
    "start": "ts-node-dev -r tsconfig-paths/register src/index.ts",
    "start:production": "npm run build && TS_NODE_PROJECT=build/tsconfig.json node -r tsconfig-paths/register ./build/src/index.js",
  }

I have created a persistent Typescript paths replacer for those wants to replace TS path aliases directly. No runtime replace.

So the issue is, that the baseUrl will be resolved relative to tsconfig.json in root of your project, but in reality, it should point to your dist folder (or wherever your compiled files are). This package is working fine and it’s not actually a bug, but I think adding something like TS_PATHS_ROOT environment variable, that would allow people to override the root of baseUrl would be much appreciated (actually there’s already a PR for that - https://github.com/dividab/tsconfig-paths/pull/114)

So the node.js is trying to load your actual typescript source files. Using the -r ts-node/register/transpile-only workaround basically means you will compile your typescript files twice because you’ll be importing the typescript files (that use the paths) not the compiled javascript. It’s almost the same as running ts-node on your uncompiled index file.

One possible solution is to copy tsconfig.json to your dist path and set the current working directory (CWD) to said dist path when running the file. You have to set the CWD to dist because the tsconfig.json in CWD has the highest priority.

Another possible solution (and probably much cleaner) is to use mentioned https://github.com/ilearnio/module-alias, just keep in mind you have to point to your dist folder.

Example

Project structure

tsconfig.json          - original tsconfig
src/index.ts           - your sources
dist/index.js          - tsc output
dist/tsconfig.json     - tsconfig copy next to the index.js

then you can use vscode to debug run with following config:

{
	"type": "node",
	"request": "launch",
	"name": "Launch Program",
	"skipFiles": [
		"<node_internals>/**"
	],
	"cwd": "${workspaceFolder}/dist/",
	"runtimeArgs": ["-r", "tsconfig-paths/register"],
	"program": "${workspaceFolder}\\dist\\src\\index.js",
	"outFiles": [
		"${workspaceFolder}/dist/**/*.js"
	]
}

@bushybuffalo reported a fix here. I couldn’t get it to work with my project but our configuration is more complicated than the example. I went with @kel-sakal-biyik’s solution for now.

TS_NODE_PROJECT=build/tsconfig.json

@TSiege Your solution is the more elegant solution while PR #114 is approved. It’s works for me. This is my full config…

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./",
    "moduleResolution": "node",
    "baseUrl": ".",
    "paths": {
      "@server/*": ["./src/server/*"],
      "@globals/*": ["./src/globals/*"],
      "@plugins/*": ["./src/plugins/*"],
      "@modules/*": ["./src/modules/*"]
    },
    "esModuleInterop": true,
    "resolveJsonModule": true,
  },
  "exclude": ["node_modules", "dist", "tests"],
  "ts-node": {
    "require": ["tsconfig-paths/register"]
  }
}

Scripts on package.json. It’s going to build then run build

Windows user, remember using copy instead of cp command with \\ and not /

{
...,
"scripts": {
    "dev": "cross-env NODE_ENV=dev nodemon --exec ts-node ./src/server/app.ts",
    "build": "tsc && cp ./tsconfig.json ./dist/",
    "build:windows": "tsc && copy .\\tsconfig.json .\\dist\\",
    "start": "npm run build && cross-env TS_NODE_PROJECT=dist/tsconfig.json NODE_ENV=production node -r tsconfig-paths/register ./dist/src/server/app.js",
    "start:windows": "npm run build:windows && cross-env TS_NODE_PROJECT=dist/tsconfig.json NODE_ENV=production node -r tsconfig-paths/register ./dist/src/server/app.js"
  }
}

Thanks, @TSiege. Your solution is awesome. FYI. Considering the tsconfig.json at the paths section. If you define those absolute paths be like this

"paths": {
  "@/*": ["./src/*"]
}

you should set the outDir to be like “./dist/src” as well.

+1

So no solution for this yet basically, only workarounds for now ?

  • I wouldn’t want to compile again when running start after already doing it when building

All above may be not elegant or convenient, I won’t use these solutions because ROI is too low

Solution above didn’t work after deployment (probably more variables involved in building Docker image and environment variables etc.) So tried with similar to https://github.com/dividab/tsconfig-paths/issues/61#issuecomment-513642851 to actually have proper imports in the resulting *.js in “/dist” folder. But eventually ended with “module-alias” https://www.npmjs.com/package/module-alias as it is easy to use and just works.

With node node -r tsconfig-paths/register main.js

So this bit from the README should be removed to avoid confusion