How to pass inline options when running npx? - npx

Without changing the global npm configuration of the workstation or creating an .npmrc file in the current directory, I want to run an npx package using a private nexus registry.
So, is it possible to run npx commands (preferably cross-paltform) with inline options like registry and strict-ssl? Something like:
npx somerunnablepackage --registry https://mynexus --strict-ssl false
I tried the snippet above on PowerShell/Windows, but my registry was not hit and it tried to reach the default registry (https://registry.npmjs.org) anyway.

Use
npm_config_registry=https://mynexus npm_config_strict_ssl=false npx somerunnablepackage
See https://docs.npmjs.com/misc/config

Related

How to fix 404 error when installing npm package from GCP artifact registry with yarn?

I'm having an issue with installing an NPM package from GCP.
I was able to upload the package to the artifact registry of GCP by doing the following steps:
Login to my google account (gcloud auth application-default login)
Run
gcloud artifacts print-settings npm \ --project=[my-project]\ --repository=[my-repo] \ --location=us-east1 \ --scope=#[my-scope]
Pasting the output of the previous step in the .npmrc file located in the root of the project.
Refreshing the access token to GCP (npx google-artifactregistry-auth ./.npmrc)
Run yarn publish
My .npmrc file looks like this:
#[my-scope]:registry=https://us-east1-npm.pkg.dev/[my-project]/[my-repo]/
//us-east1-npm.pkg.dev/[my-project]/[my-repo]/:_authToken="[auth-token]"
//us-east1-npm.pkg.dev/[my-project]/[my-repo]/:always-auth=true
However, when I try to install the package on another project by:
Executing steps 1-4 mentioned above
Run yarn add #[my-scope]/[my-package]
I get an 404 error.
Looks like yarn is looking for the package in the default registry:
error An unexpected error occurred: "https://registry.yarnpkg.com/#[my-scope]/#[my-pacakge]/-/#[my-scope]/[my-package]-0.0.1.tgz: Request failed \"404 Not Found\"".
I simply followed the steps mentioned in the installation instructions in GCP but somehow it's not working.
I encountered a similar issue in this post: Can't install a scoped package I published to a npm registry in GCP but this not the exact error I get.
I would appreciate any help regarding this issue.
Thanks in advance!
I just had this problem for a couple of days and the solution is simple, DO NOT USE YARN when publishing. That's it.
I don't know which part of yarn causes this but basically it ignores .npmrc resulting in the tarball to point to the wrong repository, you can check it if you run yarn info. So when publishing to GCP artifact registry one should use npm publish instead.
In both setting up authentication for npm and Managing Node.js packages, Obtaining an access token section the command used is
npx google-artifactregistry-auth
In the same section there is a note that explains how to add flags if you need to change the path of the .npmrc file.
Note: If you need to store your repository settings and credentials in .npmrc files other than the defaults, you can run the credential helper with additional flags.
--repo-config is the .npmrc file with your repository settings. If you don't specify this flag, the default location is the current directory.
--credential-config is the path to the .npmrc file where you want to write the access token. The default is your user .npmrc file.
Instead of:
npx google-artifactregistry-auth ./.npmrc
It could be written as
npx google-artifactregistry-auth --repo-config=pathto/.npmrc --credential-config=pathto/.npmrc
If you are not sure where your file is you can run npm config ls -l | grep config as explained here
Also check you are specifying the correct .npmrc path if it is different than the default registry as shown in Configuring npm and confirm you are trying to install a package from the Node.js package repository with the correct scope, package, tag or version to be completely explicit.

NPX create-react-app creating other folders

So whenever i use
npx create-react-app my-app
it generates outside the folder "my-app" a lot of others folders such as _locks, _npx, _cacache and a json file named anonymous-cli-metrics.
I tried changing the cache location, but I couldn't do it, I want to use the command without creating these files does anyone know how to solve it?
I've also had this same problem. You should install your nodejs on your c:/ directory. Uninstall nodejs and remove cache files and remove the PATH variable and reinstall it in your C:/ directory.
Another possible problem is with chocolatey, if you've installed along with nodejs you should remove that also and do a fresh install on your root directory
Or try setting yout npm cache to,
npm config set cache C:\cache

How to use yarn with private npm registry in Sonatyoe Nexus OSS?

I ve setup the nexus oss 3 and it looks cool. All my projects are installed by using yarn because of the --pure-lockfile option.
Steps to reproduce the issue:
1. Setup nexus oss 3 with a private npm registry (as in documentation)
2. Disable anonymous access from nexus oss 3 admin panel
3. On a linux server with alpine try to yarn install --pure-lockfile (you must have a package that is hosted on the private repo in package.json)
4. Does not work, return 401 error
I tried everything but i could not manage to make yarn to login to get those packages.
If i use npm install, it works.
Can someone tell me how to make yarn work nexus oss3 using the setup from above?
If npm install is working, then you must have login credentials and repository correctly defined.
Open terminal and run npm login, give your username and password for nexus account. This will create a file ~/.npmrc. Open this file nano ~/.npmrc, output look like
//<repository>:_authToken=NpmToken.<token>
A dummy example:
//test.server.com/repository/npm-group/:_authToken=NpmToken.123456-12345-12345-tok-en0onum
Go to the project directory cd <project_dir>, create a new file .yarnrc, open it nano .yarnrc. Insert the following line, save and exit (Ctrl+O, Ctrl+X) it.
registry "<repository>"
Create another file .npmrc in the same directory <project_dir>. Open, add the following line, save and exsit.
registry=<repository>
always-auth=true
//<repository>:_authToken=NpmToken.<token>
Delete the .npmrc at home directory rm ~/.npmrc.
Now you can download node_modules with yarn or yarn install.
I had same issue with nexus 3 and use this configuration on my .npmrc file:
registry=https://your.nexus.com/repository/some-npm/
always-auth=true
/* basic-auth-token: your user:password in base64 */
_auth=<basic-auth-token>
Hope this help you!
The fact that your requests returns 401 (Unauthorized) means that you should supply credentials when connecting to Nexus.
It is far from being a nice solution but I got it working using
yarn set registry https://user:pwd#your.nexus.host/nexus3/repository/npmjs/
I use yarn 1.4.0 (release candidate). It should also work on 1.3.2, but I cannot test that because 1.3.2 has issues with HTTPS_PROXY env vars.

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.

Running npm command from expressjs?

Can i run something like: npm install passport from expressjs itself?
I want to check if i'm using a non installed module and install it before running the application.
To answer your question, yes, you should be able to check the FileSystem for specific files/folders using fs. I imagine you would need to parse your package.json and check the local node_modules or the global install path.
I would recommend a shell script to run npm install and then fire up your app from within the same script.
npm install .
node <server> &