NPM Config List: environment configs - REGISTRY - node.js

In my C:\Users\XXXXXX.npmrc file, I have the following settings;
https-proxy=http://proxy.server.com:0000
http-proxy=http://proxy.server.com:0000
strict-ssl=false
The proxy has been verified as correct and when I attempt to install an item from the Node Package Manager to a directory that contains a package.json file, I receive the following error:
npm REGISTRY json parsing error
When I attempt to see my npm configurations, I use the following command:
npm config list
and see the following settings:
;cli configs
user-agent = "npm/2.15.5 node/v4.4.5 win32 x64"
; environment configs
cache = "C:\\LOCATION"
prefix = "C:\\LOCATION"
registry = "http://another.proxy.server:0000"
; userconfig C:\Users\USERNAME\.npmrc
https-proxy = "https://proxy.server.com:0000
http = "http://proxy.server.com:0000
rejectUnauthorized=false
strict-ssl=false
;builtin config defined
;node bin location = C:\LOCATION
;cwd = C:\LOCATOIN
;HOME = C:\LOCATION
; 'npm config ls -l' to show all defaults.
I am able to change the registry using the following command:
npm config set registry http://registry.npmjs.org
However, when I run the following command:
npm config list
It still lists the other proxy as the default.
Any ideas?
UPDATE
After viewing other threads, I decided to see all of the NPM default configurations in detail by using the following command:
npm config ls -l
Once running this command, I see the following entry (which I could not see running the previous command) for the registry configuration:
; registry = "https://registry.npmjs.org/" (overridden)
Does anyone have any idea why the registry would be overridden or common applications/locations that would do such a thing?

The line that you are seeing from 'npm config ls -l' shows that the default value of registry is 'https://registry.npmjs.org/'. When you ran the command 'npm config set registry http://registry.npmjs.org', you overrode that value. You should see a line in the userconfig section of the ls output that shows that you set the 'registry' value in your user config. It also looks like the registry is being overridden by an environment variable that probably overrides both the default config and the user config (what you are setting with the 'config set' command).

Related

Loading npm registry ca certs from environment to npmrc

In order to use private npm registry I am trying to setup credentials and ca certs in project's .npmrc file .
like following:
ca[]=<ROOT_CA> # NOTE this needs to string in double quotes, with newlines replaced be \n
ca[]=<INTERMEDIATE_CA> # NOTE this needs to string in double quotes, with newlines replaced be \n
And it works absolutely fine.
But when I move these values to system environment (Mac OS .zshrc) & try to read like following :
ca[]="${NPM_REG_ROOT_CERT}"
ca[]="${NPM_REG_INTERMEDIATE_CERT}"
It gives me error like following
npm ERR! code UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm ERR! errno UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm ERR! request <complete path to dependency> failed, reason: unable to verify the first certificate
I have ensured the env values are available & everything works fine (other login credentials are also coming from environment only).
These CA certs are having issue while loading from system environment !
If I disable strict-ssl things work. It means other credentials are loading correctly from system environment .
Am I missing something here ?
Could it be related to format of CA cert string stored in system environment. I am setting it as base64 string having newline replaced with \n.
Please help.

Selfhosted Azure DevOps Agent as NetworkService npm Install problem

we have a selfhosted Azure DevOps Server with selfhosted Build Agents. The Agents are installed as a Windows Service with user NetworkService.
Our project contains a .NET Application with public and private (selfhosted Repository) NuGet Packages and an angular WebApplication.
For the NuGet Repositories we configured global environment variables HTTP_PROXY and HTTPS_PROXY. The .NET part works fine.
The npm install step does not work with the error message:
npm ERR! Cannot read property 'startsWith' of null
Seems like npm does not like the proxy settings in environment variables and no proxy settings in .npmrc file.
My question is, how can i configure npm to use proxy settings? How can i configure .npmrc file for user NetworkService?
The build log contains following messages:
metrics-registry = "https://registry.npmjs.org/" scope = "" user-agent
= "npm/6.14.13 node/v14.17.0 win32 x64"
; environment configs userconfig =
"E:\Agent_02\_work\3\npm\15146.npmrc"
; builtin config undefined prefix =
"C:\WINDOWS\ServiceProfiles\NetworkService\AppData\Roaming\npm"
; node bin location = E:\Program Files\nodejs\node.exe ; cwd =
E:\Agent_02_work\3\s\Merida.UI.Web ; HOME =
C:\WINDOWS\ServiceProfiles\NetworkService ; "npm config ls -l" to show
all defaults.
[command]C:\WINDOWS\system32\cmd.exe /D /S /C ""E:\Program
Files\nodejs\npm.cmd" install" npm ERR! Cannot read property
'startsWith' of null
As you can see the build takes a .npmrc file from path: E:\\Agent_02\\_work\\3\\npm\\15146.npmrc. Each build the file path is different.
How can i solve this problem?
Thanks in advance
I finally found out how to set global config values for user NetworkService.
I put my proxy settings and private registry config values in the following file:
C:\WINDOWS\ServiceProfiles\NetworkService\AppData\Roaming\npm\etc\npmrc
Now the build succeeds and the npm packages are installed without errors.

