How to fix npm ERR! code EINVALIDTYPE npm ERR! Argument #1: Expected object but got string in npm
The EINVALIDTYPE error is an internal npm validation failure that occurs when npm receives data in an unexpected format. This is typically caused by corrupted lockfiles or npm version incompatibilities.
Quick fix
Fixes npm ERR! code EINVALIDTYPE npm ERR! Argument #1: Expected object but got string
# Remove lockfile
rm package-lock.json
# Reinstall dependencies
npm install- Delete package-lock.json and reinstall
- Clean install with node_modules removal
- Upgrade Node.js and npm
- Validate package.json structure
# Remove lockfilerm package-lock.json# Reinstall dependenciesnpm installnpm ERR! code EINVALIDTYPEnpm ERR! Argument #1: Expected object but got string
What this error means
The EINVALIDTYPE error indicates that npm's internal dependency resolver received a string value where it expected an object. This is a type validation failure within npm itself, not a problem with your code. This error was more common in older npm versions (v3.x–v5.x) but can still occur when package-lock.json becomes malformed or when there's a mismatch between the lockfile version and the npm version reading it. The error occurs during dependency parsing when npm cannot properly interpret the structure of dependency metadata.
Typical symptoms
- npm install fails immediately with EINVALIDTYPE error
- Error message references "Argument #X: Expected object but got string"
- Error occurs during npm install or npm update
- Issue persists even after deleting node_modules
- Works after Node.js upgrade but fails after downgrade
- Error messages may reference internal npm files like deps.js
Common causes
- Corrupt or incompatible package-lock.json file
- Lockfile version mismatch (lockfileVersion 0/1 vs newer npm)
- Node.js/npm version incompatibility especially with older versions
- Malformed package.json with empty dependency entries
- npm internal bug in older versions (v3.1.1–v5.x)
- Manual edits to package-lock.json corrupted the structure
How to fix "npm ERR! code EINVALIDTYPE npm ERR! Argument #1: Expected object but got string"
The most effective fix is removing the corrupted lockfile:
# Remove lockfile
rm package-lock.json
# Reinstall dependencies
npm installThis regenerates the lockfile in a format compatible with your npm version.
For a complete reset:
# Remove both lockfile and installed modules
rm -rf node_modules package-lock.json
# Clear npm cache
npm cache clean --force
# Reinstall
npm installThis error is much rarer in modern npm versions:
# Check versions
node -v
npm -v
# Upgrade npm to latest
npm install -g npm@latest
# Or upgrade Node.js entirely
nvm install --ltsRecommended: Node.js 18+ with npm 9+
Check for malformed dependency entries:
{
"dependencies": {
"express": "^4.18.0",
"": "^1.0.0" // ← Invalid! Empty key
}
}Remove any:
- Empty string keys ("")
- Null or undefined values
- Objects where strings are expected
Ensure package.json is valid JSON:
# Check JSON validity
node -e "require('./package.json')"
# Or use jq
jq . package.jsonFix any syntax errors reported.
Advanced notes
This error is increasingly rare in npm v6+ and v7+ due to improved validation and stricter lockfile handling. If you encounter it frequently, upgrading npm is the most reliable long-term solution.
In CI/CD environments, ensure the Node.js version matches what was used to generate package-lock.json. Version mismatches between local development and CI runners are a common source of this error.
For React applications, this error sometimes appears with react-scripts. Upgrading both npm and react-scripts together usually resolves it.
Frequently asked questions
- What does "npm ERR! code EINVALIDTYPE npm ERR! Argument #1: Expected object but g" mean?
- The EINVALIDTYPE error indicates that npm's internal dependency resolver received a string value where it expected an object. This is a type validation failure within npm itself, not a problem with your code. json becomes malformed or when there's a mismatch between the lockfile version and the npm version reading it.
- How do I fix "npm ERR! code EINVALIDTYPE npm ERR! Argument #1: Expected object but g"?
- The EINVALIDTYPE error is an internal npm validation failure that occurs when npm receives data in an unexpected format. This is typically caused by corrupted lockfiles or npm version incompatibilities.
- What causes "npm ERR! code EINVALIDTYPE npm ERR! Argument #1: Expected object but g"?
- Corrupt or incompatible package-lock.json file Lockfile version mismatch (lockfileVersion 0/1 vs newer npm) Node.js/npm version incompatibility especially with older versions
- Is the "npm ERR! code EINVALIDTYPE npm ERR! Argument #1: Expected object but g" 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
- 🐙GitHub #8904: npm/npm(GitHub)
- 🐙GitHub #18128: npm/npm(GitHub)
- 💬Stack Overflow: after putting node 5 npm err code einvalidtype(Stack Overflow)