0

I have Nest.js app with configured launch.json and when I start debugging, it fails with error saying that it can't find module, when import path starts with 'src/', for example:

This will fail with the error Error: Cannot find module 'src/user/module':

import { UserModule } from 'src/user/user.module';

@Module({
  imports: [
    ...
    UserModule,
  ]
})
export class AppModule {}

This will work:

import { UserModule } from './user/user.module';

@Module({
  imports: [
    ...
    UserModule,
  ]
})
export class AppModule {}

Here's my launch.json:

{
  "version": "0.2.0",
  "configureOptions": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Nest",
      "args": ["${workspaceFolder}/src/main.ts"],
      "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
      "sourceMaps": true,
      "cwd": "${workspaceRoot}",
      "protocol": "inspector",
      "console": "externalTerminal"
    }
  ]
}

Any ideas?

Arkadi
  • 1,153
  • 1
  • 14
  • 35

1 Answers1

1

In order to be able to debug the services in our project we needed to change a setting in VS Code:

Open Code -> Preferences -> Settings

Search for JavaScript: Auto Attach Filter and set it to Always

We did not need to change the launch.json

Also, In your jest properties which are probably located in the package.json (assuming you generated the nest project automatically). You will need to change the rootDir property:

from

rootDir: 'src',

to

rootDir: './',

This should allow it to work the full path instead of the relative path

see this post for further details

mh377
  • 1,656
  • 5
  • 22
  • 41
  • The above answer worked perfectly for me. This person is a genius. – edcincy Dec 24 '22 at 22:14
  • Also, in VSCode, you can force the QuickFix to give you relative paths by going into Settings and searching for "typescript". Then look for "Import Module Specifier" and set it to "relative". – edcincy Dec 24 '22 at 22:42