Installing npm without admin privileges: What's the trick?

Hey folks! I’m trying to set up npm without root access and I’m stuck. I followed the docs and made a .npmrc file with custom paths. The install went fine, but now node can’t find the npm packages.

I know I could use require.paths in node or set NODE_PATH, but I’m not sure what to set them to. When I installed as root before, everything just worked.

Has anyone done this successfully? What am I missing?

Here’s a quick example of what I’ve tried:

// My .npmrc file
prefix = /home/myuser/.npm-global

// In my terminal
export PATH=/home/myuser/.npm-global/bin:$PATH

// But when I try to use an npm package
const coolPackage = require('cool-package');
// Error: Cannot find module 'cool-package'

Any help would be awesome. Thanks!

I encountered a similar issue when setting up npm without admin rights. Here’s what resolved it for me:

Ensure your .npmrc file is correctly configured with the custom prefix. Then, add both the bin directory and the NODE_PATH to your shell configuration file (.bashrc or .zshrc):

export PATH=/home/myuser/.npm-global/bin:$PATH
export NODE_PATH=/home/myuser/.npm-global/lib/node_modules

After saving, run ‘source ~/.bashrc’ or restart your terminal.

If you’re still facing issues, try running ‘npm config list’ to verify your settings. Also, check if the packages are actually installed in the custom location.

Remember, this setup might need adjustments if you switch between different Node versions or use version managers like nvm.

oh man, been there! make sure u add this to ur .bashrc:

export NODE_PATH=/home/myuser/.npm-global/lib/node_modules

then run source ~/.bashrc. that fixed it 4 me. also double check ur using -g when installing global pkgs. good luck!

I’ve been in your shoes before, and it can be frustrating. Here’s what worked for me:

After setting up the custom prefix in .npmrc and updating PATH, I had to explicitly tell Node where to look for global packages. I added this to my .bashrc (or .zshrc if you use zsh):

export NODE_PATH=/home/myuser/.npm-global/lib/node_modules

Then I sourced the file or restarted my terminal. This made Node aware of the new global package location.

Also, double-check that you’re installing packages globally with the -g flag when needed. Sometimes we forget and wonder why local installs aren’t available globally.

Lastly, if you’re using nvm, make sure your NODE_PATH is set correctly for each version. It tripped me up once.

Hope this helps! Let me know if you need more details.