How to fix npm ERR! code EUNKNOWNTYPE npm ERR! Unknown type: expected string but got number in npm
EUNKNOWNTYPE occurs when npm encounters a type validation failure, typically when dependency versions in package.json are specified as numbers instead of strings. Fix the package.json to use string version values.
Quick fix
Fixes EUNKNOWNTYPE
{
"dependencies": {
"react": 18,
"lodash": 4.17
}
}- Validate your package.json syntax and types
- Check for empty or malformed dependency entries
- Clean and reinstall dependencies
- Resolve merge conflicts in package.json or package-lock.json
{"dependencies": {"react": 18,"lodash": 4.17}}npm ERR! code EUNKNOWNTYPEnpm ERR! Unknown type: expected string but got number
What this error means
The npm EUNKNOWNTYPE error occurs when npm encounters a type validation failure, typically when dependency versions in package.json are specified as numbers instead of strings, or when the package.json structure contains invalid type values. This error indicates that npm's type system has encountered data in an unexpected format - for example, a numeric version like 4 instead of the required string format "^4.0.0". The error can also occur when the 'type' field contains invalid values other than the two permitted values: "commonjs" or "module".
Typical symptoms
- npm install fails with 'ERR! code EUNKNOWNTYPE' before any packages are fetched
- Error message includes 'Unknown type: expected string but got number'
- Installation halts immediately during dependency resolution phase
- Problem persists even after clearing npm cache
- Works in one environment but fails in another (suggesting package.json corruption)
Common causes
- Dependency version specified as a number instead of a string (e.g., 'react': 18 instead of 'react': '^18.0.0')
- Empty dependency key with no version value (e.g., '': '^5.3.1' in dependencies)
- Invalid value in package.json 'type' field - not 'commonjs' or 'module', or using wrong case
- Corrupted or malformed package-lock.json with type mismatches between versions
- Merge conflicts in package.json or package-lock.json that weren't properly resolved
- Package.json created or edited by a non-standard tool that doesn't properly serialize JSON types
How to fix "npm ERR! code EUNKNOWNTYPE npm ERR! Unknown type: expected string but got number"
Open package.json in a text editor and verify all dependency versions are strings (quoted), not numbers.
Incorrect:
{
"dependencies": {
"react": 18,
"lodash": 4.17
}
}Correct:
{
"dependencies": {
"react": "^18.0.0",
"lodash": "^4.17.21"
}
}Also check for the 'type' field - if present, it must be exactly 'commonjs' or 'module' (lowercase).
Search your package.json for empty dependency keys or unusual entries.
// WRONG - Empty key:
{
"dependencies": {
"": "^5.3.1",
"express": "^4.18.0"
}
}
// CORRECT:
{
"dependencies": {
"express": "^4.18.0"
}
}Use your editor's search function to find any empty quotes ("") or validate with a JSON linter.
Remove both node_modules and lock file, then reinstall from scratch:
# Remove node_modules directory
rm -rf node_modules
# Remove lock file
rm -f package-lock.json
# Clear npm cache
npm cache clean --force
# Reinstall all dependencies
npm installThis forces npm to revalidate and regenerate package-lock.json with correct type formatting.
If your package.json or package-lock.json has merge conflict markers, resolve them manually:
// WRONG - Contains merge markers:
<<<<<<< HEAD
"dependencies": {
"express": "^4.18.0"
}
=======
"dependencies": {
"react": "^18.0.0"
}
>>>>>>> feature-branch
// CORRECT - Choose and merge:
"dependencies": {
"express": "^4.18.0",
"react": "^18.0.0"
}After resolving conflicts, run npm install to regenerate lock files.
This error was particularly common in older npm versions. Update to the latest stable release:
# Update npm to latest version
npm install -g npm@latest
# Verify the update
npm --versionAlternatively, update Node.js which includes an updated npm:
# Using nvm (recommended)
nvm install node
nvm use nodeUse npm's built-in validation and external tools to catch issues:
# Validate package.json structure
npm ls --invalid
# Check for obvious syntax errors
node -e "console.log(JSON.parse(require('fs').readFileSync('package.json', 'utf8')))"These tools will catch type mismatches, empty keys, and other structural problems before npm tries to use them.
Advanced notes
The EUNKNOWNTYPE error typically stems from package.json being manually edited or modified by tools that don't properly serialize JSON types. The 'type' field in package.json determines how Node.js interprets .js files - 'commonjs' (default) or 'module' (ES modules). Using incorrect case ('CommonJS' instead of 'commonjs') is particularly subtle because it's valid JSON but invalid for npm's module system. In CI/CD environments, ensure your .gitignore properly excludes node_modules but commits both package.json and package-lock.json with correct formatting.
Frequently asked questions
- What does "EUNKNOWNTYPE" mean?
- json structure contains invalid type values. 0". The error can also occur when the 'type' field contains invalid values other than the two permitted values: "commonjs" or "module".
- How do I fix "EUNKNOWNTYPE"?
- EUNKNOWNTYPE occurs when npm encounters a type validation failure, typically when dependency versions in package.json are specified as numbers instead of strings. Fix the package.json to use string version values.
- What causes "EUNKNOWNTYPE"?
- Dependency version specified as a number instead of a string (e.g., 'react': 18 instead of 'react': '^18.0.0') Empty dependency key with no version value (e.g., '': '^5.3.1' in dependencies) Invalid value in package.json 'type' field - not 'commonjs' or 'module', or using wrong case
- Is the "EUNKNOWNTYPE" 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
- ๐Docs: package.json format(Official Docs)
- ๐ฌSO: npm errors(Stack Overflow)
- ๐GitHub: npm repository(GitHub)