NPM install is crashing with error "ERR! cb() never called!" - npm

We are periodically seeing builds fail within our cloud build process with the following error:
docker.io/library/node:14 npm ERR! cb() never called!
npm ERR! This is an error with npm itself. Please report this error
at: npm ERR! https://npm.community
Steps from build configuration:
steps:
- name: 'gcr.io/cloud-builders/git'
id: 'fetch'
entrypoint: 'bash'
args:
- '-c'
- |
# convert the shallow clone to regular one
git fetch --unshallow --no-tags
waitFor: ['-']
- name: 'node:14'
id: 'npm-install'
entrypoint: 'bash'
args:
- '-c'
- |
npm ci --unsafe-perm
env:
- 'CYPRESS_INSTALL_BINARY=0'
- 'CYPRESS_CACHE_FOLDER=/cypress_cache'
waitFor: ['fetch']
Any thoughts on things to try or to remediate this issue?
EDIT: All packages are installed from npm.

As suggested by #Milan Tenk a good approach would be to implement a retry logic in your build by wrapping your command in a bash script.
Here are some examples on how you can modify you yaml file to a run bash scripts in Cloud Build:
steps:
- name: 'bash'
args: ['./retryPolicy.bash']
If bash is not the default entrypoint:
steps:
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args: ['tools/retryPolicy.sh','--foo']
And here is an example of a retry logic implementation in bash:
#!/bin/bash
set -euo pipefail
function myFunction() {
myCommand
return $?
}
retry=0
maxRetries=5
retryInterval=15
until [ ${retry} -ge ${maxRetries} ]
do
myFunction && break
retry=$[${retry}+1]
echo "Retrying [${retry}/${maxRetries}] in ${retryInterval}(s) "
sleep ${retryInterval}
done
if [ ${retry} -ge ${maxRetries} ]; then
echo "Failed after ${maxRetries} attempts!"
exit 1
fi
Please let me know if that approach worked for you.

See NPM CLI WiKi:
https://github.com/npm/cli/wiki/%22cb()-never-called%3F-Exit-handler-never-called%3F-I'm-having-the-same-problem!%22
Sadly, there's never a straightforward answer.
However, many people seem to suggest deleting the node_modules folder in your project and also running the command npm cache clean -force.
For me it did not work - so I copied a node_modules folder from a PC, which could run npm install without having the error.
This at least allowed to use npm install without errors and build the project again.

Related

configuring .npmrc to use NPM_TOKEN to publish to public repo

I've created a public npm repository: https://www.npmjs.com/package/point-cloud-visualiser
I'm trying to run npm publish from within codebuild. I have generated a token and saved it in AWS secrets manager. I am succesfully pulling the secret in the buildspec. However I am unsure how to configure the .npmrc file so that it uses the token and doesn't tell me to login.
Currently my .npmrc looks like this:
#point-cloud-visualiser:registry=https://www.npmjs.com/package/point-cloud-visualiser
//point-cloud-visualiser/:_authToken=${NPM_TOKEN}
And my buildspec.yml looks like this:
version: 0.2
env:
secrets-manager:
NPM_TOKEN: "npm-token:npm-token"
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
- npm publish
But when it fails on npm publish giving the error:
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in to https://registry.npmjs.org/
npm ERR! need auth You need to authorize this machine using `npm adduser`
I have also tried removing the .npmrc file and using the following in the buildspec.yml:
version: 0.2
env:
secrets-manager:
NPM_TOKEN: "npm-token:npm-token"
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
- npm config set registry 'https://www.npmjs.com/package/point-cloud-visualiser'
- npm config set '//npmjs.com/package/point-cloud-visualiser/:always-auth' 'true'
- npm config set '//npmjs.com/package/point-cloud-visualiser/:_authToken' '${NPM_TOKEN}'
- npm publish
But this approach gives the same result as above. What am I doing wrong? Thank you!
The following buildspec worked succesfully:
version: 0.2
env:
secrets-manager:
NPM_TOKEN: "npm-token:npm-token"
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
- echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
- echo "//registry.npmjs.org/:always-auth=true" >> ~/.npmrc
- npm publish

NPX command not running in Azure pipeline

I need your help
I was trying to run my cypress test cases which I generally run using command
"npx cypress run"
so I tried to have the same command in tasks when I created Azure pipeline
after NPM Install
I even tried installing npx via
npm task and custom command "npm install nx"
and this causing the below issue so can anyone suggest to me how to proceed in this case
"##[warning]Couldn't find a debug log in the cache or working directory
##[error]Error: Npm failed with return code: 1
"
From the error screenshot, it shows that you are using the Npm Task and running the command: npm npm install nx.
The command is invalid.
To solve this issue, you need to remove the npm in the NPM task -> Command and arguments .
Refer to the following sample:
YAML Pipeline:
- task: Npm#1
displayName: 'npm custom'
inputs:
command: custom
verbose: false
customCommand: 'install nx'
Classic Pipeline:

Get npm package latest version from a gitlab registry

