cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] `npm pkg fix` does not fix invalid `scripts` warning.

Open everett1992 opened this issue 1 year ago • 1 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] I am using the latest npm

Current Behavior

When publishing a package without scripts npm will warn that the errors were corrected in the published package.json, and that they can be fixed in the source package json with npm pkg fix.

npm WARN publish npm auto-corrected some errors in your package.json when publishing.  Please run "npm pkg fix" to address these errors.
npm WARN publish errors corrected:
npm WARN publish Removed invalid "scripts"

However, running npm pkg fix does not make any changes to package.json and publishing will print the same warning.

The "Removed invalid "scripts"" warning has been discussed and closed before. I think they were closed because npm maintainers justified the warning, but the ineffective advice to run npm pkg fix has not been addressed.

https://github.com/npm/cli/issues/6918 https://github.com/npm/cli/issues/6887

Expected Behavior

npm pkg fix should add "scripts": {} to package.json, or the warning should be removed for scripts.

Steps To Reproduce

~/tmp » cat package.json
{
  "name": "tmp",
  "version": "1.0.0"
}

~/tmp » npx [email protected] publish --dry-run
npm WARN publish npm auto-corrected some errors in your package.json when publishing.  Please run "npm pkg fix" to address these errors.
npm WARN publish errors corrected:
npm WARN publish Removed invalid "scripts"
npm notice
npm notice 📦  [email protected]
npm notice === Tarball Contents ===
npm notice 42B  package.json
npm notice 41B  ws/package.json
npm notice 138B ws/ws-1.0.0.tgz
npm notice === Tarball Details ===
npm notice name:          tmp
npm notice version:       1.0.0
npm notice filename:      tmp-1.0.0.tgz
npm notice package size:  344 B
npm notice unpacked size: 221 B
npm notice shasum:        26433916c42a0bb86db80fa9b56874a71193607f
npm notice integrity:     sha512-xYYLg2OHl4Syb[...]V0h8JULEXt3Zw==
npm notice total files:   3
npm notice
npm WARN This command requires you to be logged in to https://registry.npmjs.org/ (dry-run)
npm notice Publishing to https://registry.npmjs.org/ with tag latest and default access (dry-run)
+ [email protected]

~/tmp » npx [email protected] pkg fix

~/tmp » cat package.json
{
  "name": "tmp",
  "version": "1.0.0"
}

Environment

  • npm: 10.3.0 (and 10.2.30
  • Node.js: N/A (18.19.0)
  • OS Name: Ubuntu
  • System Model Name: What
  • npm config:
; node bin location = ~/.local/share/rtx/installs/node/18.19.0/bin/node
; node version = v18.19.0
; npm local prefix = ~tmp
; npm version = 10.2.3
; cwd = ~/tmp
; HOME = ~/
; Run `npm config ls -l` to show all defaults.

everett1992 avatar Jan 11 '24 17:01 everett1992

It seems like adding scripts to fix steps would fix this issue. https://github.com/npm/package-json/blob/main/lib/index.js#L32

But npm should probably check if the normalize step that created a change is actually fixable before printing the warning.

everett1992 avatar Jan 11 '24 18:01 everett1992