.npmrc Frequently Asked Questions
The .npmrc
file is a configuration file used by the npm package manager. It allows you to customize various aspects of npm's behavior, such as setting registry URLs, specifying authentication tokens, defining proxy settings, and more.
The scope of using the .npmrc
file is to provide a way to configure npm on a per-project or per-user basis. This means that you can have different configurations for different projects or users, allowing you to control how npm behaves in each specific context.
By using the .npmrc
file, you can:
- Set the registry URL to use a different package registry, such as a private registry or a mirror.
- Define authentication tokens to access private packages or registries.
- Specify proxy settings to route npm requests through a proxy server.
- Set various other configuration options like package caching, package installation behavior, and more.
Overall, the .npmrc
file gives you fine-grained control over npm's behavior, allowing you to tailor it to your specific needs for each project or user.
Exhaustive documentation can be found at: https://docs.npmjs.com/cli/configuring-npm/npmrc
The following section will cover one of many possible configuration and usages of the .npmrc file.
The examples below are not exhaustive, but rather provide a way to get started.
Using an npm token locally
To use the .npmrc
file locally for the DHL User Interface Library code (which is a private package), you can follow these steps:
Create a new
.npmrc
file in the root directory of your project if it doesn't already exist.Add the following line to the
.npmrc
file, replacing@dhl-official
with your actual scope name:@dhl-official:registry=https://registry.npmjs.com/
This tells npm to use the default registry for your scoped packages.
The DHL User Interface Library is a private package, which means you'll need to add authentication credentials into a
.npmrc
file.For example, if you're using a private registry that requires an access token, you can add the following line to the
.npmrc
file://registry.npmjs.org/:_authToken=abc123
Replace
abc123
with your actual access token (to see how you can get an access token, visit Getting started with code).Install your scoped package using the
npm install
command as you normally would. Npm will use the configuration in the.npmrc
file to determine where to download the package from and how to authenticate if necessary.
By following these steps, you can use the .npmrc
file to configure npm to download and install your scoped private package from a private registry, while also providing any necessary authentication credentials.
Using an npm token in Jenkins
- Open your Jenkins instance and navigate to the job configuration page.
- In the "Build Environment" section, add a new "Secret text" credential with the npm token you received earlier.
- In the "Build" section, add a new "Execute shell" or "Execute Windows batch command" build step.
- In the command input, add the following line to authenticate with npm using the token:
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
Replace ${NPM_TOKEN}
with the variable name you used for the "Secret text" credential.
- Save the job configuration and run a build to test the npm token authentication.
Using an npm token in Azure Pipelines (yaml)
Configure Azure Pipelines to use the npm token
- Open your Azure DevOps project and navigate to the "Pipelines" section.
- Click on "Library" in the left sidebar and then click on "New variable group".
- Name the variable group and add a new variable named "NPM_TOKEN" with the value of the npm token you generated earlier. Save the variable group.
- Edit your pipeline YAML file and add the following lines to authenticate with npm using the token:
variables:
- group: YourVariableGroupName
steps:
- script: echo "//registry.npmjs.org/:_authToken=$(NPM_TOKEN)" > .npmrc
displayName: 'Authenticate with npm'
Replace YourVariableGroupName
with the name of the variable group you created earlier.
- Save the pipeline configuration and run a build to test the npm token authentication.
Using an npm token in Azure Pipelines (classic)
To add an .npmrc
file to your classic Azure Piplines build, follow these steps:
- Open your Azure DevOps project and navigate to the "Pipelines" section.
- Click on the pipeline you want to configure.
- Click on "Edit" to open the pipeline editor.
- Click on the "Tasks" tab and then click on the "+" button to add a new task.
- Search for "npm" in the task catalog and select the "npm" task.
- Configure the task with the following settings:
- Command: "custom"
- Working Directory: "$(System.DefaultWorkingDirectory)"
- Custom Command: "config set //registry.npmjs.org/:_authToken $(NPM_TOKEN)"
- Click on the "+" button to add another task.
- Search for "npm" in the task catalog and select the "npm" task.
- Configure the task with the following settings:
- Command: "custom"
- Working Directory: "$(System.DefaultWorkingDirectory)"
- Custom Command: "install"
- Save the pipeline configuration and run a build to test the npm token authentication.
Note: You need to add the "NPM_TOKEN" variable to your pipeline variables in order for the above to work.