Xcode cloud suddenly failing to link node and install dependencies - node.js

I've been trying to figure out why this xcode cloud custom scripts suddenly started failing on me yesterday since I haven't changed anything and it was working before:
#!/bin/sh
export HOMEBREW_NO_INSTALL_CLEANUP=TRUE
brew install cocoapods
# have to add node yourself
brew install node#16
# link it to the path
brew link node#16
brew install yarn
# Install dependencies you manage with CocoaPods.
yarn
pod install
# the sed command from RN cant find the file... so we have to run it ourselves
sed -i -e $'s/ && (__IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0)//' /Volumes/workspace/repository/ios/Pods/RCT-Folly/folly/portability/Time.h
For some reason it has started failing with the following errors:
==> Installing node#16 dependency: brotli
==> Pouring brotli--1.0.9.monterey.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/brotli
/usr/local/include is not writable.
You can try again using:
brew link brotli
Then it keeps giving me same errors that it could not link for a few other dependencies.
When it tries to run yarn I get this:
yarn requires a Node installation to function. You can install one with:
brew install node
Assuming the node installation didn't work successfully.
Then finally this:
[in /Volumes/workspace/repository/ios]
[!] Invalid `Podfile` file: cannot load such file -- /Volumes/workspace/repository/node_modules/react-native/scripts/react_native_pods.
# from /Volumes/workspace/repository/ios/Podfile:1
# -------------------------------------------
> require_relative '../node_modules/react-native/scripts/react_native_pods'
# require_relative '../node_modules/#react-native-community/cli-platform-ios/native_modules'
# -------------------------------------------
sed: /Volumes/workspace/repository/ios/Pods/RCT-Folly/folly/portability/Time.h: No such file or directory
EDIT
Seems like I'm not the only one experiencing this:
https://developer.apple.com/forums/thread/712550
It seems to be an issue with the platform and not by my config apparently.

Changing the macOS version (in Workflow environment settings) from "Latest release" to 12.4 could help.

