1.8. Editing the npm configuration files

It is possible to set some settings when working with npm. The npm settings can be set using command line arguments, environment variables or one of the available npm configuration files. There are four npm configuration files available:

  • per-project configuration file (/path/to/my/project/.npmrc)
  • per-user configuration file (~/.npmrc)
  • global configuration file ($PREFIX/etc/npmrc)
  • npm built-in configuration file (/path/to/npm/npmrc) You can use the npm config command to add settings to the .npmrc file. For example, if you try to use npm, while you are connected to a network behind a corporate proxy you will not be able to install packages.

How to do it…

  1. To resolve this issue, you have to set the proxy configuration using the npm config command:
    $ npm config set proxy http://proxy.company.com:8080
    $ npm config set https-proxy http://proxy.company.com:8080
    
  2. If you need to specify credentials, they can be passed in the URL using the following syntax:
    http://user_name:password@proxy.company.com:8080
    
  3. The npm config command is actually adding those settings to your user’s .npmrc configuration file. The npm config command includes an option to open the .npmrc file in a text editor:
    $ npm config edit
    
  4. The .npmrc file contains a list of key and values. Each key-value pair is in a new line following format:
    key = value
    
  5. The key-value pairs can also access environment variables using ${VARIABLE_NAME} as follows:
    prefix = ${HOME}/.npm-packages
    
  6. You can also indicate that a key contains an array of values by adding “[]” after the key name. The values of the array can then be declared using multiple lines:
    key[] = "first value"
    key[] = "second value"
    

There’s more…

You can also perform following actions on .npmrc:

  • If you want these setting to be stored in the global .npmrc file you just need to run the commands using the -g or –global flags:
    $ npm config set proxy http://proxy.company.com:8080 -g  
    $ npm config set https-proxy http://proxy.company.com:8080 -g
    
  • You can use the npm config command to set, get or delete settings:
    $ npm config set some_key some_value [-g|--global]  
    $ npm config get some_key  
    $ npm config delete some_key
    
  • It is also possible to invoke the get and set commands directly:
    $ npm get some_key
    $ npm set some_key some_value [-g|--global]
    
  • You can use the following command to see all the settings in your development environment:
    $ npm config list
    

Shiv Kushwaha

Author/Programmer