Define private registry in package.json

We have a private npm repository based on Sinopia
What should I define in package.json that some packages will be installed from Synopia rather then from global npm repository?
If I install it from command line I can run: npm install <package_name> --registry <http://<server:port>
P.S. tried to google and looked in official NPM documentation but have found nothing.
One of the method i know that is by .npmrc
You can also use .npmrc also inside the project
set configuration like this
registry = http://10.197.142.28:8081/repository/npm-internal/
init.author.name = Himanshu sharma
init.author.email = rmail#email.com
init.author.url = http://blog.example.com
# an email is required to publish npm packages
email=youremail#email.com
always-auth=true
_auth=YWRtaW46YWRtaW4xMjM=
auth can be generate by
username:password
echo -n 'admin:admin123' | openssl base64
output YWRtaW46YWRtaW4xMjM=
The whole point of sinopia is a private registry and a proxy at the same time. You can use uplinks install all your packages from one registry entry point. Sinopia is able to route to any registry if the local storage is not able to resolve the dependency. By default, he points to npmjs .
So, if you set your configuration like
# a list of other known repositories we can talk to
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages:
'#*/*':
# scoped packages
access: $all
publish: $authenticated
proxy: npmjs
'**':
# allow all users (including non-authenticated users) to read and
# publish all packages
#
# you can specify usernames/groupnames (depending on your auth plugin)
# and three keywords: "$all", "$anonymous", "$authenticated"
access: $all
# allow all known users to publish packages
# (anyone can register by default, remember?)
publish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs
You should be able to resolve all your dependencies independently of the source of each of them
btw: sinopia has no longer maintained.

npm warn invalid config proxy config Must be a full url with 'http://' [duplicate]

This question already has answers here:
Escape # character in git proxy password
(5 answers)
Closed 7 years ago.
Please look into it as it is not working for me:
npm warn invalid config proxy http://'Accenture\username:userpassword#2017'#127.0.0.1:8080
npm warn invalid config Must be a full url with 'http://'
Finally i got the issue resolved with the help from network guy
strict-ssl=false
proxy = http://ip address of proxy:8088
https-proxy = https://ip address of proxy:8088
registry = http://registry.npmjs.org/
These settings should go in ~/.npmrc, or in /root/.npmrc if you need to use sudo.
If you wish to change any of the configuration properties in npm, the section on config on npmjs.com is useful:
https://docs.npmjs.com/cli/config
The full proxy configuration for npm is
npm config set http-proxy http://username:password#proxy-address.com:80/'
npm config set https-proxy http://username:password#proxy-address.com:80/'
That should work for you, you can also double check it's set by typing
npm config list
http-proxy = "http://username:password#proxy-address.com:80/"
https-proxy = "http://username:password#proxy-address.com:80/"
Further to that, if you prefer to edit the config file directly it is stored in a file called .npmrc that can be added either to each project root or the global settings in your user directory, please see the npmrc help content
https://docs.npmjs.com/files/npmrc
In relation to your question, if your password contains special characters, they need to be escaped. This url provides useful information on special characters:
http://www.cyberciti.biz/faq/unix-linux-export-variable-http_proxy-with-special-characters/
As an example, if your password were P#ssword then it will become P%40ssword.

How to use a private npm registry on Elastic Beanstalk?