I'm trying to put a package in a Gitlab registry using npm and .gitlab-ci.yml.
.gitlab-ci.yml
npm-package:
stage: build
image: node:buster
before_script:
- git config --global user.name "${GITLAB_USER_NAME}"
- git config --global user.email "${GITLAB_USER_EMAIL}"
script:
- npm config set #${CI_PROJECT_ROOT_NAMESPACE}:registry=https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/
- npm config set //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}
- NPM_PACKAGE_NAME=$(node -p "require('.package.json').name")
- NPM_PACKAGE_VERSION=$(node -p "require('./package.json').version")
- echo $(npm view "${NPM_PACKAGE_NAME}" versions)
- echo ${NPM_PACKAGE_NAME}
- echo ${NPM_PACKAGE_VERSION}
- |
if [[ $(npm view "${NPM_PACKAGE_NAME}" versions) != *"'${NPM_PACKAGE_VERSION}'"* ]]; then
npm config set //${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}
npm publish
echo "Successfully published version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} to GitLab's NPM registry: ${CI_PROJECT_URL}/-/packages"
else
echo "Version ${NPM_PACKAGE_VERSION} of ${NPM_PACKAGE_NAME} has already been published, so no new version has been published."
fi
I tried my first time and the package was saved successfully in the repo. And now I'm trying to run it under the condition: if the version's package has changed then run an npm publish.
But the variable $(npm view "${NPM_PACKAGE_NAME}" versions) seems to be empty, and when I try to echo it I get the error:
npm ERR! code E401
npm ERR! 401 Unauthorized - GET https://gitlab.example.com/api/v4/projects/1/packages/npm/#my_scope/my_package
Any help?
Your CI does not have enough rights.
1- You need to generate an access token (automation type) from your npm registry via the npm UI (or via the command line).
The process with the UI:
https://docs.npmjs.com/creating-and-viewing-access-tokens
2- Assign the token to an environment variable (named NPM_TOKEN in this example) accessible to your CI
3- Then create (or update) a .npmrc file with this line at the top:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

GitlabCI - Package Registry - NPM Package can't publish

My goal is to publish a npm package (private if possible) on the Gitlab Registry.
Here is my files :
Package.json :
{
"name": "#sushislasher/sushislasher-package",
"version": "0.0.1",
"description": "Package for game",
"main": "index.ts",
"repository": "https://gitlab.com/sushislasher/sushislasher-package",
"author": "Gildraen",
"license": "MIT",
"private": false
}
Gitlab-CI.yml :
stages:
- deploy
image: $CI_REGISTRY_IMAGE/docker-in-docker
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
COMPOSE_FILE: "docker-compose.yml:docker-compose.gitlab-ci.yml"
.template-load-gitlab-image: &internal-image
before_script:
- docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
- docker-compose pull js || true
deploy:
<<: *internal-image
stage: deploy
script:
- |
{
echo "#${CI_PROJECT_ROOT_NAMESPACE}:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
echo "${CI_API_V4_URL#https?}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}"
} | tee -a app/.npmrc
- make install
- make npm-publish
make install build container, up them and do a yarn install.
make npm publish do basically a npm publish.
All in container.
I'm using node:15.7-alpine
But for now, despite all my tries and research, can't make this work, i have this error :
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-02-03T11_51_48_233Z-debug.log
1
I try to follow gitlab doc.. but i'm kind of stuck right now.. What am i doing wrong ?
I managed to do it. With a lot of try and try, here are the key points.
Package name. It is very important to keep an eye on it. What the documentation does not say it's that the "package.json" need the same name as the registry.
If your registry is #foo, your registry need #foo/something as project name. With the code I provide in the script, it takes the root project, and this is the registry.
For me it's https://gitlab.com/gildraen/sushislasher-package
so my package.json name is #gildraen/sushislasher-package (though the name seems not important)
So I did it all over again from scratch, with paying more attention to this, and it worked.
I really think this was my real mistake: the registry name.

Github actions: NPM publish 404 not found

In my github project Im trying to automatically create a new version and publish it to NPM whenever something is pushed to the master branch.
The idea
Create a new minor version
Publish the package to NPM
Im using github actions. My workflow file looks like this:
# This workflow will run tests using node and then publish a package to the npm registry when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
name: Node.js Package
on:
#trigger on every commit to the main branch
push:
branches:
- main
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
- run: npm test
publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: git config user.name $GITHUB_ACTOR
- run: git config user.email gh-actions-${GITHUB_ACTOR}#github.com
- run: git remote add gh-origin https://${GITHUB_ACTOR}:${GITHUB_TOKEN}#github.com/${GITHUB_REPOSITORY}.git
- run: echo "//registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN" >> ~/.npmrc
- run: npm version patch
- run: npm publish
- run: git push gh-origin HEAD:master --tags
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
(https://github.com/ether/ep_align/actions/runs/322527776)
I keep getting a 404 error when doing the publish. Which I dont' understand because the package is online here: https://www.npmjs.com/package/ep_align
npm ERR! code E404
npm ERR! 404 Not Found - PUT https://registry.npmjs.org/ep_align - Not found
npm ERR! 404
npm ERR! 404 'ep_align#0.2.7' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
This problem is driving me nuts for a few hours now, and I have no idea what it could be.
Any ideas?
it is just an authentication issue with your token.
Have you ever set the access token in your Repository secrets ? (You might want to create environments as well.)
https://docs.npmjs.com/creating-and-viewing-access-tokens
Or it might be an issue with authorization as well. Please check your token type. It should be automation one.
Here is an example from me >
https://github.com/canberksinangil/canberk-playground
Where I have set my token under the repository settings.
Let me know if this helps :)
The NODE_AUTH_TOKEN token is attached to the wrong step, so the npm publish has no authentication. You need:
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
- run: git push gh-origin HEAD:master --tags
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
Also, make sure the case of the env variable (npm_token) here matches that in the GitHub actions settings. Environment variables are case senstive.