DEV Community

Cover image for How to debug Node.js in a Docker container

How to debug Node.js in a Docker container

Alex Barashkov on January 07, 2019

More and more teams are moving their development environments to Docker containers. It brings a lot of advantages, such as a unified environment sh...
Collapse
 
sirhennihau profile image
Sir hennihau

"start": "nodemon --inspect=0.0.0.0 source/index.ts",

throws

[nodemon] starting ts-node --inspect=0.0.0.0 source/index.ts
/app/node_modules/arg/index.js:90
throw err;
^

Error: Unknown or unexpected option: --inspect

any idea how to fix it?

Collapse
 
hypeofpipe profile image
Volodymyr V.

What's your version of nodemon?

Collapse
 
ariehkovler profile image
Arieh Kovler • Edited

Thanks for this helpful guide. I get that you're covering Open-source options here, but another option for getting code-level debug data out of a containerized Node.js app is Rookout.com. It has some interesting advantages over a traditional debugger - it's always on, even in production, and it doesn't stop the app so you can create 'breakpoints' that don't actually break, all without restarting or redeploying. It also works at scale - you don't have to attach it to a single instance in a single container, it can debug across a whole Swarm or Cluster all at once.

On the other hand, it doesn't allow variable forcing or logic forcing, which means it's more limited than a true debugger. Of course, this has some advantages too (safe use on live systems etc).

Have you encountered Rookout before?

Collapse
 
aghost7 profile image
Jonathan Boudreau

You can also start the inspector if the process is already running with:

kill -SIGUSR1 $pid

Where $pid is the process identifier of the program you want to debug.

Collapse
 
nimatrazmjo profile image
Nimatullah Razmjo

Dear Alx,

Your tutorial is great. Why did you add "/usr/src/app/node_modules" in volumes. What is the purpose of it? because "/usr/src/app" contain every thing that we need.

Collapse
 
alex_barashkov profile image
Alex Barashkov

Thank you. In you local machine in app folder you don't have node modules, so when you use mount your folder will override everything in /usr/src/app. The line you mentioned, sort of preventing overriding of node modules installed during the image build process.

Collapse
 
lfre profile image
Alfredo Lopez

You can do the same by adding node_modules to a .dockerignore file. 👍

Thread Thread
 
alex_barashkov profile image
Alex Barashkov

No. dockerignore works only for the process of copying file during the build of the image and has nothing to do with mount.

Collapse
 
gannochenko profile image
Sergei Gannochenko

Impressive, always wanted to "grow up" and stop using console.dir() while developing nodejs apps :)

Collapse
 
alex_barashkov profile image
Alex Barashkov

It's also much faster to debug functionality with debugger rather than using console.log or console.dir

Collapse
 
ramadanko profile image
Mohammed Ramadan

amazing, for the first part before configuring for VS code, will continue reading later :P

Collapse
 
shoks profile image
Boubacar Ndoye

Hi done it all and it dont wanna work

Collapse
 
pfrozi profile image
pfrozi

Thank you a lot Alex. You just save my day!