How to fix npm ERR! Invalid name in npm
The "Invalid name" error occurs when your package.json contains a name that doesn't follow npm naming rules. Names must be lowercase, URL-safe, and follow specific conventions.
Quick fix
Fixes npm ERR! Invalid name
// WRONG
{
"name": "MyAwesomePackage"
}
// CORRECT
{
"name": "my-awesome-package"
}- Check naming rules
- Fix uppercase letters
- Replace spaces and special chars
- Fix scope format
// WRONG{"name": "MyAwesomePackage"}// CORRECT{"name": "my-awesome-package"}npm ERR! Invalid name
What this error means
npm has strict rules for package names to ensure they work correctly as URLs, directory names, and command-line arguments. The name field in package.json must conform to these rules. Invalid names often occur when copying from other platforms, using organization naming conventions, or simply not being aware of npm's restrictions.
Typical symptoms
- npm publish fails with invalid name
- npm install fails for local package
- Error mentions specific naming rule
- Package works locally but not on registry
- Scoped package name incorrect
- Name copied from another source
Common causes
- Uppercase letters in name
- Spaces in name
- Starts with dot or underscore
- Contains URL-unsafe characters
- Name too long (>214 chars)
- Name matches core Node.js module
- Invalid scope format
How to fix "npm ERR! Invalid name"
Valid npm names must:
- Be lowercase
- Not start with . or _
- Not contain spaces
- Be URL-safe
- Be under 214 characters
- Not match Node.js core modules
Convert to lowercase:
// WRONG
{
"name": "MyAwesomePackage"
}
// CORRECT
{
"name": "my-awesome-package"
}Use hyphens or underscores:
// WRONG
{
"name": "my awesome package!"
}
// CORRECT
{
"name": "my-awesome-package"
}Scoped packages use @org/name:
// WRONG
{
"name": "@myorg_mypackage"
}
// CORRECT
{
"name": "@myorg/my-package"
}Don't use Node.js core module names:
// WRONG - matches core module
{
"name": "http"
}
// CORRECT
{
"name": "my-http-utils"
}Check name validity:
# npm validates on publish
npm publish --dry-run
# Or use validate-npm-package-name
npx validate-npm-package-name "your-package-name"Advanced notes
Package names are globally unique on npm - check availability before committing to a name. Use scopes (@org/package) for organization packages. npm audit uses the package name for security tracking. Consider SEO and discoverability when choosing names. Once published, the name cannot be changed (only the entire package can be deprecated).
Frequently asked questions
- What does "npm ERR! Invalid name" mean?
- npm has strict rules for package names to ensure they work correctly as URLs, directory names, and command-line arguments. json must conform to these rules. Invalid names often occur when copying from other platforms, using organization naming conventions, or simply not being aware of npm's restrictions.
- How do I fix "npm ERR! Invalid name"?
- The "Invalid name" error occurs when your package.json contains a name that doesn't follow npm naming rules. Names must be lowercase, URL-safe, and follow specific conventions.
- What causes "npm ERR! Invalid name"?
- Uppercase letters in name Spaces in name Starts with dot or underscore
- Is the "npm ERR! Invalid name" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐npm Official Documentation
- ๐
- ๐