We have a nodejs project running on Amazon Elastic Beanstalk that uses private modules that we host using nodejitsu's private npm registry.
However getting access to the private npm registry from the elastic instances hasn't been straightforward and is not documented well.
What is the best way to get this access set up?
None of the other answers were working for me. After hours of hair pulling, we finally figured it out. The solution that worked is almost the same as the other answers but with a very minor tweak.
Set an NPM_TOKEN environment variable on Elastic Beanstalk under Configuration > Software Configuration > Environment Properties.
Create a .ebextensions/npm.config file. (The name does not have to be 'npm'.)
Put this content into the file:
files:
"/tmp/.npmrc":
content: |
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Note that it uses ${NPM_TOKEN} and not $NPM_TOKEN. This is vital. Using $NPM_TOKEN will not work; it must have the curly braces: ${NPM_TOKEN}.
Why are the curly braces needed? No idea. In shell/POSIX languages, ${VAR} and $VAR are synonymous. However, in .npmrc files (at the time of this writing), variables without the curly brackets are not recognized as variables, so npm must be using a slightly different syntax standard.
UPDATE
Also, this has worked for us only on new or cloned environments. For whatever reason, environments which were not initialized with a /tmp/.npmrc will not read it in any future deployments before running npm install --production. We've tried countless methods on 4 different apps, but cloning and replacing an environment has been the only method which has worked.
So, we managed to get this working by using the npm userconfig file. See the doc page for npmrc for more info.
When a nodejs application is being deployed to Elastic Beanstalk, the root user runs npm install. So you will need to write the root's npm userconfig file, which is at /tmp/.npmrc.
So if you add a file called private_npm.config (or whatever name you choose) to your .ebextensions folder with all the information needed, you will be good to go. See Customizing and Configuring AWS Elastic Beanstalk Environments for more info.
So here is what my file looks like to use nodejitsu private registry.
.ebextensions/private_npm.config:
files:
#this is the npm user config file path
"/tmp/.npmrc":
mode: "000777"
owner: root
group: root
content: |
_auth = <MY_AUTH_KEY>
always-auth = true
registry = <PATH_TO_MY_REGISTRY>
strict-ssl = true
email = <NPM_USER_EMAIL>
Using an .npmrc within the project also works. For example...
.npmrc
registry=https://npm.mydomain.com
You may want to .gitignore this file if you include an _authToken line but make sure you don't .ebignore it so it's correctly bundled up with each deployment. After trying a few things unsuccessfully, I came across this post which made me realize specifying it locally in a project is possible.
The answer above as a step in the right direction, but the permissions and owner did not work for me. Managed to get it to work with the following combination:
files:
#this is the npm user config file path
"/tmp/.npmrc":
mode: "000600"
owner: nodejs
group: nodejs
content: |
_auth = <MY_AUTH_KEY>
always-auth = true
registry = <PATH_TO_MY_REGISTRY>
strict-ssl = true
email = <NPM_USER_EMAIL>
Place the below within your .ebextensions/app.config.
files:
"/tmp/.npmrc":
mode: "000777"
owner: root
group: root
content: |
//registry.npmjs.org/:_authToken=$NPM_TOKEN
Where NPM_TOKEN is an environment variable with the value of your actual npmjs auth token.
Note that environment variables within elasticbeanstalk can and should be set from within the AWS console Elasticbeanstalk software configuration tab.
AWS Elasticbeanstalk Configuration
In new Elastic Beanstalk Linux 2 Platforms, none of these solutions work (apart from the .npmrc file solution that works but has its issues when using them in development evironments due to the requirements that all developers have their ${NPM_TOKEN} Env Var defined in their own environments).
The reason is that the /tmp/.npmrc location no longer works.
Option 1
You have to change the .ebextensions/npm.config file to this new format:
files:
#this is the npm user config file path
"/root/.npmrc":
mode: "000777"
owner: root
group: root
content: |
_auth= ${NPM_TOKEN}
registry = https://{yourprivatenpmrepository.com}/
Option 2
Add a custom .npmrc_{any-suffix} to the root of your app and create a prebuild hook to rename it before Beanstalk executes the npm install so that it can use your private repository configuration:
Add the following file (path from your app root) .platform/hooks/prebuild/01_set_npmrc.sh with the following content:
#!/bin/bash
#Copy and rename .npmrc_beanstalk to .npmrc
mv .npmrc_beanstalk .npmrc
Create an .npmrc_beanstalk file in your root with the following content (modify it depending on your private npm config):
_auth= ${NPM_TOKEN}
registry = https://{yourprivatenpmrepository.com}/
Chmod the hook file so that it has the necessary exec permissions when uploaded to EB: chmod +x .platform/hooks/prebuild/01_set_npmrc.sh
Re-deploy using EB CLI and you are done!
With modern platforms, you no longer need to do this via .ebextensions
You can simply create a .npmrc file at the root of your deployment package, alongside your package.json with the following line:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Using this method, you can create an environment variable named NPM_TOKEN in your AWS console so you don't have to store the token in your repo.
Structure:
~/your-app/
|-- package.json
|-- .npmrc

Resources