tsconfig-paths icon indicating copy to clipboard operation
tsconfig-paths copied to clipboard

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

Open darkbasic opened this issue 6 years ago • 34 comments

$ 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

darkbasic avatar Oct 23 '18 15:10 darkbasic

I have the same problem. I also tried to put tsconfig.json file under dist but didn't help.

kel-sakal-biyik avatar Oct 25 '18 12:10 kel-sakal-biyik

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

dotansimha avatar Oct 26 '18 08:10 dotansimha

@darkbasic I moved a tsconfig.json file under dist folder with only path configuration and run the index.js under dist folder. By doing so it read the tsconfig file under the dist folder and it worked. You can give it a try, it might help you too.

kel-sakal-biyik avatar Oct 26 '18 10:10 kel-sakal-biyik

Yep just tried it not working with node, and @dotansimha solution I believe it's not optimal for production environment.

chanlito avatar Oct 29 '18 14:10 chanlito

@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.

jelling avatar Oct 30 '18 00:10 jelling

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.

chanlito avatar Dec 02 '18 04:12 chanlito

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"
}

joseluisq avatar Jan 24 '19 11:01 joseluisq

May I ask for an update on this issue?

sandangel avatar Mar 20 '19 01:03 sandangel

+1

manan avatar Jul 21 '19 19:07 manan

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

joseluisq avatar Jul 22 '19 05:07 joseluisq

@chanlito Tried your solution, but I'm having the following error message.

EntityMetadataNotFound: No metadata for "SomeEntityClass" was found.

Any ideas?

jeffminsungkim avatar Oct 16 '19 04:10 jeffminsungkim

@jeffminsungkim are you using reflect-metadata? did you import or require it?

chanlito avatar Oct 16 '19 06:10 chanlito

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

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

keesvanlierop avatar Oct 16 '19 16:10 keesvanlierop

@chanlito I believe that the Nest.js framework uses reflect-metadata. So my answer is yes.

jeffminsungkim avatar Oct 17 '19 11:10 jeffminsungkim

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.

mkalam-alami avatar Mar 15 '20 08:03 mkalam-alami

Running into this issue as well. Seems like the common solution requires changing the baseUrl value so it'll work in prod. See #114

ejhayes avatar Apr 03 '20 06:04 ejhayes

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"
	]
}

SkaceKamen avatar May 03 '20 08:05 SkaceKamen

@ejhayes sorry, but can you explain what you mean by that?

I'm usually specifying '.' as the baseUrl and then I get the explained error. Right now, I'm sticking with the workaround from above (https://github.com/dividab/tsconfig-paths/issues/61#issuecomment-443481737)

baba43 avatar Oct 18 '20 10:10 baba43

For me all of the above didn't work. Also had to use json5, cause have comments in tsconfig.json. The solution from this post worked: https://github.com/nestjs/nest/issues/986#issuecomment-509295864

my "./tsconfig"

"outDir": "./dist",

"./tsconfig-paths-bootstrap.js" (also in the root of the project)

const json5 = require("json5");
const fs = require("fs");
const tsConfigPaths = require("tsconfig-paths");

const tsConfigPath = "./tsconfig.json";
const data = fs.readFileSync(tsConfigPath);
const tsConfig = json5.parse(data);

const paths = tsConfig.compilerOptions.paths;
tsConfigPaths.register({
  baseUrl: tsConfig.compilerOptions.outDir,
  paths: Object.keys(paths).reduce(
    (agg, key) => ({
      ...agg,
      [key]: paths[key].map(p =>
        p.replace(tsConfig.compilerOptions.baseUrl, tsConfig.compilerOptions.outDir),
      ),
    }),
    {},
  ),
});

DmytroSokhach avatar Oct 22 '20 18:10 DmytroSokhach

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.

DmytroSokhach avatar Oct 23 '20 11:10 DmytroSokhach

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",
  }

tsiege avatar Feb 04 '21 17:02 tsiege

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.

g-kawin avatar Feb 18 '21 11:02 g-kawin

Instead of

node ./dist/entry.js

use:

NODE_PATH=./dist node ./dist/entry.js

inspiration from: https://youtu.be/QVxxgEyZt9Y (TDD With TypeScript, Express, NodeJS, and Mocha Unit Tests)

vilmes21 avatar Jul 04 '21 11:07 vilmes21

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"
  }
}

ajparrah avatar Jul 19 '21 16:07 ajparrah

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

Ge-yuan-jun avatar Nov 17 '21 13:11 Ge-yuan-jun

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

This solved my problem

pieeee avatar Mar 02 '22 03:03 pieeee

You can now specify TS_NODE_BASEURL thanks to https://github.com/dividab/tsconfig-paths/pull/185 as follows:

TS_NODE_BASEURL=./dist node -r tsconfig-paths/register main.js

zingerj avatar Mar 10 '22 19:03 zingerj

Hi all, In my case, if I deleted the src folder, the issue related to alias path happens. Here is my folder structure image

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["ES2015", "DOM"],
    "paths": {
      "@models/*": ["src/models/*"],
      "@controllers/*": ["src/controllers/*"],
      "@routes/*": ["src/routes/*"],
      "@views/*": ["src/views/*"],
      "@utils/*": ["src/utils/*"],
      "@app_type": ["typing/app.type.ts"],
      "@config/*": ["config/*"],
      "@fixture": ["fixtureData/fixture.ts"],
      "@tests/*": ["tests/*"]
    },
    "module": "commonjs",
    "baseUrl": ".",
    "esModuleInterop": true,
    "noEmitOnError": true,
    "resolveJsonModule": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "typeRoots": ["typing", "./node_modules/@types"],
    "rootDirs": ["./src", "./config"],
    "outDir": "./dist",
    "sourceMap": true
  },
  "exclude": ["node_modules", "build_script.ts"]
}

scripts in package.json

     "clean": "npx rimraf ./dist/",
    "prebuild": "npm run clean",
    "build": "npx tsc -p . && npm run build:js",
    "postbuild": "npx ts-node ./build_script.ts",
    "format": "prettier --write ./src/**/*.ts ./tests/**/*.ts",
    "start": "npx cross-env NODE_ENV=production node -r ts-node/register/transpile-only -r tsconfig-paths/register ./dist/src/server.js",

I would like to know is there a way (even work-around) to fix this issue.

hungdao-testing avatar Jun 22 '22 11:06 hungdao-testing

After some confusion with a lot of approaches to fix. I was able to fix the issue just using the env var suggested in the documentation. I hope that will help others with the same problem. I believe this is not an issue anymore.

My tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./built",
    "allowJs": true,
    "target": "es6",
    "moduleResolution": "Node",
    "module": "CommonJS",
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "baseUrl": "./src",
    "paths": {
      "@config/*": ["./config/*"],
      "@controllers/*": ["./controllers/*"],
      "@models/*": ["./models/*"],
      "@repositories/*": ["./repositories/*"],
      "@services/*": ["./services/*"],
      "@tests/*": ["../tests/*"]
    }
  },
  "include": ["./src/**/*"]
}

My script to start the server:

"start": "tsc && TS_NODE_BASEURL=./built node -r tsconfig-paths/register ./built/app.js",

Current versions are:

  • Node: 16
  • tsconfig-paths: "^4.1.0",
  • typescript: "^4.7.4"

In case someone wanna confirm, that's the source code: https://github.com/rogeraraujo90/hey-freela

rogeraraujo90 avatar Aug 15 '22 18:08 rogeraraujo90

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

abdulrahimiliasu avatar Oct 14 '22 08:10 abdulrahimiliasu