Skip to content

Handle npm tokens with environment variables#1211

Merged
ecraig12345 merged 4 commits intomainfrom
ecraig/npm-tokens
Apr 22, 2026
Merged

Handle npm tokens with environment variables#1211
ecraig12345 merged 4 commits intomainfrom
ecraig/npm-tokens

Conversation

@ecraig12345
Copy link
Copy Markdown
Member

@ecraig12345 ecraig12345 commented Apr 21, 2026

Add support for reading the npm token from the NPM_TOKEN environment variable, and update docs to recommend that approach. Getting the token from the environment is handled in getCliOptions for simplicity.

Pass the token through to npm using an environment variable rather than a CLI option. It's not well-documented for the specific case of auth, but the environment variable naming format is npm_config_//registry.npmjs.org/:_authToken (or other registry; trailing slash on registry is required).

Workaround for token env issues

Special characters such as / and/or : aren't valid in env var names for certain shells/platforms, which caused the token environment variable to be dropped when trying to pass it to the npm publish subprocess. Unclear whether the issue is specific to platform, shell, or the combo, but it definitely happened with Ubuntu+bash.

First issue is that running npm publish with shell: true drops the token var. Fixed by only setting shell: true on Windows.

The other issue is specific to if the parent command was run VIA yarn... Best I can tell, this is because on every yarn run ... command, yarn makes temp directories like /<temp>/yarn--1776822418161-0.7992675923334178 with aliases for node and yarn. On Linux (and Mac), the node alias looks something like:

#!/bin/sh
exec "/path/to/node" "$@"

My best guess is this extra exec step drops invalid environment variable names?? (This consistently reproed on Ubuntu+bash, but not Mac+zsh or bash. The clue was that the tests passed even on Linux when run via debugTests.js, but failed when run via yarn test.) Removing the yarn-- segment from the PATH seems to consistently fix this issue.

const npmrcContent = fs
.readFileSync(npmrcPath, 'utf-8')
.split(/\r?\n/g)
.find(line => line.startsWith(registryUrl) && line.includes('_authToken'));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it could also be _password

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In these tests using npm login against a Verdaccio registry, it always seems to use _authToken, so this should be fine.

@ecraig12345 ecraig12345 merged commit e2c6c61 into main Apr 22, 2026
10 checks passed
@ecraig12345 ecraig12345 deleted the ecraig/npm-tokens branch April 22, 2026 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants