Making Node.js Available to All Users With nvm
When trying to deploy a Node.js application to a remote server, how do we make it so that the user we've created (without sudo privilidges) to run/execute the application can see the necessary libraries needed to run our Node.js application? Read on and find out.
Join the DZone community and get the full member experience.
Join For FreeA not-uncommon situation happens when we try to deploy a Node.js application to a remote server, e.g. via Jenkins:
You set up the remote environment including all libraries needed such as the proper Node version, npm, etc. This is most likely done under a non-root user with sudo privileges. In this specific scenario, Node is installed via nvm.
You create a user to run/execute the application. This same user is unlikely to be on the sudoers list, and is a user that something like Jenkins can log in as.
You deploy and run your Node application.
Except when you try step 3, you generate an error message, effectively saying that the Node binary cannot be found.
... But we installed it in step 1!
Uh oh.
The first thought is to try installing Node for the user created in step 2. But, that would require giving said user sudo access, which is something you likely do not want to do.
So, how do we get around this? How do we make it so that the user from step 2 can see the necessary libraries needed to run your Node.js application?
There is a way to export the necessary paths for your user. Have a look at this (at least on Ubuntu):
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
Assuming you've kept the nvm and Node.js defaults, this command will put into the appropriate paths for all users the files necessary to run Node in any existing user.
Problem solved!
Have a look at this link for more information on how to set Node up in this kind of situation; indeed, it has additional tips for setting up on a VPS. That same link was also the one that provided that handy bit of code above, so I'm giving credit where it's due—it was a life saver!
Opinions expressed by DZone contributors are their own.
Comments