Publishing a NPM Package to npmjs.com from GitLab CI

To be able to more easily release packages to the NPM registry, we likely want to have our CI/CD process publish packages on tagged versions.

As I use GitLab CI for personal projects, I wanted to set this up, and through a mix of Eleanor Holley's article and a bit of brute force, I've managed to be happy with the following setup:

image: node:16

stages:
  - test
  - publish

cache:
  paths:
    - node_modules/

test:
  script:
    - npm ci
    - npm run lint
    - npm run test
    # any other steps

publish:
  stage: publish
  only:
    - tags
  script:
    - npm run ci-publish

Now, the biggest gotcha I had was that my NPM_TOKEN wasn't being registered, and so CI builds were failing with:

Example ENEEDAUTH errors
npm ERR! code ENEEDAUTH
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-12-23T09_49_35_606Z-debug.log

I'd thought it was the way I was doing it (by editing the .npmrc) so I moved over to ci-publish, but it turns out it was because GitLab CI's protected mode for CI/CD variables requires the tags to have protection, too.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #nodejs #gitlab-ci.

This post was filed under articles.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.