NPM Publish returns error 400

Hello, fellow forum members!
Straight to the point…

I created a test repo to try publishing private NPM packages. I wanted to try it with Lerna first, that’s why the project name is ‘lerna-experiment’, but I am not able to get even a basic npm publish to work.

The repo setup is the most basic setup you can have. The entire repo consists of index.js, package.json, and .npmrc.

Contents of index.js:
module.exports = "test"

Contents of package.json:

{
  "name": "@lerna-experiment/test",
  "version": "1.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://gitlab.com/<my_username>/lerna-experiment.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://gitlab.com/<my_username>/lerna-experiment/issues"
  },
  "homepage": "https://gitlab.com/<my_username>/lerna-experiment#readme",
  "publishConfig": {
    "@lerna-experiment:registry": "https://gitlab.com/api/v4/projects/<project_id>/packages/npm/"
  }
}

Contents .npmrc:

@lerna-experiment:registry=https://gitlab.com/api/v4/packages/npm/
//gitlab.com/api/v4/packages/npm/:_authToken=<my_auth_token>
//gitlab.com/api/v4/projects/<project_id>/packages/npm/:_authToken=<my_auth_token>

And this is what happens when I try npm publish…

npm ERR! code E400
npm ERR! 400 Bad Request - PUT https://gitlab.com/api/v4/projects/<project_id>/packages/npm/@lerna-experiment%2ftest

I tried manually setting the registry URL with npm set but it had no effect. The project is not a part of any group.

What could I be doing wrong?

Thank you in advance for your answers.

1 Like

Hi, a couple of quick questions:

  • is package repository enabled for your project? (Settings > General > Visibility, project features, permission)
  • if you have 2FA active, are you using a deploy token with the right scopes?
  • did you replace <project_id> with your real project id?
  • are you using Gitlab CI?

Hi! Thanks for your willingness to help.
To answer your questions…

  1. Yes, I do have enabled the package repository for my project. It is the setting at the bottom, am I correct?
  2. I don’t have 2FA active. Or at least I am not aware of it. I created a personal access token with full access to the API. That is the token that I am passing as an auth token. Based on the fact that I am getting the 400 Error I assume that it is not a problem with authentication, because in that case, it would be 401.
  3. Yes. Trust me, I checked it many times.
  4. No, I am not using Gitlab CI.

What happens if you replace this line with:
@lerna-experiment:registry”: “https://gitlab.com/api/v4/projects/lerna-experiment/packages/npm/

BTW I’m also running into (separate) issues on publishing my first NPM package up to gitlab… wow it’s not simple!

Any progress here? Im running into similar issues.

I am also curious how you are supposed to handle spaces within group names when publishing to a group repository?

@ michalstrnad

The solution for me suggested by Gitlab support was you need to use to compile
image: node:14-slim
because I was using node latest and that image has some issues with the publishConfig.

Best regards,

@rpadovani

Struggling with this too. Any word on this? Enabled 2FA with deploy token with r/w to registry. all of my ducks are in a row. I also tried a personal token.

@enrique.fermin Using node 14.15 both locally and CI.
@philbaker did you ever solve it?

I tried looking at this guys package.json.

Thinking maybe it was some some kind of validation? 400 states

malformed request syntax, size too large, invalid request message framing, or deceptive request routing

I noticed its doing a PUT. Should it do POST for the first time?

I also noticed on this blog the guy mentions that only Silver users have access?

https://medium.com/@jon.black/using-gitlab-npm-registry-3107a4e65ee1a

I’m out of ideas. here is my package.json and .npmrc

{
  "name": "@resister/gitlab-publish",
  "version": "1.0.0",
  "main": "index.js",
  "repository": {
    "type": "git",
    "url": "git+https://gitlab.com/resister/gitlab-publish.git"
  },
  "files": [
    "index.js"
  ],
  "author": "",
  "license": "MIT",
  "homepage": "https://gitlab.com/resister/gitlab-publish",
  "publishConfig": {
    "access": "public"
  }
}
@resister:registry=https://gitlab.com/api/v4/projects/24378785/packages/npm/
//gitlab.com/api/v4/projects/24378785/packages/npm/:_authToken=$DEPLOY_TOKEN