Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/npmrc-trusted-publishing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@changesets/action": patch
---

fix: conditionally append NPM_TOKEN to .npmrc for trusted publishing support

The .npmrc generation now intelligently handles both traditional NPM token authentication and trusted publishing scenarios by only appending the auth token when NPM_TOKEN is defined. This prevents 'undefined' from being written to the registry configuration when using OIDC tokens from GitHub Actions trusted publishing.
20 changes: 15 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
);

let userNpmrcPath = `${process.env.HOME}/.npmrc`;
const npmToken = process.env.NPM_TOKEN;

if (await fileExists(userNpmrcPath)) {
core.info("Found existing user .npmrc file");
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
Expand All @@ -79,20 +81,28 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
core.info(
"Found existing auth token for the npm registry in the user .npmrc file"
);
} else {
} else if (npmToken !== undefined) {
core.info(
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
);
await fs.appendFile(
userNpmrcPath,
`\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
`\n//registry.npmjs.org/:_authToken=${npmToken}\n`
Copy link
Member

Choose a reason for hiding this comment

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

q: were we potentially inserting :_authToken=undefined here before?

Copy link
Author

@ryanbas21 ryanbas21 Nov 26, 2025

Choose a reason for hiding this comment

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

This was my suspiscion, and why people were using a workaround of adding NPM_TOKEN: '' to publish with provenance. If the token was never defined, then this should be undefined.

I suppose there is an undefined check on the npm side, that this workaround was working for people.

);
} else {
core.info(
"No NPM_TOKEN found and no existing auth token - assuming trusted publishing or npm is already authenticated"
);
}
} else {
core.info("No user .npmrc file found, creating one");
} else if (npmToken !== undefined) {
core.info("No user .npmrc file found, creating one with NPM_TOKEN");
await fs.writeFile(
userNpmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
`//registry.npmjs.org/:_authToken=${npmToken}\n`
);
} else {
core.info(
"No user .npmrc file found and no NPM_TOKEN provided - assuming trusted publishing or npm is already authenticated"
);
}

Expand Down