Registry / database / nuxt-mongoose

nuxt-mongoose

JSON →
library1.1.2jsnpmunverified

Nuxt 3 module for MongoDB with Mongoose. Version 1.1.2 provides seamless integration of Mongoose ODM into Nuxt 3 applications, with auto-import of models from a specified directory, support for multiple connections, and custom model definitions. Released with regular updates, it simplifies database setup for Nuxt 3 projects compared to manual Mongoose configuration. Key differentiators include no-code module auto-imports, schema generation from model files, and built-in Nuxt DevTools support.

npm install nuxt-mongoose
INSTALL
IMPORT
SIG · NUXT-MONGOOSE
N
nuxt-mongoose
databasejavascriptv1.1.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

defineMongooseModel
import { defineMongooseModel } from '#nuxt/mongoose'
const { defineMongooseModel } = require('#nuxt/mongoose')
Module uses ESM; require() will not work.
defineMongooseConnection
import { defineMongooseConnection } from '#nuxt/mongoose'
import { defineMongooseConnection } from 'nuxt-mongoose'
Use the virtual path '#nuxt/mongoose' provided by Nuxt module, not the package name.
User
export const User = defineMongooseModel('User', { name: String })
const User = mongoose.model('User', new mongoose.Schema({ name: String }))
Do not use mongoose.model directly; use defineMongooseModel for proper auto-import.

Shows how to configure nuxt-mongoose, define a model, and use it in a Nuxt API endpoint.

// nuxt.config.ts export default defineNuxtConfig({ modules: ['nuxt-mongoose'], mongoose: { uri: process.env.NUXT_MONGOOSE_URI ?? '', options: {}, modelsDir: 'models', }, }) // .env NUXT_MONGOOSE_URI=mongodb+srv://user:pass@cluster.mongodb.net/my-db?retryWrites=true&w=majority // models/User.ts (auto-imported) export const User = defineMongooseModel('User', { name: { type: String, required: true }, email: { type: String, required: true, unique: true }, }) // server/api/users.ts import { User } from '#nuxt/mongoose' export default defineEventHandler(async (event) => { const users = await User.find() return users })
Debug
Known issues
gotchaWhen connecting to an existing collection, you must specify the collection name in options.collection, otherwise Mongoose creates a new collection with a pluralized model name.
fix
Add options: { collection: 'your_collection_name' } when defining the model.
affects: >=1.0
gotchaModule uses ESM only. CommonJS require() will not work for importing from '#nuxt/mongoose'.
fix
Use import syntax instead of require().
affects: >=1.0
gotchaThe URI must be set via NUXT_MONGOOSE_URI environment variable or mongoose.uri config; undefined URI causes connection error.
fix
Ensure URI is provided in .env or nuxt.config.ts.
affects: >=1.0
Errors
Common errors & fixes
Cannot use import statement outside a module
Trying to use ESM import in a CommonJS file or without type: module in package.json
fix
Ensure your Nuxt project uses ESM (Nuxt 3 default). Use import syntax correctly.
MongooseError: The `uri` parameter to `mongoose.connect()` must be a string, got "undefined"
NUXT_MONGOOSE_URI environment variable not set or not loaded
fix
Set NUXT_MONGOOSE_URI in .env file at project root.
Module not found: Can't resolve '#nuxt/mongoose'
nuxt-mongoose not installed or not added to modules in nuxt.config.ts
fix
Run `npx nuxi@latest module add nuxt-mongoose` and add 'nuxt-mongoose' to modules array.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies
mongooserequiredPeer dependency for MongoDB ODM functionality
Agent activity
9 hits · last 30 days
node
8
Resources
nuxt-mongoose — npm install nuxt-mongoose · libregistry