pnpm: Failed to replace env in config

Verify latest release

  • I verified that the issue exists in the latest pnpm release

pnpm version

No response

Which area(s) of pnpm are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

No response

Reproduction steps

  • Create a simple nuxt 3 repo (^3.7.0)
  • Create an .npmrc file
  • Add the following
    @fortawesome:registry = https://npm.fontawesome.com/
    //npm.fontawesome.com/:_authToken = ${FONTAWESOME_NPM_AUTH_TOKEN}
    
  • Create an .env file
  • Add the following
    FONTAWESOME_NPM_AUTH_TOKEN=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
    
  • Install fontawesome pro packages pnpm i @fortawesome/pro-duotone-svg-icons

Describe the Bug

When trying to install the packages, I’m getting the following warning.

Issue while reading "path\to\.npmrc". Failed to replace env in config: ${FONTAWESOME_NPM_AUTH_TOKEN}

Expected Behavior

According to the docs, it should read the variable properly.

Which Node.js version are you using?

16.20.2

Which operating systems have you used?

  • macOS
  • Windows
  • Linux

If your OS is a Linux based, which one it is? (Include the version if relevant)

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 11
  • Comments: 17 (5 by maintainers)

Most upvoted comments

Since other pm’s have that feature we should change that to a feature request “Support loading .env files on execution”. Especially in enterprise this is common to not have tokens in config files on employee desktops.

export the env variable defined in .env and source it will work.

.env

export NPM_AUTH_TOKEN=token

Run:

source .env.local # load the env variables

pnpm i

FWIW this is indeed not working for me either (locally):

//npm.fontawesome.com/:_authToken = ${FONTAWESOME_NPM_AUTH_TOKEN}

But this is working (see the removed curly braces):

//npm.fontawesome.com/:_authToken = $FONTAWESOME_NPM_AUTH_TOKEN

However, it does work locally (Mac OS Sequoia), but it works in reverse in GitHub Workflows… So locally I have my .npmrc set to $FONTAWESOME_NPM_AUTH_TOKEN, but on GitHub CI, I manually recreate the .npmrc with ${FONTAWESOME_NPM_AUTH_TOKEN} 😢

@zkochan PNPM seems to be working as expected here. .env files are not supported by NPM though Yarn added .env support about a year and a half ago (https://github.com/yarnpkg/berry/issues/4718 / https://yarnpkg.com/configuration/yarnrc#injectEnvironmentFiles).

I’d be curious though, is there another way of separating out secrets in .npmrc from the non-secret configuration information stored inside it? In my case, I use .npmrc to store both the NPM_TOKEN and PNPM configurations e.g. publish-branch. I would prefer to be able to check-in the .npmrc file since it has non-secret configuration settings but I can’t since it contains the NPM_TOKEN. Ideally, I’d be able to place an env file like .env or .env.pnpm (to avoid conflicts) that I can git-ignore and properly version the .npmrc.

Example:

.npmrc

//registry.npmjs.org/:_authToken=${NPM_TOKEN}

# Prevents publish from any branch other than main
publish-branch=main
# Ensures we use locally linked packages when available
link-workspace-packages=true
# Defaults to saving as workspace:*
save-workspace-protocol=rolling

.env.pnpm

NPM_TOKEN=secret_auth_token_here

Second to @muuvmuuv

I’m having the same issue on Fedora 41 w/ Zsh.

echo $FONTAWESOME_NPM_TOKEN returns the key.

The content of my .npmrc is:

@fortawesome:registry=https://npm.fontawesome.com/
//npm.fontawesome.com/:_authToken=${FONTAWESOME_NPM_TOKEN}

Also tried using _username and _password but it doesn’t matter, the env variables are not injected in the .npmrc file.

This is what I get when I run pnpm config list:

 WARN  Issue while reading "/path/to/local/repo/.npmrc". Failed to replace env in config: ${FONTAWESOME_NPM_TOKEN}
//registry.npmjs.org/:_authToken=npm_.............
__source__=project
registry=https://registry.npmjs.org/
user-agent=pnpm/9.13.2 npm/? node/v22.11.0 linux x64

Versions:

Node 22.11.0 PNPM 9.13.2

Come on guys!