Docker node installation - node.js

I'm trying to prepare a docker image to speed-up the building process and to avoid installing various tools and libs every build, my base image will contain it and then I will use it with all installed things I need.
So the problem is that I'm trying to install node from NVM (Node Version Manager) but after I install I cannot use nor nvm nor npm command.
My base image is golang:1.13.1 and I do the following things.
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
Then among tutorials, I saw that ~/.nvm/nvm.sh must be run to finish the job like below
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
I changed it a little bit because my docker image does not recognize few commands due to different shells.
But when I do two separate RUN, the second one does not see NVM_DIR anymore. Of course, I can do everything in one RUN but I need to have npm later on, so each RUN should be able to see this command.
Also, I tried exporting NVM_DIR but it still does not work even when I restart with . ~/.bashrc.
The point of having NVM is that I don't want to care about the node version. Each build will be a LTS version and this is OK for me.
Sharing your solution or advice is welcome. Thanks in advance

The way to install nodejs with npm without changing image and without nvm (which I don't really like) is
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \
&& apt-get install -y nodejs
After RUN npm -v and RUN node -v it's the same as nvm's LTS version so 6.9.0 and 10.16.3.
I couldn't find better solution but I hope it may help someone

Related

Why installed Node.js and Yarn is lost after reconnect to EC2 linux instance

I have on AWS EC2 instance. When I install Node.js with this
AWS tutorial all works fine until i logout current session and log in, than Node.js and Yarn are gone. There is also note from AWS:
The node installation only applies to the current Amazon EC2 session. If you restart your CLI session you need to use nvm to enable the installed node version. If the instance is terminated, you need to install node again. The alternative is to make an Amazon Machine Image (AMI) of the Amazon EC2 instance once you have the configuration that you want to keep, as described in the following topic.
Is it possible to install Node.js so it is available in another session? I installed zsh and that works for all sessions.
I also created simple script that will install Node.js and Yarn. Commands in this script are from AWS Tutorial page that I mentioned early. It will install Node.js and Yarn, show that all works (it shows Yarn version and message from node command), but when I type npm --version or yarn --version it will shows message zsh: command not found: yarn (or similar message for npm).
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts
node -e "console.log('Running Node.js ' + process.version)"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
echo "installing YARN !!!!! "
npm install yarn --global
Why is my script not working?
If you use manual from AWS how to install Node.js than instalation is only made in your home directory that lives only for current session. To install Node.js and Yarn I used this post: https://stackoverflow.com/a/35165401/78935
Or you could simply use this:
sudo curl --silent --location https://rpm.nodesource.com/setup_16.x | bash
sudo yum -y install nodejs
sudo npm install yarn --global
yarn --version

Stuck at npm install at fechMetadata checking installable status

Suddently I can't install angular
I get stuck at the npm install command
"npm install -g #angular/cli"
It stays forever on this "checking installable status".
my node version is 8.11.3 (yes I already tried to uninstall node and double checked to see it was really uninstalled) this was the version I had before and was working fine
my npm -v gives 5.6.0
then I run the angular command to install angular and it seems to freezing or very very very slow....
npm install -g #angular/cli --verbose
After running this command I realized npm was having problems with the connection with registry.npmjs.org
To solve this:
npm config set registry "http://registry.npmjs.org"
npm set maxsockets 3
Viewed here
In my case I had to wait for a few minutes and npm finally installed by package.
Also I suggest to use --verbose flag to see what's actually is going.
Try this:
npm install -g --no-optional pm2
This will ignore all the dependencies specified in the package.json file (if present).
Hope this helps.
To anyone still experiencing this, I spent days searching for a solution, it ended up being easier and more effective to just remove all traces of nvm (and its node) from my machine and reinstall. Everything started working smooth again after the reinstall
I did:
brew uninstall nvm
rm -rf $NVM_DIR ~/.nvm ~/.npm ~/.bower
# remove nvm entries from my .bash_profile|.bashrc then
# installed nvm from nvm's install script
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash
# add to bash_profile
cat << EOF >> ~/.bash_profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
EOF
source ~/.bash_profile
nvm install --lts
Try npm install --g --no-optional pm2
Check this out for reference
You may need to specify your proxy server in the global Git configuration, like this:
git config --global http.proxy http://your-proxy-server:port
git config --global https.proxy http://your-proxy-server:port
And since you mentioned you're using Git for Windows, best to put this into the system wide configuration, too (repeat these commands with --system instead of --global).
Theoretically, the global config should take precedence over the system config, but sometimes when using npm install on Windows, the global config seems to be ignored or not found. I suspect this can happen when there are conflicting settings in the USERPROFILE and HOMESHARE env vars, in which case Git may be confused and look in different places depending on how it is invoked.
It is possible that you have a custom registry set up in your global .npmrc. That was the problem in my case: my company uses a custom registry which falls back to the NPM registry. It is not a problem for work projects, because all the required packages are already present there, but I didn't realize this was affecting a new project whose packages were not included in the mirror registry and looking them all up must have been the cause of the slowdown.
I solved it by resetting the registry config to the NPM registry in an .npmrc for that specific project:
registry=https://registry.npmjs.org
For me the issue is that the package I was trying to install had this in its package.json:
"dependencies": {
[...]
"mobx-utils": "github:Venryx/mobx-utils#5.5.2_VPatch2"
}
Normally that works fine, but apparently today, NPM decided to hang on the call to retrieve the library contents from the GitHub repo.
I used Process Hacker 2 to investigate what exact command was hanging, and it was the following:
git.exe ls-remote -h -t git://github.com/Venryx/mobx-utils.git
In my case, I worked around the issue by just manually installing the subdependencies (and copy-pasting the mobx-utils library itself), but this is of course not ideal.
UPDATE: The issue is that I was running an outdated version of Git for Windows. Once I updated it to the latest version (v2.28.0), the issue was resolved. (ie. installing based on github urls/branches started working just fine again)
Or maybe there is a service outage as in my case. Recommending to first check the status page, where you get the info, if everything is working as it should be: https://status.npmjs.org/
If you see something like this, you know, you will have to wait for a while:
for those with the same problem try
npm config set strict-ssl false

Cant fix npm permissions mac mojave

I have spent the last few hours trying every tutorial out there on how to fix npm permissions on a mac.
NOTHING has worked thus far.
Steps I have taken
I have uninstalled node multiple times, tried running brew install node --without-npm then installing npm seperately.
I have tried to create my own npm-package file in my root directory and change the npm source and still that doesnt work.
I have tried what is detailed here https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md
I have tried what is shown in this video https://www.youtube.com/watch?v=bxvybxYFq2o&t=154s
I have also tried a few other techniques all similar to above but nothing has worked.
Im on a 2015 macbook running a fresh install 0SX Mojave.
Any time I try to install a package it errors, and even if I use sudo, npm cant find the commands I'm entering for example if I
sudo npm install #angular/cli then try and use ng I get the command not found: ng
Any help would be appreciated. This question will be updated with methods used as they are suggested. Let me know If you need any more information.
I understand your frustration :(
These steps worked for me (from https://github.com/creationix/nvm/blob/master/README.md):
check if you have ~/.bash_profile file (home directory). I didn't. So I had to create it myself with:
touch ~/.bash_profile
Get NVM by running:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
them run
export NVM_DIR="${XDG_CONFIG_HOME/:-$HOME/.}nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
verify nvm installation by running (you might need to restart terminal window before)
nvm --version
Install node via nvm - run:
nvm install node
(if you get version warning/error just follow the instructions(I had to run npm config delete prefix and then again nvm install node))
Install CLI:
npm install -g #angular/cli
verify installation:
ng -v
Hope it helps!

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 Node.js to install n to install Node.js?

I have a problem understanding the use of n. Basically, it is clear that it is a version manager for Node.js such as nvm.
But in contrast to nvm, which is basically a shell script, according to the documentation you are encouraged to use npm to install n:
$ npm install -g n
What I don't get is: For having npm at hand you need to install Node.js. Why would I install Node.js manually to use npm to then be able to install Node.js using n?
To put my question in other words: Why does n suggest installing using npm, if its main purpose is to install Node.js, which includes npm?
tl; dr
# Installs n and the latest LTS Node.js version to ~/n.
# For bash, ksh, zsh, modifies the respective user-specific shell-initialization file to
# define env. variable N_PREFIX and append $N_PREFIX/bin to the $PATH.
curl -L https://git.io/n-install | bash
I feel your pain. Installing Node.js to then install n to then manage Node.js installations is indeed a strange setup.
It would indeed be great to be able to install n by itself first.
I've created a project to support installation of n directly from GitHub; the only prerequisite beyond what n itself needs is git.
Note that you must first remove any pre-existing n / Node.js versions.
The target directory, ~/n by default, must either not yet exist or be empty.
For bash, ksh, and zsh, the relevant shell initialization file (e.g., ~/.bashrc) is automatically modified to define environment variable N_PREFIX and append $N_PREFIX/bin to the $PATH; for other shells, this must be done manually.
Aside from installing n directly from GitHub, it also installs helper scripts for updating n (n-update) and uninstalling it (n-uninstall).
Here are working examples; see the n-install GitHub repo for details:
Installation with confirmation prompt to confirm installing to default location $HOME/n and installing the latest LTS Node.js version:
curl -L https://git.io/n-install | bash
Automated installation to the default location, with subsequent installation of the latest LTS (long-term support) and latest-overall Node.js versions, as well as the latest 4.1.x Node.js version:
curl -L https://git.io/n-install | bash -s -- -y lts latest 4.1
Automated installation to the default location, without subsequent installation of a Node.js version:
curl -L https://git.io/n-install | bash -s -- -y -
Automated installation to custom location ~/util/n, with subsequent installation of the latest LTS Node.js version:
curl -L https://git.io/n-install | N_PREFIX=~/util/n bash -s -- -y
If you prefer, you can install n from source:
cd /tmp
git clone --depth=1 https://github.com/tj/n
cd n
sudo make install
Then you can install the latest stable version of node as follows:
n stable
The n module was created for convenience.
For example, if you wanted to update your version of Node.js from v0.8.0 to v0.10.20, would you rather download a package, extract and compile? Or would you rather type n 0.10.20 and have it instantly installed, while still retaining previous versions of Node for easy switching?
n suggests using npm to install it because n is a module. That is, npm is the easiest way to install it. Node modules have the functionality of being able to run in a shell when installed globally, so that function was utilized to make switching Node versions much easier.
You can also install npm separately from Node.JS; e.g.: on a system without Node.JS:
git clone https://github.com/npm/npm
cd npm
./configure
make
Reference: NPM GitHub project
I had the same question, but have seen the light. 'n' is a handy tool and makes it simple to test different versions of node. Works great on Linux, but no matter how I try to install it on OS X (git clone, then npm install or using user456584's recommended method), when I run it, I always get the same results of "Error: no installed version", even though it installs into
/usr/local/lib/node_modules/n
and
/usr/local/bin/n
Frustrating because I've found this tool to be so handy on Linux.
If you have included your default node bin in the $PATH variable like this
export PATH=/usr/local/Cellar/node/11.5.0/bin:$PATH
then n will not be able to active other node versions. Remove this export from the path and then you can manage the currently active node version by n.
The README for n now has a longer section covering different installation approaches.
Like nvm, n is a bash script. npm is suggested as an easy way to install n if you already have npm, and then you can use n to change the Node.js version. But there are plenty of other approaches for a first install of Node.js. In brief and in no particular order...
You can install n using curl:
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
Or clone the n repo and install from there:
make install
Or use n-install:
curl -L https://git.io/n-install | bash
Or Homebrew:
brew install n
Or MacPorts:
port install n
(Disclaimer: I am the current maintainer of n.)
If you are using n then you should use below command
bash$ sudo n latest

Resources