tsconfig paths not working when building for production

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When using Typescript paths in tsconfig, the project can be built with nodemon, but not with tsc (with npm run start:prod).

Expected behavior

I would expect the same code to work regardless its being built for development or production.

Minimal reproduction of the problem with instructions

  1. Create a repo with Nest CLI
  2. Add a path to tsconfig.json (for example @foo):
"paths": {
  "@foo/*": [
    "./*"
  ]
}
  1. Change an import in app.module.ts: import { AppController } from '@foo/app.controller';
  2. Run npm run start:prod

What is the motivation / use case for changing the behavior?

I had to remove every path alias in my code to get my project working for production. Since tsconfig paths are a default dependency of the project, and configured to work with nodemon, I believe it should also work when building for production.

Environment


Nest version: 5.1.0

 
For Tooling issues:
- Node version: 10.8.0  
- Platform: Windows 

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

54reactions
bisonfoutucommented, Aug 21, 2018

@marcus-sa According to the docs of tsconfig-paths it can be used when executing the built sources with node. The issue comment you are referencing also mentions execution with node.

Actually the problem occurs when executing the built files with node dist/main.js, not during the build process with tsc.

EDIT: I managed to get a working solution by following theses instructions from tsconfig-paths docs. I created an example repo here.

  1. Add a file named tsconfig-paths-bootstrap.js to the root of the project, and paste the code from the docs in it.
  2. Change the start:prod script to: node -r ./tsconfig-paths-bootstrap.js dist/main.js
  3. You should now be able to compile and execute the project with TS paths. (be aware that you will not be able to compile if there is no path set in tsconfig.json)

@kamilmysliwiec I believe this should be referenced in Nest README/docs, and maybe added to the CLI generator, and example project. What’s your opinion on this ?

6reactions
lapwatcommented, Jun 18, 2019

I had to replace ts extensions by js extensions in the paths.

const tsConfig = require("./tsconfig.json");
const tsConfigPaths = require("tsconfig-paths");

let { baseUrl, paths } = tsConfig.compilerOptions;
for (path in paths) {
  paths[path][0] = paths[path][0]
    .replace("src", "dist")
    .replace(".ts", ".js");
}

tsConfigPaths.register({ baseUrl, paths });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are these tsconfig paths not working? - Stack Overflow
I did a bit of digging and found that the tryPaths array produced by tsconfig-paths has absolute URLs relative to the project /cwd...
Read more >
Setting up Path Alias in TypeScript and tsc build without error
Third, go to tsconfig.json and add the paths alias options inside compilerOptions. ... Please be reminded that all paths in Paths are related...
Read more >
Why TypeScript Paths Failed Me | Mitchell Simoens Blog
Run a build, test the compiled code and you'll still see the same "Cannot find module" error. The reason for this is TypeScript...
Read more >
tsconfig-paths - npm
The typescript compiler can resolve these paths from tsconfig so it will compile OK. But if you then try to execute the compiled...
Read more >
TSConfig Reference - Docs on every TSConfig option
This does not affect how TypeScript emits JavaScript, it only emulates the assumption that they will be able to work via those relative...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found