NPM Install not working with artifactory behind proxy - node.js

I have configured artifactory and npm to work behind a proxy:
npm config set registry http://myserver:8081/artifactory/api/npm/npmjs
When I call npm info bower it works fine but the tarball URL already looks suspicous: { tarball: 'http://myserver:8081/artifactory/api/npm/npmjs/registry.npmjs.org/bower/-/bower-1.3.12.tgz', shasum: '37de0edb3904baf90aee13384a1a379a05ee214c' }
npm install bower fails because of the wrong tarball URL. This is the relavant output of my npm-debug.log:
34 silly mapToRegistry registry http://myserver:8081/artifactory/api/npm/npmjs
35 silly mapToRegistry uri http://myserver:8081/artifactory/api/npm/npmjs/bower
36 verbose addRemoteTarball http://myserver:8081/artifactory/api/npm/npmjs/registry.npmjs.org/bower/-/bower-1.3.12.tgz not in flight; adding
37 verbose addRemoteTarball [ 'http://myserver:8081/artifactory/api/npm/npmjs/registry.npmjs.org/bower/-/bower-1.3.12.tgz',
37 verbose addRemoteTarball '37de0edb3904baf90aee13384a1a379a05ee214c' ]
38 info retry fetch attempt 1 at 11:39:11
39 info attempt registry request try #1 at 11:39:11
40 http fetch GET http://myserver:8081/artifactory/api/npm/npmjs/registry.npmjs.org/bower/-/bower-1.3.12.tgz
41 http fetch 404 http://myserver:8081/artifactory/api/npm/npmjs/registry.npmjs.org/bower/-/bower-1.3.12.tgz
42 error fetch failed http://myserver:8081/artifactory/api/npm/npmjs/registry.npmjs.org/bower/-/bower-1.3.12.tgz
The part registry.npmjs.org in the URL is wrong and the correct tarball URL should be this one: http://myserver:8081/artifactory/api/npm/npmjs/bower/-/bower-1.3.12.tgz. From this URL I can downlaod the tarball manually using curl.
Any ideas if I have configured something wrong or if there is a bug somewhere?
Artifactory v3.4.2, npm v2.5.1

It can happen when the remote repository that should proxy the npmjs registry is pointing on the wrong URL (e.g. http://registry.npmjs.com instead of .org).

Your Artifactory front end displays the correct string for the npm config
Does this match what your running ?
Mine looks like this
#Add this registry URL to your ~/.npmrc or use:
npm config set registry http://serverName:8081/artifactory/api/npm/npmjs
I'm just wondering if the config of the remote repo is correct in Artifactory
Another option is to try zapping the cache on the npm remote repo
Is there any info in the Artifactory system logs showing what its trying to do?

I have this issue when the package has a package-lock file with reference to npmjs.org. For example a file like this: https://github.com/mckn/gulp-nuget/blob/master/npm-shrinkwrap.json
I guess the only solution is using relative URLs in resolved field of the package-lock file.

Related

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.

App Engine: node.js dependency with git+https URL

If we deploy a node.js 12 project to App Engine (standard environment) and it has a dependency in package.json that looks like this:
"some-lib": "git+https://github.com/a/b.git",
Then this will lead to a deployment error when we execute gcloud app deploy. This is from the App Engine logs:
Step #6 - "builder": npm ERR! Error while executing:
Step #6 - "builder": npm ERR! /usr/bin/git ls-remote -h -t ssh://git#github.com/a/b.git
Step #6 - "builder": npm ERR!
Step #6 - "builder": npm ERR! Host key verification failed.
Step #6 - "builder": npm ERR! fatal: Could not read from remote repository.
So even though the dependency is provided as a HTTPS URL, it will try to fetch it with SSH and this doesn't seem to work on App Engine.
It works if I go to package-lock.json and manually change the version string to start with https, but that's not really a viable solution, because it will be overwritten again.
Any ideas how it can be accomplished to provide a dependency with a GitHub URL (SSH or HTTPS) to App Engine?
Check first your git configuration in the deployment environment (Google App Engine, in your Google Cloud Console), assuming Git 2.26 or more:
git config --global --show-origin --show-scope-l
git config --system --show-origin --show-scope -l
# if you are in a local repository:
git config --local --show-origin --show-scope -l
Look for any configuration like url."ssh://git#github.com".insteadOf "git+https://github.com"
Such a rule would rewrite the URL, explaining why your initial dependency shows up as an SSH URL.
As we can see in this issue the problem relies in NPM that caused to convert any git+https reference into git+ssh.
From what I understood, the issue has been patched but is not available on NPM 6, that is the version used in the nodejs12 and nodejs14 enviroment of GAE.
What I suggest is to use nodejs16 as runtime of your App Engine application

Scoped NPM for private repository converts slash "/" to "%2F"

I would like to fetch private npm from private repository using scopes.
I have set the .npmrc as such.
registry=https://registry.npmjs.org/
#myco:registry=https://nexus.myco.com/nexus/repository/
I have set the .yarnrc as such.
registry "https://registry.npmjs.org/"
"#myco:registry" "https://nexus.myco.com/nexus/repository/"
But when I do:
yarn --verbose add #myco/some-private-npm
It throws this error:
verbose 0.708 Performing "GET" request to "https://nexus.myco.com/nexus/repository/#myco%2fsome-private-npm".
verbose 0.792 Request "https://nexus.myco.com/nexus/repository/#myco%2fsome-private-npm" finished with status code 404.
When I do:
yarn --verbose add #myco:some-private-npm
It goes to this 400 url (Nexus: Invalid repository path):
verbose 0.957 Request "https://nexus.myco.com/nexus/repository/#myco:some-private-npm" finished with status code 400.
The actual npm is located at:
https://nexus.myco.com/nexus/repository/myco/some-private-npm
How do I make sure the url fetched does not have "#" and "%2f" is a "/"?
Thanks!
Per this github issue for yarn, try adding similar config to your .yarnrc:
registry "https://registry.npmjs.org/"
"#myco:registry" "https://nexus.myco.com/nexus/repository/"
Failing that, I recommend perusing the linked issue and trying the solutions provided.
I run into exact the same issue some time ago.
check at NPM if your repository team access level is read/write.
On my side the team i was in had only read access level. Switching solved the issue.
Another possible scenario is that the slash encoding isn't really the problem. You simply do not have access to that specific repository.
That would explain the 404, since if you don't have access, for security reasons, it's as if it doesn't exist, you don't get a 401.
I found this page when also trying to run npm publish of a .tgz file in Github Actions, and getting the following (replaced scope and package):
npm ERR! 404 Not Found - PUT https://registry.npmjs.org/#SCOPE%2fPACKAGE - Not found
npm ERR! 404
npm ERR! 404 '#SCOPE/PACKAGE#0.0.5' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
This package IS published so I thought the %2f was causing the issue. I was using NPM_TOKEN as an environment variable as well, as per npmjs documentation.
Solution:
- name: Setup Node.js environment
uses: actions/setup-node#v2.1.2
with:
node-version: '12'
registry-url: 'https://registry.npmjs.org'
- name: Publish to npmjs
run: |
npm install
npm run pack
npm publish <package tgz> --access public
env:
NPM_TOKEN: ${{ secrets.NPM_APIKEY }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_APIKEY }}
via Github Documentation.
NOTE: I'm not sure NPM_TOKEN is still required here.
Make sure .yarnrc actually contains
registry "https://registry.npmjs.org/"
and not
registry "registry.npmjs.org"
That'll give the same error.

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.

Resources