Configure Lerna to publish NPM packages to private Gitlab repo

9.8k Views Asked by At

I'm doing some tests with Lerna to create a mono-repo with a bunch of packages that I'd like to share with some applications. I've followed Lerna's documentation to setup a project and this is the file structure:

- lernatest/
    - lerna.json
    - package.json
    - packages/
        - common
            - src/
                - index.ts
            - .npmrc
            - package.json
        - models
            - src
                - index.ts
            - .npmrc
            - package.json

I need to publish the packages to a private project inside my Gitlab account. Before using Lerna, I could do it manually on each package adding a .npmrc file with the following content:

@mypackages:registry=https://gitlab.com/api/v4/packages/npm/
//gitlab.com/api/v4/projects/<PROJECT_ID>/packages/npm/:_authToken=<TOKEN>
//gitlab.com/api/v4/packages/npm/:_authToken=<TOKEN>

And in each package.json file:

{
  "name": "@mypackages/common",
  "publishConfig": {
    "@mypackages:registry": "https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/npm/"
  }
  (ETC...)
}

I don't know how to configure Lerna properly to publish the packages to my Gitlab registry. This is my lerna.json file:

{
  "version": "0.0.5",
  "packages": [
    "packages/*"
  ],
  "command": {
    "publish": {
      "registry": "https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/npm/"
    }
  }
}

But when I run npx lerna publish, I get the following error:

lerna info publish Publishing packages to npm...
lerna notice Skipping all user and access validation due to third-party registry
lerna notice Make sure you're authenticated properly ¯\_(ツ)_/¯
lerna http fetch PUT 401 https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/npm/@mypackages%2fcommon 462ms
lerna ERR! E401 401 Unauthorized - PUT https://gitlab.com/api/v4/projects/<PROJECT_ID>/packages/npm/@mypackages%2fgcommon

I've tried searching the documentation but I can't find anything helpful, any ideas?

Thanks!

1

There are 1 best solutions below

2
On

It seems that the .npmrc files in each package are ignored when publishing. I've created a single .npmrc file in the root of the project with the needed authentication data and now it works like a charm.

This is how my .npmrc file looks like:

@mypackages:registry=https://gitlab.com/api/v4/packages/npm/
//gitlab.com/api/v4/projects/<PROJECT_ID>/packages/npm/:_authToken=<TOKEN>
//gitlab.com/api/v4/packages/npm/:_authToken=<TOKEN>