@nrwl/node:lib won't resolve tsconfig path aliases
See original GitHub issueCurrent 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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:14 (2 by maintainers)
Top Results From Across the Web
How to change the ts path aliases for an nx application
Nx should have created a tsconfig.base.json file in your workspace root where all the paths declarations for libs go. Option 1.
Read more >Typescript — How to solve the problem with unresolved path ...
The problem. Setting up path aliases in the tsconfig.json file is really a nice approach to making your imports look clear and tidy....
Read more >Typescript – How to solve the problem with unresolved path ...
The problem. Setting up path aliases in the tsconfig.json file is really a nice approach to making your imports look clear and tidy....
Read more >How to configure and resolve path alias with a Typescript Project
How to configure Typescript Path Alias. We can do this by adding the following lines to our tsconfig.json file. "compilerOptions": {.
Read more >How to handle tsconfig path aliases : r/typescript - Reddit
13 votes, 18 comments. I've hit the ageold "Failed to load module..." error, where it can't resolve my path alias of "@something".
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry, my explanation is not very detailed, let me carefully list the questions in the above conversation:
@nrwl/node:build
uses Webpack under the hood(as well as@nrwl/web:build
), so it does understand path alias and replace@test/test.ts
with./relative/path/test.ts
.@nrwl/node:package
uses 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/test
error.tsconfig-paths
works at run-time intead of compile-time, it cannot solve your problem either.@nrwl/workspace:run-commands
and commands like['tsc --options', 'tscpaths --options']
. Otherwise it’s getting more complicated, you need to copy source code of@nrwl/node:package
executor 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.json
to ->tsconfig.base.json
and 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 --traceResolution
all 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.