The EINVALIDPACKAGENAME error occurs when your package name starts with a period (.) or underscore (_). Remove the leading character or use a scoped package name instead.
Fixes EINVALIDPACKAGENAME
// Invalid
{
"name": ".my-package"
}
// Valid
{
"name": "my-package"
}// Invalid{"name": ".my-package"}// Valid{"name": "my-package"}npm ERR! code EINVALIDPACKAGENAMEnpm ERR! Invalid package name: name cannot start with a dot or underscore
This error occurs when your package name begins with a dot (.) or underscore (_). npm prohibits these characters at the start of package names for several reasons: Names starting with dots are typically hidden files/directories on Unix systems (.gitignore, .env, .npmrc). If npm allowed packages with these names, they could conflict with configuration files or be accidentally hidden in file listings. Names starting with underscores were historically used for npm's internal purposes and are reserved for potential future use. The error often appears when system files like .DS_Store (macOS) or temporary directories are accidentally detected as packages during npm operations.
Remove the leading dot or underscore from your package.json:
// Invalid
{
"name": ".my-package"
}
// Valid
{
"name": "my-package"
}Or use a scoped name:
{
"name": "@myorg/my-package"
}Delete macOS system files from your project and node_modules:
# Remove from current directory and subdirectories
find . -name '.DS_Store' -type f -delete
# Remove from global npm directory
find /usr/local/lib/node_modules -name '.DS_Store' -type f -delete
# If using nvm
find ~/.nvm -name '.DS_Store' -type f -deleteAdd to .gitignore to prevent committing them:
.DS_StoreDelete npm's temporary directories that may have been left behind:
# Remove .npm-* directories from node_modules
rm -rf node_modules/.npm-*
# Remove from global directory
rm -rf /usr/local/lib/node_modules/.npm-*These directories are created during installation and should be cleaned up automatically, but sometimes remain after interrupted installs.
For Angular projects with __ngcc_entry_points__ errors:
# Remove the artifact
rm -rf node_modules/__ngcc_entry_points__.json
# Clear Angular cache
rm -rf .angular
# Reinstall
rm -rf node_modules package-lock.json
npm installIf the issue persists, do a full clean reinstall:
npm cache clean --force
rm -rf node_modules package-lock.json
npm installFor global packages:
npm cache clean --force
npm update -gScoped packages (@scope/name) technically allow the package name part to start with a dot or underscore after the slash, but this is discouraged and may cause issues with some tools.
The .DS_Store problem is particularly common when:
- macOS users commit node_modules to git
- Shared network drives are used for development
- node_modules is copied between systems
To permanently prevent .DS_Store creation in a directory:
defaults write com.apple.desktopservices DSDontWriteNetworkStores trueFor CI/CD pipelines, always use npm ci instead of npm install to ensure a clean, reproducible installation that won't pick up stray files.