Unable to install NPM inside Docker - node.js

I have a project that builds locally, and I try to create a Docker image.
In my project, I install NPM packages, and my package file is located at:
\src\Core.Blazor\package.json
But, when I try to build my Docker image, it failed with the following error:
npm ERR! code ENOLOCAL
npm ERR! Could not install "src/Core.Blazor/package.json" as it is not a directory and is not a file with a name ending in .tgz, .tar.gz or .tar
I join the Dockerfile too:
### >>> GLOBALS
ARG ENVIRONMENT="Production"
ARG PROJECT="PyProd.IdentityServer.Host"
### <<<
# debian buster - AMD64
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
### >>> IMPORTS
ARG ENVIRONMENT
ARG PROJECT
### <<<
ARG NUGET_CACHE=https://api.nuget.org/v3/index.json
ARG NUGET_FEED=https://api.nuget.org/v3/index.json
# Copy sources
COPY src/ /app/src
ADD common.props /app
WORKDIR /app
# Installs NodeJS to build typescripts
RUN apt-get update
RUN apt-get install curl
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs
RUN npm install /app/src/Core.Blazor/package.json
After trying to fix with the following, I also get an error:
RUN npm install /app/src/SmartPixel.Core.Blazor/
Here is the error I got:
> aspnet-parcel-exp#0.1.0 build /app/src/SmartPixel.Core.Blazor
> parcel build wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/
sh: 1: parcel: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! aspnet-parcel-exp#0.1.0 build: `parcel build wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the aspnet-parcel-exp#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
I also join the package.json file:
{
"name": "aspnet-parcel-exp",
"private": true,
"version": "0.1.0",
"devDependencies": {
"parcel": "1.12.3"
},
"includePaths": [
"./wwwroot/assets/js",
"./wwwroot/assets/css"
],
"scripts": {
"build": "parcel build wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/",
"watch": "parcel watch wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/"
}
}

I don't know NodeJS, but as your error says:
npm ERR! Could not install "src/Core.Blazor/package.json" as it is not a directory and is not a file with a name ending in .tgz, .tar.gz or .tar
I believe you should change your Dockerfile to this:
RUN npm install /app/src/Core.Blazor/
Or even this:
RUN npm install /app/src/Core.Blazor
As the second one doesn't have trailing slash.

Related

Posting to cyclic and it's asking for package.json to be in the root?

package.json not found at root level of repository.
Cyclic runs scripts defined "scripts" section to build, test and run your app.
But my project is set up like:
-final
--backend
---package.json(including npm start)
--frontend
---package.json(including npm start)
-package.json
^ includes:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
How do I merge these packages together and put them into the root directory so I can make this react application live?
I was thinking about something like this:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
but when I go to the root directory (final)
npm ERR! Missing script: "start"
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favorite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run

npm link package name must have a name field to be linked

Hi I am trying to create a docker image.
Below is a part of my docker file which is causing problem.I think the problem is due to nodejs package.
The current softlink is:
ls -la "/usr/bin/npm"
lrwxrwxrwx 1 root root 25 Mar 2 01:50 /usr/bin/npm -> /usr/lib/node_modules/npm
Installation is does by a nonroot user
Docker File
#Install node js and npm
RUN curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
RUN apt-get -y install nodejs
RUN npm install -g bower gulp
RUN npm install -g node-gyp#latest --save
RUN npm init -y
RUN npm install -g n
RUN npm install zmq --save
RUN ldconfig
RUN npm config set prefix ~/npm
#RUN npm install -g stf t
#RUN npm install
#RUN rm -rf /usr/bin/npm
#RUN ln -s /usr/lib/node_modules/npm/ /usr/bin/npm
RUN npm link
Below is the docker build output:
Step 35/42 : RUN npm init -y
---> Running in abf773c34e35
Wrote to /package.json:
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Step 38/42 : RUN ldconfig
---> Running in 1562e282d324
Removing intermediate container 1562e282d324
---> bdf746069a90
Step 39/42 : RUN npm config set prefix ~/npm
---> Running in 400512f792d0
Removing intermediate container 400512f792d0
---> 0f3eeb03910c
Step 40/42 : RUN npm link
---> Running in 40ae29419291
npm ERR! Linux 4.4.0-98-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "link"
npm ERR! node v6.13.0
npm ERR! npm v3.10.10
npm ERR! Package must have a name field to be linked
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! /npm-debug.log
The command '/bin/sh -c npm link' returned a non-zero code: 1
Any help would be appreciated.Thanks
The problem is that the generated package.json does not have the name property set "name": "",.
As documented in npm-link:
Note that package-name is taken from package.json, not from directory name
The reason why name is empty in package.json is that you are running npm init in the root / of the container.
You have to create a directory and run npm init in there.
...
WORKDIR /app
RUN npm init -y
...

Docker container that pulls from private gilab repository

