question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@nrwl/node:lib won't resolve tsconfig path aliases

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
linbudu599commented, Sep 27, 2021

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)
  • Why I’m saying 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 the cannot find module @tests/test error.
  • If you just want alias path to be replaced correctly in compiled file, you will need to handle it yourself for now, as tsconfig-paths works at run-time intead of compile-time, it cannot solve your problem either.
  • I’ve searched for and found something may fits you well: tscpaths, which works at compile-time and will change your alias path.
  • To intergate this tool with your workflow, I’d like to suggest that it your target lib has no workspace package dependencies, it’s ok to use @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.
4reactions
mmaterowskicommented, Apr 28, 2022

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 “.”

{
...
"baseUrl": "."
}

Works without any extra mapping configurations or packages. Also with es-lint.

Read more comments on GitHub >

github_iconTop 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 >

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