How to fix npm ERR! Invalid name: name cannot contain special characters in npm
This error occurs when your package name contains characters not allowed by npm. Package names must be lowercase, URL-friendly, and follow specific naming rules.
Quick fix
Fixes npm ERR! Invalid name: name cannot contain special characters
❌ Invalid: "My-Cool-Package"
✅ Valid: "my-cool-package"
❌ Invalid: "my package"
✅ Valid: "my-package"
❌ Invalid: "my@package"
✅ Valid: "my-package"
❌ Invalid: ".my-package"
✅ Valid: "my-package"- Use only allowed characters
- Fix package.json name field
- Rename your project folder
- Use scoped packages for organization names
❌ Invalid: "My-Cool-Package"✅ Valid: "my-cool-package"❌ Invalid: "my package"✅ Valid: "my-package"❌ Invalid: "my@package"✅ Valid: "my-package"❌ Invalid: ".my-package"✅ Valid: "my-package"npm ERR! Invalid name: name cannot contain special characters
What this error means
npm enforces strict naming rules for packages to ensure they work correctly across different systems and can be used in URLs. When you run `npm init` or have an invalid name in package.json, npm rejects names that don't follow these rules. Package names must: - Be lowercase only - Not contain spaces - Not start with a dot (.) or underscore (_) - Not contain special characters like @, #, $, %, &, *, etc. (except in scoped packages) - Be URL-friendly - Be 214 characters or less
Typical symptoms
- npm init fails with 'Invalid name' error
- npm install fails due to invalid package.json name
- Error mentions special characters or URL-friendly
- Publishing to npm registry fails
Common causes
- Package name contains uppercase letters
- Package name contains spaces
- Package name starts with dot or underscore
- Package name contains special characters (@#$%&*)
- Package name matches Node.js core module
- Package name is an npm reserved word
How to fix "npm ERR! Invalid name: name cannot contain special characters"
Valid package names can only contain:
- Lowercase letters (a-z)
- Numbers (0-9)
- Hyphens (-)
- Underscores (_) - but not at the start
- Dots (.) - but not at the start
Examples:
❌ Invalid: "My-Cool-Package"
✅ Valid: "my-cool-package"
❌ Invalid: "my package"
✅ Valid: "my-package"
❌ Invalid: "my@package"
✅ Valid: "my-package"
❌ Invalid: ".my-package"
✅ Valid: "my-package"Edit your package.json:
{
"name": "my-valid-package-name",
"version": "1.0.0"
}Convert your name:
- Replace spaces with hyphens
- Convert to lowercase
- Remove special characters
If you're using npm init and it's using your folder name:
# Rename folder to valid name
mv "My Project" my-project
cd my-project
npm init -yFor organization or personal namespaces, use scoped packages:
{
"name": "@mycompany/my-package"
}Scoped names follow the format @scope/package-name where both parts must follow naming rules.
Advanced notes
Reserved names: npm blocks certain names:
- Node.js core modules: fs, http, path, etc.
- npm commands: install, publish, test, etc.
- Previously unpublished names that were squatted
Name validation: Use the validate-npm-package-name package to check names:
npx validate-npm-package-name "your-package-name"Legacy names: Some very old packages have names that wouldn't be valid today (with capitals or special chars). New packages must follow current rules.
Private packages: Even if you never publish, follow naming conventions. Invalid names can cause issues with tools and scripts that parse package.json.
Frequently asked questions
- What does "npm ERR! Invalid name: name cannot contain special characters" mean?
- npm enforces strict naming rules for packages to ensure they work correctly across different systems and can be used in URLs. json, npm rejects names that don't follow these rules. ) or underscore (_) - Not contain special characters like @, #, $, %, &, *, etc.
- How do I fix "npm ERR! Invalid name: name cannot contain special characters"?
- This error occurs when your package name contains characters not allowed by npm. Package names must be lowercase, URL-friendly, and follow specific naming rules.
- What causes "npm ERR! Invalid name: name cannot contain special characters"?
- Package name contains uppercase letters Package name contains spaces Package name starts with dot or underscore
- Is the "npm ERR! Invalid name: name cannot contain special characters" error serious?
- It is a low-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚npm Official Documentation
- 📖npm init documentation(Official Docs)
- 📖package.json naming rules(Official Docs)