Mike Graf
Mike Graf

Reputation: 5317

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.

Upvotes: 28

Views: 17079

Answers (4)

Edan Kriss
Edan Kriss

Reputation: 121

This has been supported since NPM version 7.

From the docs:

location

  • Default: "user" unless --global is passed, which will also set this value to "global"
  • Type: "global", "user", or "project"

When passed to npm config this refers to which config file to use.

https://docs.npmjs.com/cli/v10/commands/npm-config#location

Upvotes: 1

mklement0
mklement0

Reputation: 440337

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
    

Upvotes: 22

Sergey Dryganets
Sergey Dryganets

Reputation: 919

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.

Upvotes: 1

Ortomala Lokni
Ortomala Lokni

Reputation: 62683

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

Upvotes: 2

Related Questions