How to npm config save into project .npmrc file? - npm

I am hoping to run npm config to set values in a project's .npmrc file.
Docs dont seem to say how to specify a file to save the values into.
Looking for something like
npm config --file /path/to/repo/.npmrc set key value
Trying to use it for a build script that needs to build a .npmrc file from env vars.

It is baffling that this is seemingly still not directly supported as of npm v6.9.0.
It's a bit awkward, but if your npm version is recent enough, you can repurpose the --userconfig option to make npm config operate on a project-specific .npmrc file.
E.g., from a given project's root folder, the following command project-locally configures pwsh (PowerShell Core) as the shell to run scripts with, by making npm config operate on the local .npmrc file via --userconfig:
# Updates .npmrc in current dir
npm config set script-shell pwsh --userconfig .npmrc
Note: As Kerry Johnson points out:
The target file is rewritten as a whole, in the course of which any comments (#-prefixed lines) in the original file are lost (but all key-value pairs are preserved).
The config keyword is optional in this case, so the following will do:
npm set script-shell pwsh --userconfig .npmrc

You can do a chroot, build your config file on ~/.npmrc and then copy it on the right location.

you can override any property which could be set by the .npmrc
by passing it through command line.
For example --package-lock false will turn off package-lock generation.

Related

npm install always uses artifactory registry and ignores .npmrc file

My .npmrc file previously contained registry=https://company.jfrog.io so upon npm install this registry was used. Now I want to use the public registry for my personal project, but even though I delete the .npmrc file and even create one at the root of my project containing the public registry, this custom registry is always used to install dependencies!
When i run npm config ls -l it displays registry = "https://registry.npmjs.com/" . But when i remove the lockfile and npm install, it still uses the artifactory registry!
How can I just use the public npm registry?
If you change from one registry to an other you need to make sure, that you do following things:
Check the current registry with npm config list command. The command should be called from the root directory of the project, as a local .npmrc can overwrite the registry settings. If the listed registry entry is not ok, please update the global and/or the local .npmrc file.
Delete node_modules in your project.
Remove package-lock.json in your project.
Clean the npm cache by deleting the npm-cache folder manually. On Windows it is by default on %appdata%\npm-cache location. As an alternative npm cache clean --force command could also work, but if you want to be sure, do the manual delete.
In your case most probably the last step is missing, the cleaning of the npm cache. If the npm cache is not cleaned, npm install gets the already downloaded packages from the cache and writes their registry url into the newly generated package-lock.json. And as the cached packages are still from https://company.jfrog.io, the urls in the generated package-lock.json will still point to https://company.jfrog.io.

npm config list - multiple config

i have created a project using npm init and i created a .npmrc file with some config. when i check the npm config list i am getting project config and userconfig.
do i need to remove the userconfig , but i am not able to edit the file.
i am not understanding why we need the .npmrc file , when you hit a command npm install express , all the config is done in the npm config file ?
how to edit the .npmrc global file in ubuntu ?
do we need to remove the userconfig if we need to use project config ? or its something like which ever comes it takes that
By Searching , i found some infos
when you install npm you have a global config file, whenever you need to access the global config file you can just add -g command
example : npm config list -g
for every projects you can create a .npmrc file, in that file you can configure and you will have private kind of stuff like the packages which are hosted private
so for that you need to create a .npmrc file , if you check npm config list you will have both configs (.npmrc in project folder) and (.npmrc in global)
so its like first it will check with the config in the project folder, if there is no config for project folder it will check for the global config(userconfig)
no need to remove the global config , it will take first comes first serve so the project folder config will be taken if its there otherwise only it will check for the global config
Editing the Global config file
you can edit the global config file by going to
cd /
cd home/"username"/
vi .npmrc
change the file save it by using press esc type :x

Replicating `npm pack` behavior

I am trying to replicate the behavior of npm pack because it has a limitation where it does not write to stdout, it can only write to a local file (see this issue: https://github.com/npm/npm/issues/12039)
Now, I can tar the current directory and write to stdout like so:
tar --exclude='./node_modules/' -cv . | gzip > archive.tar.gz
however, when you extract npm pack tarballs, all the contents of a your package should be in a directory called 'package'.
So my question is - how can I tarball the current directory, but put the current directory inside a directory called 'package' before it gets archived?
Is there some tar -flag that lets you do that?
I did some legwork and as far as my testing goes, npm will accept a tarball with everything in the root, or everything in a subdirectory called 'package'.
To test the above theory, you can tar a NPM project directory with:
tar --exclude='node_modules' -c . > archive.tar
then install it somewhere else with
npm install /path/to/archive.tar
you can't install in the same project though, NPM will complain about circular deps, so install it in another project.

Pass npm config in a different way than .npmrc

For some reason related to the tools I'm using I can't use .npmrc file. There is another way to pass configuration to npm?

Installing Bower globally using npm

I am trying to install Bower globally and it is installed in the directory of
C:\Users\{{user}}\AppData\Roaming\npm\node_modules\bower
I have added the system path of Path and NODE_PATH to that of above, however when running bower, it still cant find the modules.
the prefix of npm-config is:
C:\Users\{{user}}\AppData\Roaming\npm
however later within the the list it states it as
"C:\Program Files (x86)\nodejs" (overridden)
Ive run out of ideas of what it could be,
If you using npm, which looks like you are, then use the global flag (run as admin)
npm install -g bower
Sometimes another version or just a wrong path is referenced in the npm config file instead of the installed version.
This may cause node/npm to misplace global modules.
To check and fix:
In cmd line type: npm config list
You should get a list of configuration values, one of them is prefix.
Make sure the path in prefix is the same path (only without node.exe) as the actually installed node.exe path.
(this path is listed further down as node bin location)
If it's not, change it:
Either in the config file (in your user folder, named .npmrc)
Or, via cmd line: npm config set prefix "C:\Program Files\nodejs" (change path if needed)
Reinstall the module/package you tried to install, don't forget -g for global.