Arkadi
Arkadi

Reputation: 1357

VSCode debugger not working with src/ prefixed import paths

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?

Upvotes: 0

Views: 992

Answers (1)

mh377
mh377

Reputation: 1856

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

Upvotes: 2

Related Questions