I'm building a Docker container for my Node.js + Vue application.
Since I have a global css library in another repository I have added this line in my package.json file:
"lib-css": "git+ssh://git#git.lib.com:9922/username/lib-css.git#development",
That way when I run npm install I install also my CSS library. The problem is that on my local env it asks for my password and I can insert it, but in the Docker build the process fails with the following error:
Step 7/10 : RUN npm install
---> Running in db10ca83586d
npm WARN deprecated babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git#git.lib.com:9922/username/lib-css.git
npm ERR!
npm ERR! Host key verification failed.
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! /root/.npm/_logs/2017-12-11T08_49_11_152Z-debug.log
This is my current Dockerfile:
FROM node:carbon
WORKDIR /usr/src/app
RUN mkdir -p /root/.ssh
COPY .secrets /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh && chmod 600 /root/.ssh/*
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 8081
CMD [ "npm", "run dev" ]
~
My .secrets file contains my private key associated to the repository.
How can I make this works?

Running babel-cli from npm script not working

I followed the directions here to install babel-cli. I added "build": "babel src -d lib" to my package.json in the directory I want to run it in. However, on running, I get this error:
🐕 npm run build
> ipfs-readme-standard#1.0.0 build /Users/richard/src/ipfs-readme-standard
> babel src -d lib
src doesn't exist
npm ERR! Darwin 14.5.0
npm ERR! argv "/Users/richard/.nvm/versions/node/v5.0.0/bin/node" "/Users/richard/.nvm/versions/node/v5.0.0/bin/npm" "run" "build"
npm ERR! node v5.0.0
npm ERR! npm v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! ipfs-readme-standard#1.0.0 build: `babel src -d lib`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the ipfs-readme-standard#1.0.0 build script 'babel src -d lib'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ipfs-readme-standard package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel src -d lib
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs ipfs-readme-standard
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls ipfs-readme-standard
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/richard/src/ipfs-readme-standard/npm-debug.log
I'm at a loss. Shouldn't src be generated? There's no extra step on babeljs.io that I am missing.
Shouldn't src be generated?
This is the folder that contains the script that you want to be transpiled. If it doesn't exist then babel will throw the error you posted.
Also, take note of what it says at the bottom of page you linked to:
Pre-6.x, Babel enabled certain transformations by default. However, Babel 6.x does not ship with any transformations enabled. You need to explicitly tell it what transformations to run. The simplest way to do this is by using a preset, such as the ES2015 Preset.
This means that even if you create a src directory and place a file containing ES6 code in it, Babel will happily run, but the output will be (almost) identical to the input.
This is a quick example of how to get up and running with the babel-cli.
Create a project, then install the babel-cli package and ES2015 preset:
mkdir babeltest && cd babeltest
touch package.json
npm install babel-cli babel-preset-es2015 --save-dev
Next edit package.json:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"build": "babel src -d lib"
},
"scripts": {
"build": "babel --presets es2015 src -d lib"
},
"devDependencies": {
"babel-cli": "^6.0.0"
}
}
Notice that the command in the npm scripts is slightly different to that on the babel homepage, in so far as we're telling it to use the installed presets.
Next make a file in the src directory:
mkdir src && cd src
touch main.js
In main.js add:
[1,2,3].map(x => x * x)
Then run babel via npm:
npm run build
And inspect the output in lib/main.js
"use strict";
[1, 2, 3].map(function (x) {
return x * x;
});
You also get this error when your node modules are not installed, If you download the code from the internet and immediately try running the code, it throws the above error, just run
npm install
and then
npm run build // or other commands should work
In case someone is still looking for solution, check if .babelrc is missing
If so simply create a new .babelrc file and paste the above snippet in it.
{
"presets": ["es2015", "stage-0"]
}

Npm install failed with "cannot run in wd"

I am trying to get my node environment set up on a new Ubuntu 12.04 instance, with Node 0.8.14 already installed, but I ran into problems when I try to run npm install.
So when I try npm install, it says that I need to run it as root or adminisrator:
Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/coffee-script',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/coffee-script',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ 'DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:297:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
But when try to run it as sudo, it says the following:
npm WARN cannot run in wd PackNodeDev#0.0.1-166 npm install -g coffee-script node-gyp (wd=/home/ubuntu/PackNode)
In my package.json, it contains the following scripts:
"scripts": {
"preinstall": "npm install -g coffee-script node-gyp",
"start": "node server.js",
"test": "mocha --require should --compilers coffee:coffee-script --colors"
},
The rest of devdependencies are valid since I have been installing it all right on my own machine (Mac)
Does anyone have a clue why this is happening?
The documentation says (also here):
If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
Your options are:
Run npm install with the --unsafe-perm flag:
[sudo] npm install --unsafe-perm
Add the unsafe-perm flag to your package.json:
"config": {
"unsafe-perm":true
}
Don't use the preinstall script to install global modules, install them separately and then run the regular npm install without root privileges:
sudo npm install -g coffee-script node-gyp
npm install
Related:
package.json for global module installation
The only thing that worked for me was adding a .npmrc file containing:
unsafe-perm = true
Adding the same config to package.json didn't have any effect.
I have experienced the same problem when trying to publish my nodejs app in a private server running CentOs using root user. The same error is fired by "postinstall": "./node_modules/bower/bin/bower install" in my package.json file so the only solution that was working for me is to use both options to avoid the error:
1: use --allow-root option for bower install command
"postinstall": "./node_modules/bower/bin/bower --allow-root install"
2: use --unsafe-perm option for npm install command
npm install --unsafe-perm
OP here, I have learned a lot more about node since I first asked this question. Though Dmitry's answer was very helpful, what ultimately did it for me is to install node with the correct permissions.
I highly recommend not installing node using any package managers, but rather to compile it yourself so that it resides in a local directory with normal permissions.
This article provides a very clear step-by-step instruction of how to do so:
https://www.digitalocean.com/community/tutorials/how-to-install-an-upstream-version-of-node-js-on-ubuntu-12-04
!~~ For Docker ~~!
#Alexander Mills answer - just made it easier to find:
RUN npm set unsafe-perm true
I fixed this by changing the ownership of /usr/local and ~/Users/user-name like so:
sudo chown -R my_name /usr/local
This allowed me to do everything without sudo

Resources