Nest: VSCode debug: can not find module

Created on 17 Jul 2018  ·  10Comments  ·  Source: nestjs/nest

npm run start is ok and can also run the project。 but when i debug the error console show me can not find module。

Note:if i use import {AppModule} from './app.module' it will be ok. but when import {AppModule} from 'app.module'(just like the blow pictures). it will tell me that cannot find the app.module.

image

image

this is my lanch.json

{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "启动程序",
"protocol": "inspector",
"sourceMaps": true,
"stopOnEntry": true,
"console": "internalConsole",
"program": "${workspaceFolder}\src\main.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/dist/*/.js"
]
}
]
}

Most helpful comment

I think this is NestJs related. It'll be good if there is some default configuration to debug NestJs in documentation, in tests context and app context. tsconfig-paths already comes by default in start:dev script. But, then if you use npm run start:prod, problems will appear, the user manually has to configure the 'tsconfig` to has the expected behavior of nest. i think it just need to define a default behavior to resolve these problems.

All 10 comments

@FourLeafClover this issue has nothing to do with Nest.js, you should check on typescript config paths & module resolution.

@chanlito I know。 but I used the default tsconfig.json of nestjs。so, i don't konw how to config my tsconfig.json to solve this issue ,can you help me? thanks? this is my tsconfig.json.
{
"compilerOptions": {
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"allowJs": true,
"outDir": "./dist",
"baseUrl": "./src"
},
"include": [
"src//"
],
"exclude": [
"node_modules",
"
/.spec.ts"
]
}

Why not just use import { AppModule } from './app.module';? This is the common / recommended way of importing internal modules

@BrunnerLivio because some of our co-workers used IntelliJ idea, they used import like this ‘app.modules’, 'services/???.service.ts' and 'controller/???.controller.ts'。 I alse think this way import is good。use relative path to import is difficult to read。Just like vue-cli, also import file like @/path(not '../../)

@FourLeafClover
Can you close this issue? This is not Nest related.


In general you use inside the application relative imports e.g. import * from './app.module'. In TypeScript, if you use non-relative imports e.g. import * from 'jquery', it looks up inside your node_modules folder, so you usually just use non-relative imports for dependencies.

The approach from frameworks like nest, angular or vue with the @-prefix is a different story. @-prefix means the packages are scoped. Read more here. I would only use this, if I want to make complicated modular software. It is quite hard to setup the architecture for scoped packages.

But I think your coworkers used path aliases: If you do not like to write a lot of ../../ you can still use TypeScript path aliases. So you can convert e.g. ../../../../lib/some-module to @lib/some-module

Also check out the TypeScript module resolution

If you want to use import like you showed you can use https://www.npmjs.com/package/tsconfig-paths. Just install package and change start script by registering it: -r tsconfig-paths/register

I think this is NestJs related. It'll be good if there is some default configuration to debug NestJs in documentation, in tests context and app context. tsconfig-paths already comes by default in start:dev script. But, then if you use npm run start:prod, problems will appear, the user manually has to configure the 'tsconfig` to has the expected behavior of nest. i think it just need to define a default behavior to resolve these problems.

@BrunnerLivio OK, I closed this issue. Thanks

I know the issue is closed, but wanted to share my approach:

npm install --save module-alias

In main.ts, line 1:

import 'module-alias/register'

tsconfig.json:

    "outDir": "./dist",
    "baseUrl": "./src",
    "paths": {
      "~/*": ["*"]
    }

package.json:

  "_moduleAliases": {
    "~": "./dist"
  },

Just make sure the path in package.json is the same as outDir in tsconfig.json (NOT paths), so the paths are resolved correctly in production.

EDIT:
Okay, just realised you asked for something a little bit different, but you could try "*": ["*"] in tsconfig.json and check if the same or similar would work in package.json, or use module-alias package to add the alias programmaticaly. I would still recommend using some @/ or ~/ sort of alias.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anyx picture anyx  ·  3Comments

thohoh picture thohoh  ·  3Comments

rlesniak picture rlesniak  ·  3Comments

mishelashala picture mishelashala  ·  3Comments

rafal-rudnicki picture rafal-rudnicki  ·  3Comments