Performing a npm install via Docker on a windows host - node.js

I'm trying to create a docker dev tools container for a devlopement environment on a windows host via docker toolbox but I have some trouble running the npm install command.
It worked fine on a linux host but on the windows host I got the following error :
npm ERR! Linux 4.1.13-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v5.5.0
npm ERR! npm v3.3.12
npm ERR! path /var/www/site/.npm/gulp/3.9.0/package.tgz.e87c24357cd6065ee71ce44c6f23673b
npm ERR! code ETXTBSY
npm ERR! errno -26
npm ERR! syscall rename
npm ERR! ETXTBSY: text file is busy, rename '/var/www/site/.npm/gulp/3.9.0/package.tgz.e87c24357cd6065ee71ce44c6f23673b' -> '/var/www/site/.npm/gulp/3.9.0/package.tgz'
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Linux 4.1.13-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v5.5.0
npm ERR! npm v3.3.12
npm ERR! path npm-debug.log.39d944b679d410e5293d6721cbc8287a
npm ERR! code ETXTBSY
npm ERR! errno -26
npm ERR! syscall rename
npm ERR! ETXTBSY: text file is busy, rename 'npm-debug.log.39d944b679d410e5293d6721cbc8287a' -> 'npm-debug.log'
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /var/www/site/npm-debug.log
Here is my Dockerfile :
FROM node:latest
RUN apt-get update
RUN apt-get install vim -y
RUN useradd -ms /bin/bash node
RUN echo "fs.inotify.max_user_watches=100000" > /etc/sysctl.conf
ADD . /var/www/site
RUN chown -R node:node /var/www/site
RUN chown -R node:node /usr/local/lib/node_modules
RUN chown -R node:node /usr/local/bin
USER node
ENV HOME /var/www/site
WORKDIR /var/www/site
RUN npm install -g bower
RUN npm install --global gulp -y
EXPOSE 80 8080 35729
In Docker quickstart terminal, I use the following commands :
Building the image (works fine)
docker build -t dev_tools .
Building the container (works fine)
docker run --name=dev_tools_container -t --rm -v "//c/Users/Public/site:/var/www/site" --net=host dev_tools
Trying to install npm dependencies (shoots the error):
docker exec -it dev_tools_container npm install
Thank you for your time !

Instead of
RUN npm install --global gulp -y
use
RUN sudo npm install --global gulp -y
You try to install gulp as a global package from user node (not superuser).
Or install gulp before switch user to node.
USER node
RUN npm install --global gulp -y
EDIT:
boot2docker is based on VirtualBox. Virtualbox does not allow symlinks on shared folders for security reasons.
To enable symlinks You must set VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME to 1. (Here is link to description how to do it on Vargrant: Symbolic links and synced folders in Vagrant)
VBoxManage setextradata VM_NAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME 1
Replace VM_NAME and SHARE_NAME and restart VirtualBox.
Another solution is add --no-bin-link to npm:
RUN npm install -g bower --no-bin-link
RUN npm install --global gulp -y --no-bin-link
EDIT 2
By default Windows 7 security policy does not allow creating symlinks as it's a potential security threat. If user is not in Administrators group run secpol.msc and navigate to Local Policies-User Rights Assignments and add your user to Create symbolic links.
If your user belongs to Administrators group then start VirtualBox with Run as Administrator.

You can mount node_modules as a volume, so it will be a Linux filesystem inside the Docker container. Add this to your Dockerfile:
VOLUME /var/www/site/node_modules
You will see the directory at C:Users/Public/site/node_modules because it is necessary for a mount point, but you will not see any contents unless you are inside the container.

Related

Error while loading shared libraries in dev containers and electron

Problem
I have an electron project inside a dev container and every time I do npm start inside the dev container I get this error:
>ProjectName#1.0.0 start /workspaces/ProjectName
>electron .
/workspaces/ProjectName/node_modules/electron/dist/electron: error while loading shared libraries: libxshfence.so.1: cannot open shared object file: No such file or directory
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! ProjectName#1.0.0 start:`electron .`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the ProjectName#1.0.0 start script
npm ERR! This is probably not a problem with npm. There is likely additional logginh output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/node/.npm/_logs/2021-04-30T21_59_57_871Z-debug.log
Access -> /home/node/.npm/_logs/2021-04-30T21_59_57_871Z-debug.log
How I got here
(I am on Debain 10 btw)
Created a remote container for Node 14 using the VS Code Command Palette
npm init -y
Added start script in package.json.
Installed electron npm install --save-dev electron#latest
This solution was made with the help of Alexander Leithner.
The problem is related to the lack of some libraries.
Solution:
Reopen the project locally.
And modify the Dockerfile in the .devcontainer folder.
Add the following line to the file:
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive && apt-get -y install --no-install-recommends xorg openbox libnss3 libasound2 libatk-adaptor libgtk-3-0
Open the project in the dev container.

