NPM: how to specify registry to publish in the command line? - npm

I'm testing a new version of our npm packages registry. I'd like to run a job in our CI server specifying a registry different of the default.
I tried to execute npm publish --registry "http://nexus.dsv.myhost/nexus/repository/npmjs-registry but it didn't work. It was published to the default registry.
How do I specify a different registry while running npm publish. It is a scoped package.

There's multiple ways to accomplish this.
use npm config to set the registry globally:
npm config set registry http://nexus.dsv.myhost/nexus/repository/npmjs
use npm config to set the registry for the package scope:
npm config set #<your scope here>:registry http://nexus.dsv.myhost/nexus/repository/npmjs
configure your package.json with a publish config:
{
...
"publishConfig": {
"registry": "http://nexus.dsv.myhost/nexus/repository/npmjs"
},
...
}
use npmrc to configure the registry
registry=http://nexus.dsv.myhost/nexus/repository/npmjs

It sounds like you have a scope-specific registry configured somewhere in your npm config.
npm will merge your global, local and CLI-provided config. But any scope-specific config will take precedence over unscoped config regardless of where each of them are defined.
For example, if you have #myscope:registry=xyz in your ~/.npmrc file, that will take precedence over --registry=abc provided on the CLI, because a scope-specific registry always overrides the unscoped registry.
However, you can also pass a scope-specific registry on the CLI itself like this:
npm publish --#myscope:registry=http://nexus.dsv.myhost/nexus/repository/npmjs-registry
Note that because of how nopt (which is what npm uses under the hood to parse the CLI options) parses freeform switches, the = sign here is required. If you use a space instead, it won't work as expected.

Related

Use custom registy, ideally from package.json

In my package.json I have
"config": {
"registry": "https://registry.example.net"
}
In order to use a local registry. If I execute yarn install --verbose though, I see yarn does not use this but uses its own registry:
Performing "GET" request to "https://registry.yarnpkg.com/ts-node/-/ts-node-8.3.0.tgz".
I managed to set the yarn registry manually
yarn config set registry https://registry.example.net
Even then, in verbose mode, it shows me that it is sending GET requests to registry.yarnpkg.com.
Also I need this to be configured so it also uses this registry on other dev machines and jenkins.
How can I get yarn to use my custom registry, ideally from a config file?

How can I configure the auth parameter when using lerna publish?

I have an .npmrc set up to publish to a private registry, but I do not want to check in auth credentials. How do I provide the _auth config when running lerna publish?
Unfortunately lerna publish does not accept the _auth flag: https://github.com/lerna/lerna/tree/master/commands/publish#options
I found out you can set npm config parameters using environment variables, so this does the trick:
npm_config__auth=AUTH npx lerna publish # yes, double underscore
This recent PR was made to lerna which exposes the _auth config through the --legacy-auth flag.
https://github.com/lerna/lerna/pull/2347/files
Usage is as follows: lerna publish --legacy-auth aGk6bW9t
This should be available in the next lerna release >3.19.0. It has not yet been released at time of writing.

Using .npmrc with npm publish actions

We have a private NPM registry to which we are publishing our packages to, and have a publishConfig section in our projects package.json file which juts contains our registry url which gets picked up by our npm publish commands:
"publishConfig": {
"registry": "xxxxxx"
}
I would like to have this registry url read out from am .nprmc file rather than the package.json.
I have tried doing this, but when using npm publish I get:
400 - Repository with ID='xxx' is Read Only, but action was 'create'!
I figured this may be because I hadnt added my user details to my npmrc, which I have now done, but the problem still remains.
Is it possible for npm publish to use the details from an npmrc specifically?
Just add the following to your .npmrc
registry=YOUR_REGISTRY

how can i get scoped npm packages form artifactory

I have a problem with pulling scoped packages from artifactory remote repository
I have watched item : [Installing scoped npm packages from Artifactory
my virtual repository name is swm-virtual-npm
I did a :
npm config set registry http://server:8080/artifactory/swm-virtual-npm
npm install #angular/core
and it fails. when I do it with gulp package it works.
I have added the org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH = true flag.
the data the request.log gives me is :
20170329113552|12|REQUEST|10.82.72.231|anonymous|GET|/swm-virtual-npm/#angular%2fcore|HTTP/1.1|302|0 20170329113552|0|REQUEST|10.82.72.231|anonymous|GET|/ui/nativeBrowser|HTTP/1.1|406|0 20170329113552|1|REQUEST|10.82.72.231|anonymous|GET|/swm-virtual-npm/#angular%2fcore/|HTTP/1.1|406|0
The problem is quite simple, you are missing the required api/npm in the URL. It should be:
npm config set registry http://server:8080/artifactory/api/npm/swm-virtual-npm
Your ~/.npmrc should look something like this:
registry=http://server:8080/artifactory/api/npm/swm-virtual-npm
_auth = ......
always-auth = true
You may want to delete all the content in your NPM remote repository caches (if there are any in what I assume is a virtual NPM repository). It is likely your attempts to access it without api/npm may have corrupted the cache.

How to reset the npm registry in global npm config

Is there any way to change or reset the global npm registry.
Appreciate your assistance.
Run the following command in your terminal to revert back to the default regsitry
npm config set registry https://registry.npmjs.org/
or
npm config delete registry
NPM CONFIG DOCS
If you are on windows, other than setting the registry, you can also delete the .npmrc file to reset the registry.
You can find this file at C:\Users\<Your User Name>\.npmrc
Sets a configuration key to a value
npm config set <key> <value> [-g|--global]
Gets the value of an existing configuration key
npm config get <key>
Deletes the key from all configuration files.
npm config delete <key>
Lists all the config settings,could be used to check for existing config entries
npm config list
Opens the config file in an editor.
npm config edit
All that would help make changes to the npm registry
Source
Hope that helps!
Yes...
You can use this
$(npm config get globalconfig)
npm config --global edit
In my experience, I had to use a private NPM registry for security reasons and while switching projects I had to change my registry and found that I was unable to! A requirement of the private registry was to be on a VPN network so to be able to connect to the private registry.
Command I was trying to run
npx sb init --builder webpack5
I was trying to install storybook in an existing project.
I tried and failed by:
npm config set registry https://registry.npmjs.org commands even with --location= set to project, user, global
setting .npmrc with npm_config_registry=https://registry.npmjs.org value in multiple locations
even yarn wouldn't change the registry and I also tried to use the yarn specific commands as well yarn config set ...
restarting my system multiple times and re-trying all of the above in different ways
Information about my environment:
OSX, NVM (Node Version Manager), using npm not yarn
What ended up fixing my problem
2. I ended up reconnecting to the VPN so that when I ran my npx ... command from above it would still download the packages through my private registry
3. rm -rf node_modules package-lock.json I removed the installed files and lockfile
4. I disconnected from my VPN and re-installed packages with the same npx ... command
5. I see now that the packages are properly pulling from https://registry.npmjs.org
For some reason NPM wanted to resolve something from the private registry I had before finally using the newly configured public registry.
Might be a bug with NPM that I do not have the experience nor time to troubleshoot though I wanted to share my experience here in hopes it will help someone with a similar experience.