简体   繁体   中英

How to npm config save into project .npmrc file?

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.

Eg, 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

您可以执行chroot ,在~/.npmrc上构建配置文件,然后将其复制到正确的位置。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM