Install multiple npm packages from private gitlab registry - node.js

I'm using gitlab to host my private npm packages. At the moment I've 2 projects I published to the gitlab package registry. Both packages are used by another project (let's say 3rd project). According to the gitlab documentation, I installed both packages in the 3rd project using the following commands:
npm config set #myscope:registry https://gitlab.com/api/v4/projects/<first-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<first-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/first-package
npm config set #myscope:registry https://gitlab.com/api/v4/projects/<second-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<second-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/second-package
My problem is now that I cannot run a simple "npm install" anymore because my 3rd project depends on the 2 private packages. Sure, I can call "npm config set" before running "npm install" in the 3rd project, but the problem is that when I call "npm config set" for the second project, it replaces the first config (because both are having the same scope). So I can only install 1 package, for the second I'm getting an error because it cannot be found (because it has another project-id in gitlab, thus another package registry url). I already tried the following without success:
npm config set #myscope/first-package:registry https://gitlab.com/api/v4/projects/<first-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<first-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/first-package
npm config set #myscope/second-package:registry https://gitlab.com/api/v4/projects/<second-project-id>/packages/npm/
npm config set "//gitlab.com/api/v4/projects/<second-project-id>/packages/npm/:_authToken" "<auth-token>"
npm install #myscope/second-package
But now I cannot install any of the packages. So, anyone know how I can set the registry-url for packages sharing the same scope but having a different url?

I finally solved the issue by using the instance-level installation. First, I thought it's not working for me because it still told me 404. But the problem was that gitlab instructs me to run npm config set '//gitlab.com/api/v4/packages/npm/:_authToken' "<my-token>". But this is not working on my Windows because of the apostrophes that surrounds the url. I replaced them with " and now everything works fine.

Related

Installing a private package from Gitlab with Yarn

I have a private npm package that is published to the Gitlab Package Registry using a Gitlab CI pipeline.
I want to install this package in a project using yarn.
Following the documentation helped me come up with the following .npmrc file :
//gitlab.com/api/v4/packages/npm/:_authToken=glpat-***********
#my-org:registry=https://gitlab.com/api/v4/packages/npm/
With the above, npm install #my-org/my-package works perfectly.
However, yarn add #my-org/my-package fails. using --verbose shows a 404 :
verbose 1.169823875 Error: https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/#my-org/my-package/-/#my-org/my-package-1.0.3.tgz: Request failed "404 Not Found"
error An unexpected error occurred: "https://gitlab.com/api/v4/projects/<my-project-id>/packages/npm/#my-org/my-package/-/#my-org/my-package-1.0.3.tgz: Request failed \"404 Not Found\""
The package does exist at version 1.0.3, NPM installs it.
I need to get this to work with Yarn. How can I do this ?
Additionaly, is there a way to take the authToken out of the .npmrc file ?
I also was not able to install it with yarn, but npm was ok.
This this worked out:
npm config set -- //gitlab.com/api/v4/packages/npm/:_authToken=XXX
npm config set -- //gitlab.com/api/v4/projects/<projectID>/packages/npm/:_authToken=XXX
npm config set #my-scope:registry https://gitlab.com/api/v4/packages/npm/
yarn config set '//gitlab.com/api/v4/projects/:_authToken' "XXX"
yarn config set '//gitlab.com/api/v4/packages/npm/:_authToken' "XXX"
And no need for .npmrc for this setup.
Here is the official GitLab docs on that issue.
Your config in .npmrc should work if you are using Yarn v1.x, as Yarn 1 uses the registries configured in this file.
However, for Yarn 2, you must configure your private registries in the .yarnc.yml at the project level:
npmScopes:
my-org:
npmRegistryServer: "https://gitlab.com/api/v4/projects/my-project-id/packages/npm/"
npmAlwaysAuth: true
npmAuthToken: glpat-*******
Replace my-org with the scope of your package, my-project-id with the numeric id of your Gitlab project, and put your token with scope "api".

NPM default registry config to a private repo doesn't work

On my local MacOs, I have set registry npm config to https://<myrepo>.pkgs.visualstudio.com/_packaging/<myrepo>/npm/registry/ in both the project directory .npmrc beside package.json and also in $HOME\.nmprc.
But still, when I run npm i it doesn't replace https://registry.npmjs.org with the one configured above.
What else I am missing to config?
EDIT:
I realized that the problem is with dependency packages.
When I put .npmrc file in the same directory as package.json, running npm install will use the registry in npmrc.
You need to check if the package-lock.json file was generated during your previous npm install.
If yes, you could remove this file and run the npm install again.
Here is a doc about Package-lock.json. And the doc about configure the .npmrc on Mac/linux machine
On the other hand, you can also try to use the following command to set the registry:
npm config set registry feedurl

npm install Error: rollbackFailedOptional

When I try npm install new packages it shows me this error:
rollbackFailedOptional: verb npm-session 585aaecfe5f9a82
node --version
8.4.0
npm --version
5.3.0
# first this
> npm config rm proxy
> npm config rm https-proxy
# then this
> npm config set registry https://registry.npmjs.org/
solved my problem.
Again: Be sure to check whether you have internet connected properly.
Try this
npm config rm proxy
npm config rm https-proxy
In my case I had to edit the .npmrc directly and add the proxy settings manually.
proxy=http://yourorganizationproxy.com:8080
https-proxy=http://yourorganizationproxy.com:8080
Hope this helps someone.
The cause for this might be your current NPM registry. Try to check for a .npmrc file. These can be at various locations:
per-project config file (/path/to/my/project/.npmrc)
per-user config file (~/.npmrc)
global config file ($PREFIX/etc/npmrc)
npm builtin config file (/path/to/npm/npmrc)
Within these there can be something like
registry=https://mycustomregistry.example.org
which will take priority over the default one (http://registry.npmjs.org/). You can delete this line in the file or use the default registry like that:
npm <command> --registry http://registry.npmjs.org/
Most likely to be npm registry cannot be reached by npm. Check npm proxy configuration
I had exactly the same issue on Windows Server 2008 R2. I suspected Internet Explorer's Enhanced Security Configuration at first but after turning that off with no success the issue turned out to be that npm was not configured to use my corporate proxy connection to the internet.
It turns out that npm does not use the proxy settings in effect via Internet Options > Connections tab > LAN settings where the server is set to 'Automatically detect settings'. Being set to automatically detect settings does not guarantee that a proxy is indeed being used, it just means that Windows will automatically configure proxy settings for Internet Explorer if it finds a special'wpad.dat' file at http://wpad.[yourdomain.com]/wpad.dat.
You can test whether a wpad.dat file is in use in your organisation by typing the following into a web browser.
http://wpad.[yourcompany.domain]/wpad.dat
If no file is available then it is likely you are not using an organization-wide proxy. If one does get returned to the browser then...
Toward the bottom of this file, you should see a line saying
PROXY <host:port>;
It might be repeated if you have multiple proxies available. The host and port are needed in order to tell npm to use the proxy settings like so:
npm config set proxy http://[host]:[port]
and
npm config set https-proxy http://[host]:[port]
For example if your proxy is at my.proxy.com on port 8080 then the npm commands would be:
npm config set proxy http://my.proxy.com:8080
npm config set https-proxy http://my.proxy.com:8080
Once I had told npm which proxy to use all started working in I was able to run the install commands without a problem.
Thanks to the following post for help with the wpad file discovery.
In my case I had my npm set registry set to https://nexus, use:
npm config delete registry
This will revert to its default state.
I tried following options to fix this issue and it worked.
Uninstall Node.js version 8.
Install Node.js version 6.11.4
Use the registry option along with command to install any package.
For example to install express I used following command.
npm install express --registry http://registry.npmjs.org/
or
npm install express -g --registry http://registry.npmjs.org/
If you want to install locally in any specific folder then use below command. Below command will install express on path C:\Sample\Example1 .
C:\Sample1\Example1> npm install /Sample/Example1 express --registry http://registry.npmjs.org/
Note: If you are installing locally in a specific location then first go to that directory using command and then run above command. If you are not inside that directory and giving only path in command that will not work.
If you get package.json missing error then run below command before installing package locally
C:\Sample\Example1> npm init
above command will create package.json file. No need to provide any data. just hit enter.
Note: If you are behind a firewall then you may need to set a proxy.
Hi I'm also new to react and I also faced this problem after so many trouble I found solution: Just run in your command prompt or terminal :
npm config set registry http://registry.npmjs.org/
This will resolve your problem.
Reference link: http://blog.csdn.net/zhalcie2011/article/details/78726679
Make sure you can access the corporate repository you configured in npm is available.Check you VPN connection.
Else reset it back to default repository like below.
npm config set registry http://registry.npmjs.org/
Good Luck!!
The following commands resolved my issue:
npm config set proxy http://yourproxyurl.com:8080 (you need to enter your or your company proxy URL and 8080 should be replaced by your proxy port)
npm config set https-proxy http://yourproxyurl.com:8080
I had the same effect creating a react app with PhpStorm. And then at the end it just says done. Running the same command in the terminal gave me detailed errors. The project folder I've created was named react which seems to be a no-go.
Make sure your project folder is not named react.
If you have access to the registry but the error is still occurred nothing mentioned above wouldn't work. I noted that this problem is only applicable for local project's installation (i.e. if you use -g to global install everything is working fine).
What's resolved the problem for me: just remove an entry regarding a package you're going to install from a project's package.json file. After that next call to npm will work and install the package successfully.
Seem this bug is not fixed yet [1]. Some people get worked, some people not. I also get not worked.
I tried clear cache with command: npm cache verify then run install command again. I got worked.
[1]. https://github.com/npm/npm/issues/17246
I had the same issue. But it can run properly with switching from company's internal network to visitor network.
I set two system environment variables -
HTTP_PROXY = <_proxy_url_>
HTTPS_PROXY = <_proxy_url_>
This actually worked for me.
In some rarer occasions, check that the project can be built using regular npm commands. I ran into one that is configured to work with bower, so bower install <github_url> works while npm install <github_url> gives that unhelpful cryptic error message on all platforms...
While installing the Angular Command line tool (CLI), If you are getting Rollback Error then it may be occurring due to your network is connected with your Client Network or your Company Network where you are working on.
So Please try to install CLI in your public network (or Your Mobile's hotspot Network) then you would definitely get CLI installed.
Solution:
The default value of ‘proxy’ and ‘https-proxy’ key of npm config is NULL. After doing some more R & D then I tried to set the proxy setting value to above key (this solution works in my scenario).
Also, the same proxy settings need to be applied in IE browser Internet Settings >> Settings >> LAN settings.
Conclusion:
In corporate/some environment the proxy is a somewhat mandatory setting otherwise npm install will not work.
Issue on GitHub: https://github.com/SharePoint/sp-dev-docs/issues/3266
You can refer blog for detailed information
https://blogs2share.blogspot.com/2019/01/spfx-environment-setup-error.html
You can resolve the issue by looking if your network has any proxies, that is prohibiting the download process. My company's network had a firewall enabled, which was causing the issue for me. So I had to switch to an un-secure network (probably a hotspot from your mobile network), and that worked for me.
Mine was due to McAfee firewall.
It is set to Ask mode, so should have popped up a prompt to ask for internet connection, but it didn't!
Going into McAfee and (temporarily!) disabling the firewall allowed me to install.
I've already had the proxies set as described above and it was working until today. Then it turned out that now I need "http://" in front of my proxy address: "http://{proxyURL}:{proxyPort}". Then it finally worked.
Struggled with this issue for some time before figuring it out.
I'm using High Sierra (10.13.6)
Uninstalled and re-installed node and nvm multiple times - using the installer.pkg, HomeBrew, and then using the command line. IMO, the command line works the best.
I followed these steps:
1. Ran npm config ls -l
2. Checked that the value for globalconfig was $<installpath>/.nvm/versions/node/v12.16.3/etc/npmrc But when I tried to get to this path in the Terminal, it gave me No such file or directory
So I
3. created the folder etc, created the npmrc file and added this line in it.
registry = "https://registry.npmjs.org/"
I do not have the ~/.npmrc file in my $HOME
Then
4. I re-ran the npm install command.
Note that this still threw the rollbackFailedOptional: verb npm-session error, but this time it completed, though with a different error.
You could try these steps and see if it works.
For those who are curious, it threw a Response timeout while trying to fetch https://registry.npmjs.org/<package> (over 30000ms) error, so I added the timeout = "60000" to the /etc/npmrc file (as found on another Stackoverflow thread), and tried again. This worked for me.
Hope this helps!
Try this all command answered here to solve the problem https://stackoverflow.com/a/54173142/12142401
if problem persists
Do the following Steps
Completely Uninstall the nodejs checkout this answer for complete uninstallation of nodejs https://stackoverflow.com/a/20711410/12142401
Download the updated nodejs setup from their website
Install it in any drive but not on previously installed drive like if you installed in C drive then install in D,S,G Drive
Run your npm command it will completely work fine
If you use vpm as I do, try turn it off a sec, at least that's my problem
I had same problem.
I deleted files in my Mac
"/usr/local/bin/npm", "/usr/local/bin/node"
and deleted folder
"/usr/local/lib/node_modules"
and then installed the current latest feature version of node. then problem solved.
When downloaded LTS version, it shows the same problem.
So, try installing different versions of node and deleting old one.
Hi,
For people that use yarn package. Just go to the project folder that you are having this issue with. Let's say the project's folder name is chatApp:
cd chatApp
And then if you have already done yarn or yarn install then delete the node_modules folder and run the below command or if you haven't done yarn install, only run the blow command:
yarn cache clean
Once yarn has successfully cleaned the cache, run the below command:
yarn config set registry https://registry.npmjs.org/
That's it, this should fix the issue ✔️.
try this:
delete all file in folder: %APPDATA%\npm-cache\_locks
For Windows:
Run the installer again and choose to 'Repair' the installation
Worked for me
Try this. It worked fine for me
npm install /your_floder_location package_name --registry
http://registry.npmjs.org/
Below is the exact command for me for installing vue-router package in my laravel project (my project name vue_laravel)
aslam004:vue_laravel $ npm install
/var/www/html/projects/vue_laravel vue-router --registry http://registry.npmjs.org/
Good luck
use sudo before your command as
sudo npm install

How to use npm in an environment with restricted internet access

In a restricted environment where not every user has access I would like to be able to use npm offline where ever possible.
My idea is to point the global config at a shared cache directory so that power users can do installs and the dependencies will end up in the cache directory. Other users can then do npm offline installs for anything previously in the cache.
So 2 Questions:
Will this work?
Short of setting up my own local npm repository is there an easier way?
Per documentation:
npm install (with no args in a package dir)
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <name> [--save|--save-dev|--save-optional]
npm install <name>#<tag>
npm install <name>#<version>
npm install <name>#<version range>
npm i (with any of the previous argument usage)
As such, npm allows you to do:
npm install /path/to/folder/containing/ node_modules
For example: npm install ~/Downloads/http-proxy, provided that the node_modules folder resides within http-proxy.
You could set your repository up on an internal (accessible) server and direct people to download by the same name from there.
Thanks for the answers. What I've ended up doing is using https://github.com/rlidwka/sinopia
This acts as a mirror repository. I can give this process internet access and not other users. Then I set a environment variable for all users to point their npm repository at the sinopia instance.
Early days but this seem to be working well.
r3mus is right. Though, for each user it would result in some cognitive overhead and possibly management issues.
What might work better is to have a corporate hosted npm repository (as described here: http://clock.co.uk/tech-blogs/how-to-create-a-private-npmjs-repository) and then just have the users change (once) their registry settings via npm set registry http://yourregistry.com
For build servers, a reasonable strategy is to symlink the node_modules directory against an existing directory where the modules have already been installed.
e.g. my powershell script might read something like this
If (-Not (Test-Path node_modules))
{
& cmd /c mklink /d /j node_modules D:\npm-cache\node_modules
Write-Verbose "Symlinked node_modules"
}

How can I change the cache path for npm (or completely disable the cache) on Windows?

I've installed Node.js on my Windows 7 x64 development machine, the manual way:
mkdir C:\Devel\nodejs
cd C:\Devel\nodejs
set NODE_PATH=%CD%
setx /M PATH "%PATH%;%NODE_PATH%"
setx /M NODE_PATH "%NODE_PATH%\node_modules"
I've placed the main node x64 binary along with npm package manager in C:\Devel\nodejs. Works like a charm and I can update the main binary without dealing with the installer.
The only problem I can't solve is moving the cache folder. When I install a local package:
npm install express
... cache is placed under %APP_DATA%\npm-cache folder. I'd like to change it to:
C:\Devel\nodejs\npm-cache
How can I change the npm cache folder, or disable it completely?
You can change npm cache folder using the npm command line. (see https://docs.npmjs.com/cli/v6/using-npm/config#cache)
So you might want to try this command :
> npm config set cache C:\Devel\nodejs\npm-cache --global
Then, run npm --global cache verify after running this command.
You can also set an environment variable with export npm_config_cache=/path/to/cache (Unix) or set npm_config_cache=C:\path\to\cache (Win) as an alternative to npm config set (this is true for all config options in npm).
For anyone using docker you can add the env var at runtime with:
docker run -e npm_config_cache=/path/to/cache mydockerimage:tag
You can also do following:
For having cache path as you wish, for a single package while installing it:
npm install packageName --cache path/to/some/folder
For having cache path as you wish, for all the packages in package.json:
Just be in the directory where package.json is as usual and do
npm install --cache path/to/some/folder
You may not find this in npm documentation but i have tried it with npm 6 and it works.
Looks like it works since npm 5 [Refer: How to specify cache folder in npm5 on install command?
In Windows you can simply cd to the desired cache folder and do npm set cache --global
Solution
Paste the following code into npmrc file.
Location of npmrc file: C:\Program Files\nodejs\node_modules\npm\npmrc
prefix=D:\nodejs\npm
cache=D:\nodejs\npm-cache
Notes:
There is no '.' in front of npmrc
Diagrams
NPMRC file folder look like this
NPMRC Content look like this
Hope it helps. Cheers
In addition, I found that running an update command works also - for example:
npm update npm
Lastly, one can check their npm-cache directory to see if is being filled or not.

Resources