nx: @nrwl/node:lib won't resolve tsconfig path aliases
Current Behavior
I am using path aliases in tsconfig.json in my generated @nrwl/node:lib.
Let’s say tsconfig.json paths looks like this
"paths": { "@test": ["src/lib/index.ts"], }
And let’s say I use it like this in the code
import something from '@test'
Expected Behavior
When I build my library the code should look something like this
import something from '../../i/am/relative/path/to/the/right/place'
Instead it still looks like this
import something from '@test'
Steps to Reproduce
nx g @nrwl/node:lib name --buildable
Add path alias to tsconfig.json
nx build name
Compare imports from the source files and the dist.
Site note
This same configuration works for @nrwl/react:lib but not for this @nrwl/node:lib
Environment
$ nx report
> NX Report complete - copy this into the issue template
Node : 16.8.0
OS : darwin x64
yarn : 1.22.10
nx : 12.9.0
@nrwl/angular : Not Found
@nrwl/cli : 12.9.0
@nrwl/cypress : 12.9.0
@nrwl/devkit : 12.9.0
@nrwl/eslint-plugin-nx : 12.9.0
@nrwl/express : Not Found
@nrwl/jest : 12.9.0
@nrwl/linter : 12.9.0
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : 12.9.0
@nrwl/nx-cloud : Not Found
@nrwl/react : 12.9.0
@nrwl/schematics : Not Found
@nrwl/tao : 12.9.0
@nrwl/web : 12.9.0
@nrwl/workspace : 12.9.0
@nrwl/storybook : 12.9.0
@nrwl/gatsby : Not Found
typescript : 4.3.5
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 13
- Comments: 18 (3 by maintainers)
Sorry, my explanation is not very detailed, let me carefully list the questions in the above conversation:
@nrwl/node:builduses Webpack under the hood(as well as@nrwl/web:build), so it does understand path alias and replace@test/test.tswith./relative/path/test.ts.@nrwl/node:packageuses TypeScript compiler directly, so it’s not changing path alias(but it will include aliased path to build)You will need tsconfig-paths for executing built file with path mapping configurated.is that I thought you wants to execute built file but facing thecannot find module @tests/testerror.tsconfig-pathsworks at run-time intead of compile-time, it cannot solve your problem either.@nrwl/workspace:run-commandsand commands like['tsc --options', 'tscpaths --options']. Otherwise it’s getting more complicated, you need to copy source code of@nrwl/node:packageexecutor and add your own codes…, but I will do this thing soon in nx-plugin-workspace.My problem was transitioning from existing angular CLI project to nx. During that process nx renames:
tsconfig.jsonto ->tsconfig.base.jsonand that’s what vscode didn’t handle very well. Even after restarting TS server, changing paths, adjusting baseUrl, deep-diving into extending process, etc…
After investigating multiple approaches it turns out that while running
tsc --traceResolutionall modules and imports were resolved as expected. I renamed tsconfig.base.json to tsconfig.json and everything was working again @vsavkin is it a known issue?@vugar005 configuration is the right one, what’s maybe worth mentioning is that in tsconfig.json I set baseUrl to “.”
Works without any extra mapping configurations or packages. Also with es-lint.
Make sure you pass correct full path. // tsconfig.json in apps/mfe1 folder
Also,I discovered that thanks to comment. That even if your local tsconfig.json path is empty , it would just override tsconfig.base.json. // tsonfig.json in apps/mfe1 folder
So this is very important and I hope NX resolves it soon.
If you’re using nrwl/js:tsc, you can use typescript-transform-paths and pass it in
transformersparameter. E.g.project.jsonI’m facing a similar issue with my current setup:
The libs are being published on npm after the deployment under the names of
@my-org/my-clientand@my-org/common, while I’m defining the following path alias (ontsconfig.conf) to use them directly onmy-appcode:The issue is that
my-appis using an external packageanother-external-packagethat depends on@my-org/common(it’s importing with its published version).When I import
@my-org/commononmy-app, it seems that it’s picking up the peer dependency@my-org/common(fromanother-external-package) and not from the alias that is defined ontsconfig.conf. This happens only when we build for production but not in the dev environment.Any idea on how to tell nx/tsc to pick the library instead of the published package?
@linbudu599 @TheMeteoRain
I try to overwrite the
pathsinmy-lib/tsconfig.json:but I got an error
Cannot resolve module @/lib/test/index.tsxfrom compiler I don’t want to import modules from relative path e.g.../../../test. How do I overwrite the path alias in lib folder. However, if I add above totsconfg.base.json, it works.