npmrc-replace-env

1.2.1 · active · verified Sun Apr 19

npmrc-replace-env is a Node.js utility designed to simplify the management of npm registry configurations and authentication tokens across various projects and environments. It dynamically generates or updates a `.npmrc` file based on a user-defined `.npmrc.config` template and corresponding environment variables, which can be loaded from `.env` files. The current stable version is 1.2.1. The project follows a regular release cadence with frequent patch releases addressing bug fixes and dependency updates, alongside minor releases introducing new features like shell-style variable syntax support. Its primary differentiator is its focus on securely managing sensitive npm configuration details by preventing their exposure in version control, leveraging environment variables for token replacement. It supports both prefix-based (`NPMRC_TOKEN`) and shell-style (`$TOKEN`, `${TOKEN}`) placeholder syntax, offering flexibility for different CI/CD environments, including GitHub Actions. The tool is primarily used via its command-line interface with `npx` and requires Node.js version 24.0.0 or higher for current versions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up a `.npmrc.config` and `.env` file, then use `npx npmrc-replace-env` to generate a `.npmrc` with environment variables replaced.

mkdir my-project
cd my-project

# Create a configuration template
cat <<EOF > .npmrc.config
# .npmrc.config for my-project
@myorg:registry=https://private.registry.com/myorg
//private.registry.com/myorg/:_authToken=NPMRC_MY_ORG_TOKEN

@otherorg:registry=https://another.registry.com/
//another.registry.com/:_authToken=\${OTHER_ORG_TOKEN}
EOF

# Create an .env file with your tokens (add .npmrc to .gitignore immediately!)
cat <<EOF > .env
NPMRC_MY_ORG_TOKEN=your_private_token_12345
OTHER_ORG_TOKEN=your_other_token_67890
EOF

# Generate the .npmrc file
npx npmrc-replace-env

# Verify the generated .npmrc
cat .npmrc

# Expected output in .npmrc:
# @myorg:registry=https://private.registry.com/myorg
# //private.registry.com/myorg/:_authToken=your_private_token_12345
#
# @otherorg:registry=https://another.registry.com/
# //another.registry.com/:_authToken=your_other_token_67890

view raw JSON →