Skip to content

Commit 3811182

Browse files
committed
Fix first publish failure for prerelease versions with npm 10+
Fixes #673
1 parent e996c6f commit 3811182

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

source/npm/util.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,24 @@ export const prereleaseTags = async packageName => {
130130
} catch (error) {
131131
throwIfNpmTimeout(error);
132132
// HACK: NPM is mixing JSON with plain text errors. Luckily, the error
133-
// always starts with 'npm ERR!' (unless you have a debugger attached)
133+
// always starts with 'npm ERR!' (npm <10) or 'npm error' (npm >=10)
134134
// so as a solution, until npm/cli#2740 is fixed, we can remove anything
135-
// starting with 'npm ERR!'
135+
// starting with 'npm ERR!' or 'npm error'
136136
/** @type {string} */
137137
const errorMessage = error.stderr;
138138
const errorJSON = errorMessage
139139
.split('\n')
140-
.filter(error => !error.startsWith('npm ERR!'))
140+
.filter(line => !line.startsWith('npm ERR!') && !line.startsWith('npm error'))
141141
.join('\n');
142142

143-
if (((JSON.parse(errorJSON) || {}).error || {}).code !== 'E404') {
143+
try {
144+
const parsed = JSON.parse(errorJSON);
145+
// Only handle E404 errors gracefully; throw all other errors
146+
if (parsed?.error?.code !== 'E404') {
147+
throw error;
148+
}
149+
} catch {
150+
// If JSON parsing fails, we can't determine the error type, so throw the original error
144151
throw error;
145152
}
146153
}

test/npm/util/prerelease-tags.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,32 @@ test('non-existent (code 404) - should not throw', createFixture, [{
6565
);
6666
});
6767

68+
test('non-existent with modern npm format (npm >=10) - should not throw', createFixture, [{
69+
command: 'npm view --json non-existent dist-tags',
70+
stderr: stripIndent`
71+
npm error code E404
72+
npm error 404 Not Found - GET https://registry.npmjs.org/non-existent - Not found
73+
npm error 404
74+
npm error 404 The requested resource 'non-existent@*' could not be found or you do not have permission to access it.
75+
npm error 404
76+
npm error 404 Note that you can also install from a
77+
npm error 404 tarball, folder, http url, or git url.
78+
{
79+
"error": {
80+
"code": "E404",
81+
"summary": "Not Found - GET https://registry.npmjs.org/non-existent - Not found",
82+
"detail": "The requested resource 'non-existent@*' could not be found."
83+
}
84+
}
85+
npm error A complete log of this run can be found in: ~/.npm/_logs/...-debug.log
86+
`,
87+
}], async ({t, testedModule: {prereleaseTags}}) => {
88+
t.deepEqual(
89+
await prereleaseTags('non-existent'),
90+
['next'],
91+
);
92+
});
93+
6894
test('bad permission (code 403) - should throw', createFixture, [{
6995
command: 'npm view --json @private/pkg dist-tags',
7096
stderr: stripIndent`

0 commit comments

Comments
 (0)