I managed to find a workaround by installing node from the .tar.gz from node website:
NODE_VER=16
VERSION=$(curl -s https://nodejs.org/dist/latest-v$NODE_VER.x/ | sed -nE 's|.*>node-(.*)\.pkg</a>.*|\1|p')
if [[ "$(arch)" == "arm64" ]]
then
ARCH="arm64"
else
ARCH="x64"
fi
curl "https://nodejs.org/dist/latest-v$NODE_VER.x/node-$VERSION-darwin-$ARCH.tar.gz" -o $HOME/Downloads/node.tar.gz
tar -xf "$HOME/Downloads/node.tar.gz"
NODE_PATH="$PWD/node-$VERSION-darwin-$ARCH/bin"
PATH+=":$NODE_PATH"
export PATH
node -v
npm -v
also: If you need node to build a react native app you need to generate ios/.xcode.env.local so it exports $NODE_BINARY:
echo "export NODE_BINARY=$(which node)" > ../.xcode.env.local
Good luck!

Related

Deleted nvm now npm is not working

I installed nvm to update my node but then decided to uninstall. After updating my node via website, I am not able to get npm installed despite installing a new version of node and using:
sudo curl -L npmjs.org/install.sh | sudo sh
Here is the message I am getting:
-bash: /Users/SICNARF/.nvm/versions/node/v8.2.1/bin/npm: No such file or directory
I think I just have to change the command from non-existing nvm directory to (wherever its supposed to run).
Node.js version management: no subshells, no profile setup, no convoluted API, just simple.
Installation
npm install -g n
Example
$ n 8.2.1
https://www.npmjs.com/package/n

env: node: No such file or directory in mac

I'm searching it in google on how to show the version of node js or how I'm gonna work with npm?
If I do this
npm -v
even I already install it using brew install node. The result is always
env: node: No such file or directory
I already do a lot of command to solve for it, but failed.
these are the command that I already try:
sudo apt-get install nodejs-legacy
sudo ln -s /usr/local/bin/node /usr/bin/node
brew unlink node
brew link node
I can't start the task because of this. help me guys. This work before after I used this command brew link --overwrite node. Now I have problem, i cant work anymore with npm. I'm really new about this. I don't have any idea now on how to fix this.
Update
I already get the list using npm list command. I also get the idea here link but when I npm install inside the project. I alwayst get this "result env: node: No such file or directory",even I command npm -v but If i command it outside my project. It work the npm -v
I solved it this way:
$ brew uninstall --force node
$ brew uninstall --force npm
after it
$ brew install node
which suggested me to overwrite simlinks
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/doc/node/gdbinit
Target /usr/local/share/doc/node/gdbinit
already exists. You may want to remove it:
rm '/usr/local/share/doc/node/gdbinit'
To force the link and overwrite all conflicting files:
brew link --overwrite node
after executing
$ brew link --overwrite node
everything worked again.
NOTE: Only mac users!
uninstall node completely with the commands
curl -ksO https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh
chmod +x ./uninstall-node.sh
./uninstall-node.sh
rm uninstall-node.sh
Or you could check out this website: How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X)
if this doesn't work, you need to remove node via control panel or any other method. As long as it gets removed.
Install node via this website: https://nodejs.org/en/download/
If you use nvm, you can use:
nvm install node
You can already check if it works, then you don't need to take the following steps with: npm -v and then node -v
if you have nvm installed:
command -v nvm
Uninstall npm using the following command:
sudo npm uninstall npm -g
Or, if that fails, get the npm source code, and do:
sudo make uninstall
If you have nvm installed, then use: nvm uninstall npm
Install npm using the following command: npm install -g grunt
I was getting this env: node: No such file or directory error when running the job through Jenkins.
What I did to fix it - added export PATH="$PATH:"/usr/local/bin/ at the beginning of the script that Jenkins job executes.
If you're switching from bash to oh my zsh, you will need to add nvm's path into the zshrc file.
#Zgpeace's answer here helped solve my issue (on IOS):
Open the .zshrc file. I used nano: nano ~/.zshrc
Add this into the file: export NVM_DIR=~/.nvm
Save changes by typing Ctrl+X --> Yes.
Reload the configuration. Type source $(brew --prefix nvm)/nvm.sh
Test by typing nvm --version.
You'll now be able to see the version. For e.g: 0.37.2
Thanks to this forum.
Sharing my notes below.
macOS Monterey
version 12.2.1
Chip Apple M1
$ brew uninstall --force node
$ brew uninstall --force npm
$ brew install node
$ brew link --overwrite node
$ brew doctor
$ brew cleanup
$ nvm --version
// 0.35.3
$ nvm use stable
// Now using node v17.6.0 (npm v8.5.1)
$ node -v
// v17.6.0
$ npm -v
// 8.5.1
I got such a problem after I upgraded my node version with brew. To fix the problem
1)run $brew doctor to check out if it is successfully installed or not
2) In case you missed clearing any node-related file before, such error log might pop up:
Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built.
node
3) Now you are recommended to run brew link command to delete the original node-related files and overwrite new files - $ brew link node.
And that's it - everything works again !!!
It was nvm changing paths for me, this fixed it:
nvm use stable
I re-installed node through this link and it fixed it.
I think the issue was that I somehow got node to be in my /usr/bin instead of /usr/local/bin.
Let's see, I sorted that on a different way. in my case I had as path something like ~/.local/bin which seems that it is not the way it wants.
Try to use the full path, like /Users/tobias/.local/bin, I mean, change the PATH variable from ~/.local/bin to /Users/tobias/.local/bin or $HOME/.local/bin .
Now it works. 🙀🙀🙀
I get this error running npm start through PyCharm on Mac. For that the answer is to start PyCharm from the Terminal with open -a '/Applications/PyCharm.app/'. See https://stackoverflow.com/a/34017083/733092.
For Pycharm Professional, starting from the command line is different. Get the JetBrains Toolbox app, go to Settings, Generate Shell Script, and it will create a ~/pycharm_shell_scripts for you to run.
Working on MacOSx, maven exec not willing to execute a shell script that I could manually execute with desired result. Gave me the same error, solved it as well with
export PATH="$PATH:"/usr/local/bin/ at the beginning of the script
For mac user using the package manager homebrew:
-brew uninstall node
-brew uninstall npm
-brew doctor
-brew link node
-node -v (to check node version)
That's all you need.
For me, this error occurred when I tried to install a package with sudo npm. This fixed it:
sudo env PATH="$PATH:/usr/local/bin/" npm <rest of command>
I was using fish shell and encountered this problem on my m1 macbook air.
install nvm omf install nvm
add nvm to path by creating this file ~/.config/fish/functions/nvm.fish
I use vim so vim ~/.config/fish/functions/nvm.fish
function nvm
bass source ~/.nvm/nvm.sh --no-use ';' nvm $argv
end
install node again nvm install node
It worked for me after that, :) hopefully it does for you too
credit https://eshlox.net/2019/01/27/how-to-use-nvm-with-fish-shell
If you just installed/modified node and if intellij/webstorm or your shell is throwing this error, it's because it has not sourced the environment variables, try restarting the IDE/shell.
I also face this issue on mac. I had installed node using brew. I was able to resolve the issue by following these steps:
uninstall node using the command: brew uninstall --force node
uninstall npm using the command: brew uninstall --force npm
Then install node using the installer provided on the following link: https://nodejs.org/en/download/
Just did an update on my Mac OS which switched my .bash to /zshrc. Here is what I added to my ~/.zshrc file:
export NVM_DIR="/Users/< my name >/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # this loads nvm
Then I did a $ source ~/.zshrc and it refreshed those values.
Double checked my work with nvm --version and it was all good.
just run cmd:
% npm
before
% npx install-peerdeps --dev eslint-config-airbnb
on Mac m1 pro
Kept getting this error, found it it's because NVM wasn't using any version of node. I didn't set this but after using these commands it seemed to fix the error
nvm install lts/*
nvm use lts/*

Customize AWS ElasticBeanstalk NodeJS Install (use yarn)

Isit possible to configure EBS to install my NodeJS application using yarn package manager instead of NPM?
I've figured out a way, but it is a little hacky.
Create a .ebextensions/yarn.config file. (The name does not have to be 'yarn'.)
Put this content into the file:
files:
# Runs right before `npm install` in '.../50npm.sh'
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
#!/bin/bash
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
# install node
curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -;
# install yarn
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo;
yum -y install yarn;
# install node_modules with yarn
cd "${app}";
yarn --production;
This ebextension creates a file which does 3 things:
Installs node.
Installs yarn.
Installs node_modules with yarn.
In order to make Elastic Beanstalk run yarn install before it runs npm install, the file is created under /opt/elasticbeanstalk/hooks/appdeploy/pre. This turns the file into a pre-deployment hook, which means that Elastic Beanstalk will run it during the first phase of deployment. By default, there is another file in this directory called 50npm.sh, which runs npm install. Since Elastic Beanstalk runs the files in this directory alphabetically, 49yarn.sh (our file) will run before 50npm.sh (the default file), resulting in yarn install running before npm install.
One potential problem is that the environment variables set in the Elastic Beanstalk UI (under Configuration > Software Configuration) are not available at this point of the deployment phase. This is a big problem if you have an npm auth token there which you use to install private npm modules.
Another potential problem is that this installs node manually, so the "Node version" you specify in the Elastic Beanstalk UI (under Configuration > Software Configuration) will have no effect on the version of node your application uses; you need to specify it in this ebextension. Elastic Beanstalk's 50npm.sh both installs node and runs npm install. Since we have to run yarn install before that file runs, we also have to install node manually. Then, when Elastic Beanstalk goes to install node, it detects that node is already installed but does not verify that it is the correct version, so it skips the node installation.
For reference, the yarn installation instructions came from here: https://yarnpkg.com/docs/install#linux-tab
I did this following instructions on https://yarnpkg.com/lang/en/docs/install/
commands:
01_install_yarn:
command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - && sudo yum install yarn -y"
This way that i came up with lets you still control the node version via the Elastic Beanstalks Dashboard.
Thanks for this question! couldn't have come to this solution without it :D
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script
# (https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663).
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script.
# But their default script actually doesn't work at all, since the app
# staging dir, where they try to run `npm install`, doesn't exist during
# config deploys, so ebnode.py just aborts:
# https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L140
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
tmp="$(mktemp || bail)";
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
version="$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)";
echo $version
major="$(cut -d'.' -f1 <<<${version})"
yum -y install python26 python26-libs
wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo;
wget "https://rpm.nodesource.com/pub_${major}.x/el/7/x86_64/nodejs-${version}-1nodesource.x86_64.rpm" -O "${tmp}";
rpm -i --nosignature --force "${tmp}";
rm -f "${tmp}";
yum -y install yarn;
cd "${app}";
yarn --production;
Had to revisit this as we couldn't figure out why we were stuck on node 8 even though we set it to node 12 in the EB UI. Seems that if you install a global node it overrides the version setting. Instead of installing a global node, this uses the Elastic Beanstalk node install and adds it to the path. You have to add the PATH in again at the start of your yarn install script but it seems to be the least invasive way to use yarn.
content: |
#!/usr/bin/env bash
set -euxo pipefail
EB_NODE_VERSION=$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)
echo "EB node version: $(EB_NODE_VERSION)"
# Make sure Node binaries can be found (required to run npm).
# And this lets us invoke npm more simply too.
export PATH=/opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-x64/bin:$PATH
if yarn -v; then
echo 'Yarn already installed.'
else
echo 'Installing yarn...'
npm install yarn -g
fi
An easy way to prevent EB from running npm install is to create an empty node_modules folder in a prebuild hook:
Edit your_project/.platform/hooks/prebuild/prevent-npm.sh:
#!/bin/bash
# EB build scripts will not install using npm if node_modules folder exists
mkdir node_modules
This way, EB will still install Node.js, so you don't have to install that yourself. But it will skip npm install when node_modules exists.
Then inside the predeploy hook, you can run yarn. If you are running Node.js 16 or newer, you can use corepack yarn, and it will just work (you don't have to install yarn from yum, npm or anything else.)
Edit your_project/.platform/hooks/predeploy/yarn.sh:
#!/bin/bash
corepack yarn
Be sure to also create symlinks under confighooks in your project, or else it will fail to build when changing config (e.g. updating environment values):
mkdir -p .platform/confighooks/{prebuild,predeploy}
ln -s ../../hooks/predeploy/yarn.sh .platform/confighooks/predeploy/yarn.sh
ln -s ../../hooks/prebuild/prevent-npm.sh .platform/confighooks/prebuild/prevent-npm.sh
Since get-config is no longer present in the new Amazon Linux 2 platform, we had to figure another clean way to do this, and came up with the following :
container_commands:
01_npm_install_yarn:
command: "npm install -g yarn"
10_yarn_install:
command: 'PATH="$PATH:$(dirname $(readlink $(which node)))" yarn install'
You may want to put the PATH= logic in a script and call it before every yarn command, to have clean command: instructions in your extentions.
Also, note that if you install yarn using the yum package manager, you completely break the NodeJS version management provided by Beanstalk (since it the black magic running behind make some symlinks in /bin and /usr/bin).

NPM and NODE command not found when using NVM

Seen other questions, but I think they are not my case.
I think the problem is over here, but I don't know how to solve it:
I do have latest Node version installed (I followed the official github page instructions)
> nvm install v7.3.0
v7.3.0 is already installed.
Now using node v7.3.0
I check node and npm versions installed
> which node
~/.nvm/versions/node/v7.3.0/bin/node
> which npm
~/.nvm/versions/node/v7.3.0/bin/npm
I check the PATH is right and it actually is
> echo $PATH
~/.nvm/versions/node/v7.3.0/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
But it still fails and when I browse through nvm folders... I find this, which I don't know how to change or solve:
> ls -a .nvm/versions/node
.
..
.DS_Store
v6.4.0 // WTF???
And I promise I didn't do anything, I mean... this is a clean install, .nvm folder didn't exist before installing nvm.
If you want to install the version you want in the place you want then you can follow my tutorial here on GitHub:
https://gist.github.com/rsp/edf756a05b10f25ee305cc98a161876a
It's about version 6.7.0 but you can change it to any other version. It show you how to install either from source or from binary packages and following that tutorial you will always know which version is where, because you have full control over the installation instead of relying on tools that do that automatically for you.
If you want to have Node 7.3.0 in /usr/local for example the it is just:
wget https://nodejs.org/dist/v7.3.0/node-v7.3.0.tar.gz
tar xzvf node-v7.3.0.tar.gz
cd node-v7.3.0
./configure --prefix=/usr/local
make && make test && echo OK || echo ERROR
sudo make install
Edit .bash_profile using the below command.
nano .bash_profile
And add the following lines to .bash_profile
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
Save it. Exit the terminal and check the magic.

Install NPM into home directory with distribution nodejs package (Ubuntu)

I'd like to use the distribution Node.js packages (or the chris-lea ppa for more recent releases) but install NPM to my home directory.
This may seem picky, but it's a pretty idiomatic way for polyglot/github-using developers to setup language runtime/library environments under Linux: distro packages for the runtime, 3rd-party libraries in per-user environment (see virtualenv, RVM - RVM will also build Ruby for you if you want). If necessary I will build node locally but it's a PITA since Node is becoming an incidental development requirement for lots of projects.
Instructions for installing node+npm to home directory
NPM will install local packages into your projects already, but I still like to keep the system away from my operating system's files. Here's how I suggest compartmentalizing Nodejs packages:
Install Nodejs and NPM via the chris-lea PPA. Then I set up a package root in my homedir to hold the Node "global" packages:
$ NPM_PACKAGES="$HOME/.npm-packages"
$ mkdir -p "$NPM_PACKAGES"
Set NPM to use this directory for its global package installs:
$ echo "prefix = $NPM_PACKAGES" >> ~/.npmrc
Configure your PATH and MANPATH to see commands in your $NPM_PACKAGES prefix by adding the following to your .zshrc/.bashrc:
# NPM packages in homedir
NPM_PACKAGES="$HOME/.npm-packages"
# Tell our environment about user-installed node tools
PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath` command
unset MANPATH # delete if you already modified MANPATH elsewhere in your configuration
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
# Tell Node about these packages
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
Now when you do an npm install -g, NPM will install the libraries into ~/.npm-packages/lib/node_modules, and link executable tools into ~/.npm-packages/bin, which is in your PATH.
Just use npm install -g as you would normally:
[justjake#marathon:~] $ npm install -g coffee-script
... (npm downloads stuff) ...
/home/justjake/.npm-packages/bin/coffee -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/coffee
/home/justjake/.npm-packages/bin/cake -> /home/justjake/.npm-packages/lib/node_modules/coffee-script/bin/cake
coffee-script#1.3.3 /home/justjake/.npm-packages/lib/node_modules/coffee-script
[justjake#marathon:~] $ which coffee
/home/justjake/.npm-packages/bin/coffee
Jake's answer was posted in 2012 and while useful it references Chris Lea's Node.js PPAs who are no longer updated since march 2015.
Here's the steps I use to install Node.js and npm in my home directory:
Install Node.js with nvm (no sudo required):
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
nvm install 7
npm install -g npm # update npm
Now you can install -g without sudo and everything goes into ~/.nvm/
Or install Node.js without nvm (official instructions):
Install Node.js
Node.js v6 (current LTS as of May 2017):
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
Node.js v7:
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs
Change npm's default directory to a local one:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH="$HOME/.npm-global/bin:$PATH" # ← put this line in .bashrc
source ~/.bashrc # if you only updated .bashrc
Alternatively replace .npm-global by the directory of your choice.
Update npm and check it is installed in your $HOME directory:
$ npm install npm -g
/home/<username>/.npm-global/bin/npm -> /home/<username>/.npm-global/lib/node_modules/npm/bin/npm-cli.js
/home/<username>/.npm-global/lib
└─┬ npm#3.10.6
├─┬ glob#7.0.5
│ └── minimatch#3.0.2
├── npm-user-validate#0.1.5
└── rimraf#2.5.3
Now you can install -g without sudo and without messing with your system files.
The solution posted by Just Jake is great. However, due to a bug with npm > 1.4.10, it may not work as expected. (See this and this)
While the bug is solved, you can downgrade to npm 1.4.10 by following this steps:
Comment the prefix line in your $HOME/.npmrc
Run sudo npm install -g npm#1.4.10
Ensure that the right version of npm is installed (npm --version)
Uncomment the prefix line in your $HOME/.npmrc
Proceed to install your global packages in your home folder!.
Because python does already a great job virtualenv, I use nodeenv. Compared to nvm, you can create multiple environments for the same node version (e.g. two environments for node 0.10 but with different sets of packages).
ENVNAME=dev1
# create an environment
python -m virtualenv ${ENVNAME}
# switch to the newly created env
source ${ENVNAME}/bin/activate
# install nodeenv
pip install nodeenv
# install system's node into virtualenv
nodeenv --node=system --python-virtualenv
The readme is pretty good:
https://github.com/ekalinin/nodeenv
I used #just-jake solution for some time and found that nvm is easier to setup.
Also it's much powerful solution that allows to install and use different versions of nodejs.
On Ubuntu 14.04 or 16.04:
Install prerequisite packages for building nodejs:
sudo apt-get update
sudo apt-get install build-essential libssl-dev
Install nvm:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
In case newer version of nvm will be available you can find actual installation command on nvm site.
nvm installer will add bootstrap script to ~/.bashrc, so you need either to reopen terminal to run it, or to do:
source ~/.bashrc
Now you can install any nodejs version you like, switch between them etc.
Use nvm ls-remote to list available nodejs versions.
To install, for example, nodejs v4.2.4 do:
# install v4.2.4
nvm install v4.2.4
# use nodejs v4.2.4 in the current terminal session
nvm use v4.2.4
# use v4.2.4 by default in new terminal session
nvm alias default v4.2.4
As stated already here and here
npm config set prefix ~
echo export PATH=\$PATH:\~/bin >> ~/.bashrc
. ~/.bashrc
Other answers have outdated solutions: 2020's solution is using NPM_CONFIG_PREFIX environment variable. (See details)
For example,
$ NPM_CONFIG_PREFIX="$HOME/.npm-packages" npm install -g ios-sim
/Users/<name>/.npm-packages/bin/ios-sim -> /Users/<name>/.npm-packages/lib/node_modules/ios-sim/bin/ios-sim
+ ios-sim#9.0.0
added 108 packages from 68 contributors in 3.094s
To expand on the answer provided by Just Jake and user1533401: I am unable to downgrade as I use shared hosting and node is installed in a system directory. This is also why I have change the directory where npm installs global scripts if I want it to do that. For those in the same boat, here is a another temporary fix I found works:
npm install -g --prefix=$(npm config get prefix) <package>
The bug is that npm doesn't read your per-user config file, but specifying it every time you install a global script fixes that. Found here.
I have a slightly different solution to a similar problem, which was due to my installing npm globally so I can use it in the Terminal of my macOS system. I simply initialised it locally at the root directory of my repository with the command:
npm init --yes
This did the trick for enabling me to install node packages in the local root directory at /node_modules with the "package.json" and "package-lock.json" files instead of at the user's home directory.
You can use npm-user to automatically set up npm to install packages into your user's directories instead of the system's. No root privileges needed.
Here's a link to the script, instructions on how to use it and information about its options. It works on macOS, Linux, *BSD and Windows.
You can run it like so:
$ curl -s "https://raw.githubusercontent.com/alexdelorenzo/npm-user/main/npm-user.sh" | bash
After you run it, using npm install -g <package> will install packages to your user's directories without needing to use sudo.
Here's the code if you want to copy and paste it into your console:
#!/usr/bin/env bash
# Copyright 2022 Alex DeLorenzo <alexdelorenzo.dev>. Licensed under the GPLv3.
export ROOT="${1:-$HOME}"
export NPM_DIR=".npm-packages"
export NPM_ROOT="$ROOT/$NPM_DIR"
export NPM_BIN="$NPM_ROOT/bin"
export NPM_MAN="$NPM_ROOT/share/man"
export BASH_RC="$HOME/.bashrc"
export ZSH_RC="$HOME/.zshrc"
export DEFAULT_RC="$BASH_RC"
export RC_ERR=1
export INDENT=2
set -e
shopt -s expand_aliases
alias indent="paste /dev/null - | expand -$INDENT"
quiet() {
"$#" &> /dev/null
}
expand-tilde() {
local path="$1"
echo "${path/#\~/$HOME}"
}
create-paths() {
local bin="${1:-$NPM_BIN}"
local man="${2:-$NPM_MAN}"
mkdir --parents --verbose "$bin" "$man"
}
set-prefix() {
npm config set prefix "$NPM_ROOT"
}
get-vars() {
local bin="${1:-$NPM_BIN}"
local man="${2:-$NPM_MAN}"
cat <<EOF
export PATH="\$PATH:$bin"
export MANPATH="\${MANPATH:-\$(manpath)}:$man"
export NPM_PACKAGES="$NPM_ROOT"
EOF
}
already-added() {
local rc="${1:-$DEFAULT_RC}"
local bin="${2:-$NPM_BIN}"
local man="${2:-$NPM_MAN}"
local vars="$(get-vars "$bin" "$man")"
quiet grep "$vars" "$rc"
}
main() {
local rc="$(expand-tilde "${1:-$DEFAULT_RC}")"
local bin="$(expand-tilde "${2:-$NPM_BIN}")"
local man="$(expand-tilde "${3:-$NPM_MAN}")"
printf "Creating %s and %s\n" "$bin" "$man"
create-paths "$bin" "$man" || {
printf "Couldn't create paths: %s and %s.\n" "$bin" "$man"
return $RC_ERR
}
printf "Setting npm prefix.\n"
set-prefix || {
printf "Couldn't set prefix.\n"
return $RC_ERR
}
if ! already-added "$rc" "$bin" "$man"; then
printf "Writing to %s.\n" "$rc"
get-vars "$bin" "$man" >> "$rc"
fi || {
printf "Unable to write to %s.\n" "$rc"
printf "Add the following to your shell's configuration file:\n\n"
get-vars "$bin" "$man" | indent
return $RC_ERR
}
printf "Done.\n\n"
printf "To load the changes in this shell, run:\n"
printf "\tsource %s\n" "$rc"
}
main "$2" "$3" "$4"
At least on Ubuntu the default config for system wide npm is that npm install --global tries to install packages to /usr/lib/node_modules. To set different default for your own user account run following once:
mkdir -p ~/.npm/lib/bin
npm config set prefix "~/.npm/lib"
in addition you want following fragment in .profile:
# set PATH so it includes user's private .npm/lib/bin if it exists
if [ -d "$HOME/.npm/lib/bin" ] ; then
PATH="$HOME/.npm/lib/bin:$PATH"
fi
If you now install something with npm install --global packagename it will end up in correct location and can be found in your PATH (you may need to logout and re-login for .profile changes to take effect).
Of course, you could select some other directory instead. For example ~/.config/npm could make sense for modern systems.

Resources