Using NodeJS Like A Boss

İbrahim Gündüz
Developer Space
Published in
2 min readAug 23, 2019

If you’re the one who doesn’t like contaminate the operating system with programming language packages, I’m sure you will love my solution.

Problem:

I’m working on in a nodeJS project and want to install dependencies, run or debug the code from my IDE. However, I want to install nothing related NodeJS on my computer.

Solution:

You can simply run NodeJS in a docker container and you don’t need to struggle any IDE plugin in order to debug or run your code or install any npm package.

Purpose:

- Keeping the host operating system clean. Also, This item includes avoiding installation of any virtualization mechanism based on the programming language.
- Getting rid of custom and complex IDE setups in order to debug the code in a container or installing dependent packages.

Potential Issues:

- Global dependencies.
- Having code dependencies out of container context.

However, it’s more than enough for any simple development environment that doesn’t have any relation to the issues above.

Let’s do it!

First, we need to create a script which will be represented the node binary on the local system. Create a file in /usr/local/bin/node with the following content.

#!/bin/bash
docker run \
— rm -i \
-u $(id -u):node \
-e HOME=$HOME \
-v ~/:$HOME \
— network host \
-w $(pwd) \
node:8.10 node $@

The script will be executed the node binary in a container and it’ll be accessing the files in mounted volume with the host operating system user’s rights. Additionally, we used the host network, in order to get rid of exposing ports one by one for every single need.

And… Test it!

$ node -v
v8.10.0

It works! Now we need to provide a way to run NPM in the local system. Basically, NPM is a javascript program and it uses the node binary the operating system in order to execute the code. In order to keep it simple, just copy the NPM code to the needed place from the docker image:

$ docker cp $(docker create node:8.10):/usr/local/lib/node_modules/npm /usr/local/lib/node_modules/.

Create a symbolic link for NPM script to /usr/local/bin from /usr/local/lib/node_modules/npm/bin/npm-cli.js.

$ sudo ln -sf /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm

Test it!

$ npm -v 
5.6.0

Currently, I use this setup with IntelliJ without any remote debug configuration and it worked smoothly so far.

Image resources:

https://blog.hasura.io/an-exhaustive-guide-to-writing-dockerfiles-for-node-js-web-apps-bbee6bd2f3c4/

https://thehackernews.com/2018/11/nodejs-event-stream-module.html

--

--