npm link package name must have a name field to be linked - node.js

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
...

Related

Timeout trying to fetch resolutions from npm

I'm trying to install microsoft-cognitiveservices-speech-sdk
npm install microsoft-cognitiveservices-speech-sdk
but I keep encountering this error Timeout trying to fetch resolutions from npm
The solutions I'm finding on stackoverflow all point to adding a preinstall line to my package.json where I specify this "preinstall": "npx npm-force-resolutions#0.0.3" which would work fine if the microsoft-cognitiveservbices-speech-sdk didn't have the same line in its package.json where its trying to install the npm-force-resoultion in its preinstall without a version declared. So I guess is there an .npmrc config value I can set so I won't experience the timeout error (timeout=60000 didn't work)?
> microsoft-cognitiveservices-speech-sdk#1.21.0-alpha.0.1 preinstall
> npm install --package-lock-only --ignore-scripts --no-audit && npx npm-force-resolutions
up to date in 2s
98 packages are looking for funding
run `npm fund` for details
/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/npm_force_resolutions/core.cljs:61
(go
^
Error: Timeout trying to fetch resolutions from npm
at switch__2144__auto__ (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/npm_force_resolutions/core.cljs:61:3)
at /home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/npm_force_resolutions/core.cljs:61:3
at npm_force_resolutions$core$find_resolutions_$_state_machine__2145__auto____1 (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/npm_force_resolutions/core.js:476:4)
at cljs.core.async.impl.ioc-helpers/FN-IDX (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/npm_force_resolutions/core.js:492:85)
at cljs.core.async.impl.ioc-helpers/run-state-machine (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/cljs/core/async/impl/ioc_helpers.cljs:35:23)
at cljs.core.async.impl.ioc-helpers/run-state-machine-wrapped (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/cljs/core/async/impl/ioc_helpers.cljs:39:6)
at fret (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/cljs/core/async.cljs:490:22)
at take-cb (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/cljs/core/async.cljs:188:63)
at /home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/cljs/core/async/impl/channels.cljs:160:45
at Immediate.cljs$core$async$impl$dispatch$process_messages (/home/woodsy/.npm/_npx/73b02210abc194ff/node_modules/npm-force-resolutions/out/cljs/core/async/impl/dispatch.cljs:18:7)
npm ERR! code 1
npm ERR! path /home/woodsy/cognitive-services-speech-sdk-js
npm ERR! command failed
npm ERR! command sh -c npm install --package-lock-only --ignore-scripts --no-audit && npx npm-force-resolutions
I am able to install the speech sdk on a default node#16.15.1 docker container.
The only trouble I had was figuring out the earlier version of the node docker contain require you to provide the --workdir parameter, or NPM will fail every install with a generic "IdealTree" error.
Are you able to use the following?
If not, what errors do you get?
C:\Users\brianem>docker run -it --rm --workdir /temp node:16.15.1 bash
root#1a4900f41d89:/temp# npm install microsoft-cognitiveservices-speech-sdk
added 44 packages, and audited 45 packages in 47s
9 packages are looking for funding
run npm fund for details
found 0 vulnerabilities
npm notice
npm notice New major version of npm available! 8.11.0 -> 9.2.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.2.0
npm notice Run npm install -g npm#9.2.0 to update!
npm notice
root#1a4900f41d89:/temp# head node_modules/microsoft-cognitiveservices-speech-sdk/package.json
{
"name": "microsoft-cognitiveservices-speech-sdk",
"author": "Microsoft Corporation",
"homepage": "https://docs.microsoft.com/azure/cognitive-services/speech-service/",
"version": "1.24.1",
"license": "MIT",
"description": "Microsoft Cognitive Services Speech SDK for JavaScript",
"keywords": [
"microsoft",
"cognitiveservices",
root#1a4900f41d89:/temp#

Unable to install NPM inside Docker

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.

Installing fresh react app with dockerfile

I'm trying to run a fresh installation of React using a Dockerfile.
FROM node:12.14-alpine
RUN mkdir /app
WORKDIR /app
RUN npm install -g nodemon \
npx create-react-app .
COPY package.json package.json
RUN npm install --silent
COPY . .
CMD [ "node", "start" ]
My root folder is empty so no package.json file exists etc. When I run docker-compose up --build I get the following error which indicates that's failing on npx create-react-app .
Step 5/9 : RUN npm install -g nodemon npx create-react-app .
---> Running in c4878af5f94d
npm ERR! code ENOLOCAL
npm ERR! Could not install from "" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-02-18T12_55_14_899Z-debug.log
ERROR: Service 'react' failed to build: The command '/bin/sh -c npm install -g nodemon npx create-react-app .' returned a non-zero code: 1
Any ideas what I'm doing wrong? I'm not having node or npm installed nativelly so all needs t run through docker
You need to delete the . at the end of the RUN :
RUN npm install -g nodemon \
npx \
create-react-app
You forgot "&&" at the beginning line 7
FROM node:12.14-alpine
RUN mkdir /app
WORKDIR /app
RUN npm install -g nodemon \
&& npx create-react-app .
COPY package.json package.json
RUN npm install --silent
...
CMD [ "node", "start" ]

npm install not creating node_modules inside dockerfile

I am using a dockerfile to define a custom Wordpress distribution that uses React in frontend (via special theme).
To do that I am modifying the official Wordpress dockerfile and adding the theme, wget and npm as dependencies.
The problem is at the end of the dockerfile in the CUSTOM PART. npm install does not create the node_modules directory, but finishes successfully (just WARN messages). Then npm start fails gives the ERROR, because the rimraf and webpack npm packages are not installed.
Why is /var/www/html/wp-content/themes/lexi-master/node_modules not being created?
The error:
sh: 1: rimraf: not found
npm ERR! Linux 4.4.20-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "clean"
npm ERR! node v7.4.0
npm ERR! npm v4.1.1
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! Lexi-WP-Theme#0.0.1 clean: `rimraf dist`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the Lexi-WP-Theme#0.0.1 clean script 'rimraf dist'.
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 Lexi-WP-Theme package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! rimraf dist
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs Lexi-WP-Theme
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls Lexi-WP-Theme
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! Please include the following file with any support request:
npm ERR! /var/www/html/wp-content/themes/lexi-master/npm-debug.log
npm ERR! Linux 4.4.20-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "build"
npm ERR! node v7.4.0
npm ERR! npm v4.1.1
npm ERR! code ELIFECYCLE
npm ERR! Lexi-WP-Theme#0.0.1 build: `npm run clean && webpack`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Lexi-WP-Theme#0.0.1 build script 'npm run clean && webpack'.
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 Lexi-WP-Theme package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! npm run clean && webpack
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs Lexi-WP-Theme
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls Lexi-WP-Theme
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! Please include the following file with any support request:
npm ERR! /var/www/html/wp-content/themes/lexi-master/npm-debug.log
npm ERR! Linux 4.4.20-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "--prefix" "/var/www/html/wp-content/themes/lexi-master" "start"
npm ERR! node v7.4.0
npm ERR! npm v4.1.1
npm ERR! code ELIFECYCLE
npm ERR! Lexi-WP-Theme#0.0.1 start: `npm run build && node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Lexi-WP-Theme#0.0.1 start script 'npm run build && node server.js'.
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 Lexi-WP-Theme package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! npm run build && node server.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs Lexi-WP-Theme
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls Lexi-WP-Theme
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! Please include the following file with any support request:
npm ERR! /var/www/html/npm-debug.log
The command '/bin/sh -c npm --prefix /var/www/html/wp-content/themes/lexi-master start' returned a non-zero code: 1
Possible cause in dockerfile (line with npm install):
RUN cd /var/www/html/wp-content/themes/lexi-master; npm install
...whole dockerfile (only CUSTOM PART was added):
FROM php:7.1-apache
# install the PHP extensions we need
RUN set -ex; \
\
apt-get update; \
apt-get install -y \
libjpeg-dev \
libpng12-dev \
; \
rm -rf /var/lib/apt/lists/*; \
\
docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
docker-php-ext-install gd mysqli opcache
# TODO consider removing the *-dev deps and only keeping the necessary lib* packages
# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
echo 'opcache.fast_shutdown=1'; \
echo 'opcache.enable_cli=1'; \
} > /usr/local/etc/php/conf.d/opcache-recommended.ini
RUN a2enmod rewrite expires
VOLUME /var/www/html
ENV WORDPRESS_VERSION 4.7.1
ENV WORDPRESS_SHA1 8e56ba56c10a3f245c616b13e46bd996f63793d6
RUN set -ex; \
curl -o wordpress.tar.gz -fSL "https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz"; \
echo "$WORDPRESS_SHA1 *wordpress.tar.gz" | sha1sum -c -; \
# upstream tarballs include ./wordpress/ so this gives us /usr/src/wordpress
tar -xzf wordpress.tar.gz -C /usr/src/; \
rm wordpress.tar.gz; \
chown -R www-data:www-data /usr/src/wordpress
COPY docker-entrypoint.sh /usr/local/bin/
# CUSTOM PART START
COPY lexi-master /var/www/html/wp-content/themes/lexi-master
# Install wget
RUN apt-get update \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/*
# Install dependencies
RUN apt-get update && apt-get install -y \
software-properties-common
# RUN add-apt-repository universe
RUN apt-get update && apt-get install -y \
apache2 \
curl \
git \
libapache2-mod-php5 \
php5 \
php5-mcrypt \
php5-mysql \
python3.4 \
python3-pip
# Install Node.js
RUN \
cd /tmp && \
wget http://nodejs.org/dist/node-latest.tar.gz && \
tar xvzf node-latest.tar.gz && \
rm -f node-latest.tar.gz && \
cd node-v* && \
./configure && \
CXX="g++ -Wno-unused-local-typedefs" make && \
CXX="g++ -Wno-unused-local-typedefs" make install && \
cd /tmp && \
rm -rf /tmp/node-v* && \
npm install -g npm && \
printf '\n# Node.js\nexport PATH="node_modules/.bin:$PATH"' >> /root/.bashrc
RUN cd /var/www/html/wp-content/themes/lexi-master; npm install
RUN echo "export const WP_URL = '<http://www.example.com/wp-json/wp/v2';" > /var/www/html/wp-content/themes/lexi-master/src/wp-url.js
# Test the installation
RUN ls /var/www/html/wp-content/themes/lexi-master/
RUN ls /var/www/html/wp-content/themes/lexi-master/node_modules
# Start the server
RUN cd /var/www/html/wp-content/themes/lexi-master; npm start
# CUSTOM PART END
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
/var/www/html/wp-content/themes/lexi-master/package.json:
{
"name": "Lexi-WP-Theme",
"version": "0.0.1",
"description": "",
"license": "MIT",
"dependencies": {
"history": "^1.9.0",
"isomorphic-fetch": "^2.1.1",
"react": "^0.14.0-rc1",
"react-dom": "^0.14.3",
"react-redux": "^3.0.1",
"react-router": "1.0.0-rc1",
"redux": "^3.0.2",
"redux-thunk": "^1.0.0"
},
"devDependencies": {
"babel-core": "^5.6.18",
"babel-loader": "^5.1.4",
"css-loader": "^0.18.0",
"debug": "^2.2.0",
"extract-text-webpack-plugin": "^0.8.2",
"file-loader": "^0.8.4",
"html-webpack-plugin": "^1.6.1",
"react-hot-loader": "^1.3.0",
"rimraf": "^2.5.0",
"style-loader": "^0.12.3",
"webpack": "^1.9.11",
"webpack-dev-server": "^1.9.0"
},
"scripts": {
"clean": "rimraf dist",
"build": "npm run clean && webpack",
"start": "npm run build && node server.js"
}
}

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