Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to npm config save into project .npmrc file?

Tags:

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.

like image 822
Mike Graf Avatar asked Jan 05 '15 21:01

Mike Graf


People also ask

How do I save a .npmrc file?

Show activity on this post. Open in it in (for first time users, create a new file) any editor and copy-paste your token. Save it. You are ready to go.

How do I find npm config file?

Run npm config ls -l to see a set of configuration parameters that are internal to npm, and are defaults if nothing else is specified.

How do I edit Npmrc files?

The config setting for npm is gotten from the command line, environment variables and the npmrc files. You can use the npm config command to update and edit the contents of the user and global npmrc files. Npmrc has four relevant files, they are: The per-project config file (/path/to/my/project/.


2 Answers

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
    
like image 159
mklement0 Avatar answered Oct 08 '22 09:10

mklement0


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

like image 42
Ortomala Lokni Avatar answered Oct 08 '22 09:10

Ortomala Lokni