npm "Invalid package name" / "Invalid Version" - Fix a Malformed package.json
npm validates the name and version fields of every package.json against strict naming and semver rules. A name with illegal characters, or a version that is not valid semver, makes npm reject the manifest outright.
What this error means
npm aborts immediately with Invalid package name or Invalid Version, naming the offending value. Nothing installs because npm cannot accept the manifest as published or as a workspace.
npm error code EINVALIDPACKAGENAME
npm error Invalid package name "My_App": name can only contain URL-friendly
npm error characters; name cannot start with an underscore.
# or
npm error Invalid Version: "v1.0"Diagnose it: reproduce the CI install locally
Install failures are usually environment drift rather than a broken lockfile: a different package-manager major, a different Node version, or a cache that is being restored from a run with different inputs. Reproduce the CI conditions before changing the lockfile, because regenerating it hides the real cause.
# match the runner exactly, then install from a clean slate
node --version && npm --version
rm -rf node_modules
npm ci --foreground-scripts
# if that succeeds locally but fails in CI, the difference is the cache
# or the package-manager version, not your lockfileCommon causes
Illegal characters or casing in name
npm names must be lowercase, URL-safe, and cannot start with a dot or underscore. Uppercase letters, spaces, or special characters are rejected.
A version that is not valid semver
A version like v1.0, 1.0, or latest is not valid semver. npm requires a full MAJOR.MINOR.PATCH (optionally with pre-release/build metadata).
How to fix it
Correct the name and version fields
Use a lowercase URL-safe name and a valid semver version.
// package.json
{
"name": "my-app",
"version": "1.0.0"
}Validate before committing
- Run
npm pkg get name versionto read the current values. - Ensure the name is lowercase, URL-safe, and not prefixed with
./_. - Ensure the version is full semver (
1.2.3), notv1.2or a tag.
Verify the fix survives a cold cache
A green run immediately after a fix often proves nothing, because it restored a cache written before the change. Force a cold install once to confirm the fix is real.
# temporarily bust the cache key to prove the fix on a cold runner
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
cache-dependency-path: package-lock.json
# then bump this suffix once, run, and remove it
# key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}-v2How to prevent it
- Keep
namelowercase and URL-safe;versionfull semver. - Use
npm versionto bump rather than hand-editing. - Lint package.json in CI before installing.