npm publish gives "unscoped packages cannot be private"

Npm

Npm Problem Overview


I want to publish a normal, public package to npm. When I do npm publish I get:

npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! unscoped packages cannot be private : my-package

Npm Solutions


Solution 1 - Npm

It appears that (as of November 2018), you have to do:

npm publish --access public

This tells the npm registry that you want your package to be downloadable by everyone. This used to be the default, and from the documentation still should be, so probably this is just a bug in npm. There is some more, not very well written documentation about scoped/unscoped and public/private packages.

Instead of using --access, you can also add the setting to your package.json, as seen in @smnbbrv's answer below. But if I'm right an this is just a bug, you may want to just use --access as a temporary workaround.

Solution 2 - Npm

With all the credits to @mb21 and his solution there is a small addition to his answer.

The proposed

npm publish --access public

works perfectly. However it is not always possible to make it work within the CI environment, e.g. when you use semantic-release. The proper solution there would be using the very same access parameter but inside your package.jsons publishConfig (btw this also makes it easier to publish manually in the future):

{
  "name": "...",
  ...
  "publishConfig": {
    "access": "public"
  }
}

And now you can use it within CI tools or simply

npm publish

It costed me some time to figure this out, so I hope it saves some time for the future readers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmb21View Question on Stackoverflow
Solution 1 - Npmmb21View Answer on Stackoverflow
Solution 2 - NpmsmnbbrvView Answer on Stackoverflow