4

Problem

I'm working with environment variables on a NodeJS project using VSCode.
I want to optimize my workflow using .vscode/launch.json with a Launch via NPM configuration.
I firstly followed this article which explained which configuration to choose.

Here is my actual configuration which doesn't work :

.
├── .vscode
│   └── launch.json
├── .env
├── server.js
├── package.json
// other stuff…

The launch.json file

"configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch via NPM",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "start"
      ],
      "envFile": "${workspaceFolder}/.env"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}/server.js",
      "envFile": "${workspaceFolder}/.env"
    }
]

The .env file

NODE_ENV='dev'

The server.js file

// some stuff
console.log(process.env.NODE_ENV)

The package.json file

{
  "name": "server Node",
  "version": "0.1.0",
  "main": "server.js",
  "scripts": {
    "start": "node --inspect server.js"
  },
// dependencies and more
}
}

When I run npm start I have an Undefined output on the terminal.

Other attempts

I read the nodejs debugging documentation on VSCode website, so

  • I tried to add "protocol"="inspector" in my launch.json configuration
  • I also tried to add port=9229 with --inspect-brk=9229 on my package.json start script which end up with no output at all on the terminal neither on the output or the debug console. (Can't understand why again…)

And finally I read a few stackoverflow questions with no additional information. (this one, this one and this one)
I'm lost, I really can't find why my configuration doesn't work.

look forward to seeing your next answers and finally understand what's wrong with the configuration I have.

When I launch the debugger from VSCode, the terminal outputs dev. So it isn't a path or deeper configuration problem. Well, I think…

Environment

VS Code : Version 1.30.2
node : 11.6.0

Community
  • 1
  • 1
Tenshock
  • 107
  • 10
  • I didn't have any luck using "launch" for debugging a running Express JS app. It only worked for me for running files. However, this worked for me for a running app, using "attach" instead of "launch": https://stackoverflow.com/questions/37321872/debug-expressjs-server-side-code-using-visual-studio-code – Joe Flack Sep 14 '21 at 22:17

0 Answers0