Supermon - Solving the infamous node_modules Docker problem



Node.js and Docker go together like peanut butter and jelly (if you are into such things). However,
that combination has a common problem that everyone eventually has to face which is that the volume
mapping from host also brings in node_modules/
that has code built specifically for the host OS.
This can be solved with internal node_modules/
volume but now you are left with the problem of
how do you sync any module installations done in the host machine to the internal volume within the
container?
Supermon
Supermon is nodemon replacement that, while
restarting your application on file changes it also monitors the package.json
and makes
sure that the modules that are mentioned there are installed to locally available
node_modules/
directory.
In practice, this means that when you run npm install awsomemodule
on your host machine,
supermon running in a docker container will install the same module on the internal
node_modules/
directory before restarting the application as usual. Such magic, much wow.
If you are running supermon normally, without any Docker shenanigans, it will do
the usual application restart on file changes since the module installation is only triggered
on the misalignment of package.json
and node_modules/
.
Also, it supports TypeScript out of the box via ts-node.
Install & usage
Install nodemon as usual
npm install supermon --save-dev
Add package.json
script that runs supermon with the application entry file
{
"scripts": {
"dev": "supermon app.js"
}
}
You can run the script in the container by overriding the command in docker-compose.yml
services:
app:
command: "npm run dev"
Feedback and bugs
I'm using supermon in my projects daily, however, it is still in its infancy and missing a lot of features so if you have any feedback or ideas, don't hesitate to create an issue
Happy coding!