Module alias not working with the new cli @nestjs/[email protected]
Adding alias in tsconfig.json is not working as espected. After build or start command the module resolution paths in the js files are replaced by a strange relative path pointing to src folder.
powershell...
"paths": {
"@/*": [
"src/*"
]
}
import { AppModule } from './app.module';
by
import { AppModule } from '@/app.module';
current path resolution: '../../../../ittempnestjs-demosrcapp.module'. in my case because project is in c:/git/temp/nestjs-demo folder.
Expected path resolution '.app.module' or simply keep path as '@/app.module' to use module-alias or another path resolution package.
using tsc keep module path to '@/app.module'.
Nest version: X.Y.Z
For Tooling issues:
- nest cli version: 6.13.1
- Node version: v12.14.1
- Platform: Windows 10
Others:
Thanks for reporting. Fixed in 6.13.2
@kamilmysliwiec unfortunately I'm still experiencing this in version 6.14.1.
Up to now I've built my project with tsc, and the compiled js in dist still used the aliased paths, and my main.ts requires tsconfig-paths/register to make them work.
Now I'm trying to use nest build -- the result is more or less the same, but the aliased imports are already expanded in the compiled files, which results in an import error when I try to run the application.
I think the cause of the problem is that I've provided two paths for each alias (one for the typescript files, one for the compiled js, since they're imported from another project). The optimal solution for me would be if nest build just left the aliased paths there, same as tsc -b does.
// Side note: I'm trying to use nest-cli only to be able to use the swagger plugin. If it's possible to use the plugin without using nest-cli for builds, I'd very much like to do so, if you could point me to some docs about this that I failed to find :)
npx nest info:
[System Information]
OS Version : Linux 5.4
NodeJS Version : v11.9.0
(node:1783722) [DEP0091] DeprecationWarning: crypto.DEFAULT_ENCODING is deprecated. // ???
NPM Version : 6.5.0
[Nest CLI]
Nest CLI Version : 6.14.1
[Nest Platform Information]
platform-express version : 6.11.4
microservices version : 6.11.4
swagger version : 4.2.3
testing version : 6.11.4
common version : 6.11.4
core version : 6.11.4
tsconfig.json excerpt:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist",
"paths": {
"@backend-common/*": [
"../backend-common/src/*",
"../backend-common/dist/src/*"
],
...
main.ts:
// Needs to be imported before anything with a @alias
import 'tsconfig-paths/register';
import { NestFactory } from '@nestjs/core';
import { ... } from '@backend-common/utils/asyncUtils';
// ...
main.js compiled with tsc -b
// ...
const asyncUtils_1 = require("@backend-common/utils/asyncUtils");
// ...
main.js compiled with nest build -c nest-cli.json -p tsconfig.json
// ...
const asyncUtils_1 = require("../../backend-common/dist/src/utils/asyncUtils");
// ...
Running the app -- both manually and with nest start -c nest-cli.json -p tsconfig.json
internal/modules/cjs/loader.js:611
throw err;
^
Error: Cannot find module '../../backend-common/src/utils/asyncUtils'
@quezak , Did you have any luck?
I'm experiencing the same issue with the same cause - I'm trying to use swagger plugin, which can be used only with nest build, which I can't use, because my shared lib is in another dir.
I'm working in a large multilingual monorepo with flat project-as-folder structure, and I extremely wouldn't want to to place all node.js projects into 1 folder
@vokilam-d no, I've abandoned the idea for now. While I very much like what the swagger plugin has to offer, I don't want to completely redo my app's structure and build process just for that, especially that nest-cli's building doesn't seem to handle all the custom options that I need.
https://medium.com/zero-equals-false/how-to-use-module-path-aliases-in-visual-studio-typescript-and-javascript-e7851df8eeaa
@vokilam-d @quezak here you go, worked for me
Most helpful comment
@kamilmysliwiec unfortunately I'm still experiencing this in version 6.14.1.
Up to now I've built my project with
tsc, and the compiled js indiststill used the aliased paths, and mymain.tsrequirestsconfig-paths/registerto make them work.Now I'm trying to use
nest build-- the result is more or less the same, but the aliased imports are already expanded in the compiled files, which results in an import error when I try to run the application.I think the cause of the problem is that I've provided two paths for each alias (one for the typescript files, one for the compiled js, since they're imported from another project). The optimal solution for me would be if
nest buildjust left the aliased paths there, same astsc -bdoes.// Side note: I'm trying to use nest-cli only to be able to use the swagger plugin. If it's possible to use the plugin without using nest-cli for builds, I'd very much like to do so, if you could point me to some docs about this that I failed to find :)
npx nest info:tsconfig.jsonexcerpt:main.ts:main.jscompiled withtsc -bmain.jscompiled withnest build -c nest-cli.json -p tsconfig.jsonRunning the app -- both manually and with
nest start -c nest-cli.json -p tsconfig.json