Skip to content
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

tsconfig paths not working when building for production #986

Closed
bisonfoutu opened this issue Aug 20, 2018 · 17 comments
Closed

tsconfig paths not working when building for production #986

bisonfoutu opened this issue Aug 20, 2018 · 17 comments

Comments

@bisonfoutu
Copy link

bisonfoutu commented Aug 20, 2018

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 
@marcus-sa
Copy link

marcus-sa commented Aug 20, 2018

tsconfig-paths only works inline (during execution) and not when building.
I'd suggest using Webpack with awesome-typescript-loader and tsconfig-paths-webpack-plugin instead to build your application.

The example should be updated tho.

TypeStrong/ts-node#138 (comment)

@bisonfoutu
Copy link
Author

bisonfoutu commented 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 ?

@kamilmysliwiec
Copy link
Member

Thanks for sharing your solution @bushybuffalo. If you have some time, you can create a PR to the starter project :)

@locinus
Copy link

locinus commented May 17, 2019

@bisonfoutu : nice !
Just a note for those who read: when you paste the code from the link, DON'T copy the cleanup() call, cause it actually cancel the registration you just did.
Just somewhat not clear to have put it in the same block here, could cause one to look around for a while...

@m-ripper
Copy link

Hey for anybody who has this issue and could not fix it via the fix that @bisonfoutu posted.
If you are using TypeORM, make sure that your entities & subscribers are set correctly there.

I could not get the fix to work and saw that a subscriber.ts was loaded which of course throws an error when executed inside a js-environment. So what I did to get rid of the problem was setting the entities & subscribers-folders inside the ormconfig.js dynamically depending on the NODE_ENV.

example:

const entityPath = process.env.NODE_ENV === 'dev' ? `${__dirname}/src/**/*.entity.ts` : `${__dirname}/dist/**/*.entity.js`;

@lapwat
Copy link

lapwat commented 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 });

@psabeckis
Copy link

psabeckis commented Jul 8, 2019

Thank you for your ideas. I've managed to solve my problem in a bit more universal fashion.

Here's my tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": "src",
    "paths": {
      "@/*": ["./*"]
    },
    "incremental": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

And tsconfig-paths-bootstrap.js

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

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),
      ),
    }),
    {},
  ),
});

This just sets baseDir to be destination and replaces base dir with destination dir inside every path as well. Hope this helps someone.

@lapwat
Copy link

lapwat commented Jul 8, 2019

@psabeckis How do you handle the file extensions (ts VS js) ?

@psabeckis
Copy link

@lapwat I may not understand the problem you have, but the main problem I see here is that tsc does not support absolute imports this approach fixes that. You don't have to specify extensions in paths and since node is running the js files after build the only thing you need to worry about is the absokute path resolution. If you could provide a usecase I would be happy to help out if I can.

@lapwat
Copy link

lapwat commented Jul 9, 2019

@psabeckis
When building for development, node is looking for ts files in src folder.
When building for production, node should be looking for js files in dist folder. But it is actually looking for ts files because I have an "unexpected token {" error style.

That is why I have to change baseUrl to outDir but also I have to change js extensions to ts extensions.
I think we don't have the same issue.

@psabeckis
Copy link

Something is not right here as tsc only builds ts files and should do so without any changes to the config. tsconfig-paths/register is needed only for runtime for nodejs/nodemon. Judging by the example you provided earlier, could you ommit extensions from tsconfig path definitions and test this out? E.g replace @/**/*.ts to @/**/*.

@4F2E4A2E
Copy link
Contributor

I highly recommend module-alias

@yantrab
Copy link

yantrab commented Jul 19, 2019

@bisonfoutu @locinus @psabeckis Hi, someone can help me with this, can not get it to work.

source

@rubiin
Copy link
Contributor

rubiin commented Aug 29, 2019

Any updates on this one. Still facing issue

@rubiin
Copy link
Contributor

rubiin commented Aug 29, 2019

I highly recommend module-alias

Even that module doesnt work as promised

@rubiin
Copy link
Contributor

rubiin commented Sep 2, 2019

@kamilmysliwiec how about i update the starter project with paths included and send a PR

@lock
Copy link

lock bot commented Dec 1, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants