Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Making Node.js Available to All Users With nvm

DZone's Guide to

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.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

A not-uncommon situation happens when we try to deploy a Node.js application to a remote server, e.g. via Jenkins:

  1. 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.

  2. 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.

  3. 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!

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
node.js ,server ,users ,trick

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}