This plugin provides integration with Jenkins Pipeline by provisioning a custom .npmrc configuration for build steps like sh 'npm ...' or bat 'npm ...'. Configuration files are centrally and securely managed in Jenkins via the Config File Provider plugin. This enables managing scoped private registries, authentication tokens, proxies, and custom npm settings outside source control.
The plugin also automatically exposes NPM_CONFIG_USERCONFIG pointing to the generated .npmrc, ensuring that npm, yarn, and npx discover the configuration across subshells and scripts.
withNPM(npmrcConfig: 'my-custom-npmrc') {
sh 'npm install'
sh 'npm publish'
}
my-custom-npmrc is the identifier of an .npmrc managed configuration file defined under Manage Jenkins > Managed Files.
During execution, withNPM:
- Expands registry credentials and writes the
.npmrcfile to the workspace. - Injects the
NPM_CONFIG_USERCONFIGenvironment variable. - Automatically cleans up the
.npmrcfile when the block exits to prevent credential leaks (restoring any pre-existing workspace.npmrcif one existed prior to the step).
For monorepos or multi-module projects where JavaScript code resides in a subfolder (e.g. frontend/ or packages/client), pass the optional path parameter:
withNPM(npmrcConfig: 'my-custom-npmrc', path: 'frontend') {
dir('frontend') {
sh 'npm install'
sh 'npm test'
}
}
This plugin requires Node and NPM to be available on the agent PATH or provided via a container image:
stage('npm-build') {
agent {
docker {
image 'node:20-alpine'
}
}
steps {
withNPM(npmrcConfig: 'my-custom-npmrc') {
sh 'npm ci'
sh 'npm test'
}
}
}
| Parameter | Type | Default | Description |
|---|---|---|---|
npmrcConfig |
String | (required) | ID of the managed .npmrc file from Config File Provider. |
path |
String | "" (workspace root) |
Optional relative subfolder path where .npmrc should be written. |
cleanup |
boolean | true |
Automatically deletes the generated .npmrc upon block completion (and restores any pre-existing .npmrc). Set to false to retain the file. |