Error while running (npm install -g #angular/cli) in mac

I'm trying to install angular/cli and when I run
npm install -g #angular/cli
in the terminal I come up with these error messages, furthermore, I tried to use sudo as a prefix but again not a positive result.
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git#github.com/angular/cli.git
npm ERR!
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/username/.npm/_logs/2018-10-06T04_44_40_632Z- debug.log
npm packages should be installed without sudo in macos
Infact sudo should be never be used unless tinkering with system wide
permissions. Node puts npm packages in a specific folder, usually
/usr/local/lib/node_modules. But the trouble is you need sudo
permissions to write here. This leads to an endless use of
non-requisite sudo permissions. This location is what we need to
change and here are handy terminal commands to achieve the same:
mkdir ~/.npm
npm config set prefix ~/.npm
nano ~/.bashrc
export PATH="$PATH:$HOME/.npm/bin"
source ~/.bashrc
Quoted from here: https://medium.com/#Mandysidana/using-npm-install-without-sudo-2de6f8a9e1a3
Finally, I found the answer
Here is the code just run in terminal:
sudo npm install -g #angular/cli

Can't run sample express app in node container on Windows

I'm watching a Docker course from Pluralsight and I need to run a Docker source on a Docker container I just downloaded.
Even though I have package.json file in the current path, it is not recognized.
Usuario#RE MINGW64 /d/node/ExpressSite
$ docker run -p 8080:3000 -v /$(pwd):/var/www -w "/var/www" node:4.4.5 npm start
npm info it worked if it ends with ok
npm info using npm#2.15.5
npm info using node#v4.4.5
npm ERR! Linux 4.4.12-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v4.4.5
npm ERR! npm v2.15.5
npm ERR! path /var/www/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/var/www/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /var/www/npm-debug.log
Why the Pluralsight does the same and it works? What's wrong with my version?
From official docs https://docs.docker.com/engine/userguide/containers/dockervolumes/:
If you are using Docker Machine on Mac or Windows, your Engine daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory. So, you can mount files or directories on OS X using.
docker run -v /Users/<path>:/<container path> ...
On Windows, mount directories using:
docker run -v /c/Users/<path>:/<container path> ...
So, you can create a directory inside C:\Users\<your_username> (code for example) with your code and mount it inside the container like so:
docker run -p 8080:3000 -v /c/Users/<your_username>/code:/var/www -w "/var/www" node:4.4.5 npm start
Please note that your code will be available inside the container in /var/www/ directory
pwd returns /c/Users/<my_username>. Try it yourself in Docker terminal.
You can use pwd for convenience sake:
docker run -p 8080:3000 -v $(pwd)/code:/var/www -w "/var/www" node:4.4.5 npm start
Good luck with the course and Dockerize all the things!

'http-server' is not recognized as an internal or external command

After installing angular-seed project, i did the following steps:
Cloned the repository :
git clone https://github.com/angular/angular-seed.git
cd angular-seed
Then I ran npm install
at the end of the install i get:
angular-seed#0.0.0 prestart C:\Users\user\Documents\GitHub\comilion\angular-seed
npm install
angular-seed#0.0.0 postinstall C:\Users\user\Documents\GitHub\myproject\angular-seed
bower install
angular-seed#0.0.0 start C:\Users\user\Documents\GitHub\myproject\angular-seed
http-server -a localhost -p 2324
'http-server' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! angular-seed#0.0.0 start: http-server -a localhost -p 2324
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the
angular-seed#0.0.0 start script. npm ERR! This is most likely a
problem with the angular-seed package, npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system: npm ERR!
http-server -a localhost -p 2324 npm ERR! You can get their info via:
npm ERR! npm owner ls angular-seed npm ERR! There is likely
additional logging output above. npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\Program Files\nodejs\\node.exe" "C:\Program
Files\nodejs\node_modules\npm\bin\npm-cli.js" "start" npm ERR!
cwd C:\Users\user\Documents\GitHub\myproject\angular-seed npm ERR!
node -v v0.10.22 npm ERR! npm -v 1.3.14 npm ERR! code ELIFECYCLE npm
ERR! npm ERR! Additional logging details can be found in: npm ERR!
C:\Users\user\Documents\GitHub\myproject\angular-seed\npm-debug.log
please let me know if you have any suggestions how to solve this issue.
#BenFortune found the answer it was
http-server needs to be installed globally with npm install -g http-server
If the accepted solution did not work for you. Try the following. This solved the issue in my system.
Make sure the locations of both npm and http-server are added in environment variable 'PATH' (Both user variable and System variable).
If it is not present,
add C:\Users\username\AppData\Roaming\npm and C:\Users\username\AppData\Roaming\npm\node_modules\http-server to environment variable 'PATH'
If you are using Window 10. Then after run
npm install -g http-server
The http-server.cmd will available in C:\Users\your-user\AppData\Roaming\npm
Then check if `http-server` work in `cmd` or not.
If it works, you don't need to see the below part.
If it not work. You need to check Environment Variable
If you add variable to Path (inside admin). You need to run cmd as Administrator.
If you add variable to Path (inside System variable). You don't need to run cmd as Administrator.
Add C:\Users\your-user\AppData\Roaming\npm -> OK -> OK -> Re-open the cmd.
The http-server will work
npm install -g http-server
Use this command to install http-server module into your project. You can verify this in package.json
You can fix the issues by doing the following steps,
Install http-server globally by npm install -g http-server
Added 'PATH' in environment variable for system variable. Path will be like this C:\Users\username\AppData\Roaming\npm
For me all that had to be done after the basic install
npm install http-server -g
Is to run as administrator from the project directory this command:
http-server dist/project-name
1-remove http-server by command
npm uninstall http-server -g
2- install again
npm install http-server -g

npm install error ENOTDIR

I am very new to Node.js and trying to install Flatiron using npm but it gives me an error.
sudo npm install flatiron -g
And I get -
npm http GET https://registry.npmjs.org/flatiron
npm http 304 https://registry.npmjs.org/flatiron
npm ERR! Error: ENOTDIR, mkdir '/home/siddharthsaha/tmp/npm-28554/1353323290836-0.20847953506745398'
npm ERR! If you need help, you may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-#googlegroups.com>
npm ERR! System Linux 3.2.0-24-generic
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "flatiron" "-g"
npm ERR! cwd /home/siddharthsaha/denarit
npm ERR! node -v v0.8.14
npm ERR! npm -v 1.1.65
npm ERR! path /home/siddharthsaha/tmp/npm-28554/1353323290836-0.20847953506745398
npm ERR! code ENOTDIR
npm ERR! errno 27
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/siddharthsaha/denarit/npm-debug.log
npm ERR! not ok code 0
What is wrong here? I have no clue.
Just solved the issue. Its because there's a file called tmp in the home directory.
rm -rf ~/tmp
sudo npm cache clear
sudo npm install -g node
Also... if you are trying to install npm then the same error and solution applies - delete ~/tmp
Try
sudo mkdir -p /home/siddharthsaha/tmp
sudo npm cache clear
before starting the install script, since npm http 304 https://registry.npmjs.org/flatiron line states that this module is coming from cache. And also ENOTDIR states that there is no directory. Therefore, emptying the cache would solve your problem.
Below are the steps to install a given release from source without root
NOTE - this installs nodejs which gives you both node as well as npm,
they come together per release.
to start fresh remove prior node.js and npm installs as well as these :
sudo mv ~/.npmrc ~/.npmrc_ignore
sudo mv ~/.npm ~/.npm_ignore
sudo mv ~/tmp ~/tmp_ignore
sudo mv ~/.npm-init.js ~/.npm-init.js_ignore
to install nodejs and npm as yourself NOT root do these commands (OSX/linux) :
export NODE_PARENT=${HOME}/bin_0_10_32
mkdir ${NODE_PARENT}
download source from : http://nodejs.org/download/
cd node-v0.xxxx
./configure --prefix=${NODE_PARENT}/nodejs
make -j8
make install # IMPORTANT this is NOT using sudo
# not wanted since installing into $USER owned $NODE_PARENT
which puts it into dir defined by above --prefix
export PATH=${NODE_PARENT}/nodejs/bin:$PATH
define environment variable NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into current dir :
export NODE_PATH=${NODE_PARENT}/nodejs/lib/node_modules
when you use syntax : npm install -g some_cool_module
the -g for global installs it into dir $NODE_PATH and not your $PWD
nodejs install gives you npm as well :
ls -la ${NODE_PARENT}/nodejs/bin
Subsequent modules you install using global flag -g will automagically put
their ~binaries~ into above bin dir ... like browserify
Now put above three export xxx=yyy
commands into your ~/.bashrc or some such so your environment is setup
I direct delete the file of npm-debug.log.
then it's ok for me.

Resources