Mongoose throws Invalid schema configuration when the schema is created
TypeError: Invalid schema configuration: `...` is not a valid type at path `...`.Short answer
Mongoose throws this TypeError while building a Schema, before any database call. The message names the path and the value it could not read as a type. The usual causes are a plain value used in place of a type, a type name Mongoose does not have, or an array whose element is undefined.
Fix it fast
1Find out which cause you have
Read the path in backticks in the message; it is the key that Mongoose could not turn into a type. Open that line of the schema and look at the value on the right.
node -e "require('./models/user.js')" 2>&1 | head -n 52Match what you see, then jump to its fix
What does the error look like?
TypeError: Invalid schema configuration: `...` is not a valid type at path `...`.Invalid schema configuration: Could not determine the embedded type for array `...`.Invalid schema configuration: `...` is not a valid type within the array `...`.
Cause 1 of 3
A plain value or an unknown name was written where a type belongs
1Check
In Schema.add, a key whose value is neither an object nor a function (and not the name of a Mongoose type) is rejected with this message. Writing age: 18, active: true or a misspelled type name triggers it. A typical slip is a default value written in place of the type.
grep -n -B1 -A12 "new Schema\|new mongoose.Schema" models/user.js2Fix
Give the path a real SchemaType, and move the value into a default. Use a name from the Mongoose list: String, Number, Date, Buffer, Boolean, Mixed, ObjectId, Array, Decimal128, Map, UUID, BigInt and others.
const userSchema = new mongoose.Schema({
name: String,
age: { type: Number, default: 18 },
active: { type: Boolean, default: true },
});3Verify
Load the model file again; it should run with no error.
node -e "require('./models/user.js'); console.log('schema ok')"Confirmed by mongoosejs.com
Cause 2 of 3
An array element type is undefined
1Check
The array branch of the schema parser fails when the element type resolves to undefined. That happens when the sub-schema or type you wrote inside [ ] is undefined at that moment, for example a misspelled import or a value that is not exported.
node -e "const s = require('./models/child.js'); console.log(typeof s)"2Fix
Make sure the element is defined before the parent schema uses it: export the sub-schema from its own file and import it at the top of the parent.
// child.schema.js
const childSchema = new mongoose.Schema({ label: String });
module.exports = childSchema;
// parent.js
const childSchema = require('./child.schema');
const parentSchema = new mongoose.Schema({ children: [childSchema] });3Verify
Load the parent model file.
node -e "require('./models/parent.js'); console.log('schema ok')"Confirmed by mongoosejs.com
Cause 3 of 3
The array element is not a type Mongoose knows
1Check
The element type name is looked up among the registered Mongoose types. A made-up or misspelled element type is not found.
grep -n "\[" models/*.js | grep -v "\[\]"2Fix
Use a SchemaType (String, Number, ObjectId) or a sub-schema inside the array.
const postSchema = new mongoose.Schema({
tags: [String],
authors: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
});3Verify
Load the model file and create one document in a script.
node -e "const m=require('./models/post.js'); console.log(new m({tags:['a']}).tags)"Confirmed by mongoosejs.com
How do I stop it coming back?
- Write every path as { type: ..., default: ... } once it needs more than a type.
- Put sub-schemas in their own files and import them at the top of the parent.
- Load every model once at start-up in a test, so a broken schema fails the build.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix