nuxt-server-utils
nuxt-server-utils copied to clipboard
Error on run regarding [ @nuxt/kit ]
Hello,
When trying to run Nuxt app, I get following error message: This module cannot be imported in server runtime. [importing @nuxt/kit from node_modules/nuxt-server-utils/dist/runtime/server/plugins/mongoose.mjs]
I use following dependencies: "dependencies": { "@mdi/font": "^7.4.47", "jsonwebtoken": "^9.0.2", "nuxt": "^3.10.1", "nuxt-server-utils": "^0.0.7", "ts-md5": "^1.3.1", "vue": "^3.4.15", "vue-router": "^4.2.5" }, "devDependencies": { "@nuxt/devtools": "^1.0.8", "mongoose": "^8.1.1", "sass": "^1.70.0", "vite-plugin-vuetify": "^2.0.1", "vuetify": "^3.5.2" }
Kindly, note that app is working fine, just the ERROR message appears !
regards,
I have also same issue
I'm also getting the same issue. Any update on this?
I'm also getting the same issue, any update?
Same issue here, any update?
+1. Any update on this?
I've raised a PR here with a fix to the issue. @jahidanowar please could you have a look? This seems to be causing problem for a number of people.
For anyone running into this issue, you can follow these steps to automate @quyumkehinde fix until @jahidanowar publishes a new version.
- Create a folder called
prebuildin your root directory - Create a
mongoose.mjsinside of that folder with this:
import mongoose from "mongoose";
import { useRuntimeConfig } from "#imports";
function defineNitroPlugin(def) {
return def;
}
export default defineNitroPlugin(async () => {
const config = useRuntimeConfig();
if (!config.nuxtServerUtils?.mongodbUri) {
console.warn(
"Mongodb URI not found in runtime config, skipping mongodb connection"
);
return;
}
try {
await mongoose.connect(config.nuxtServerUtils.mongodbUri);
console.info("Mongodb connected");
} catch (e) {
console.error("Mongodb connection error: ", e);
}
});
- Create a
script.shinside of that folder with this:
#!/bin/bash
SOURCE_FILE="./prebuild/mongoose.mjs"
TARGET_FILE="node_modules/nuxt-server-utils/dist/runtime/server/plugins/mongoose.mjs"
if [ ! -f "$SOURCE_FILE" ]; then
echo "Error: Source file $SOURCE_FILE does not exist."
exit 1
fi
cp "$SOURCE_FILE" "$TARGET_FILE"
if [ $? -eq 0 ]; then
echo "Successfully replaced $TARGET_FILE with $SOURCE_FILE"
else
echo "Error: Failed to replace the file."
exit 1
fi
- Create a new script in
package.jsonwith:"prebuild": "bash prebuild/script.sh" - Run the
prebuildscript before yourbuildscript and afternpm install
Hi guys, web comments say that Jahid has passed away. What a pity! Hope he keeps on rocking whereever he is now. Thanks so much.
Now this news discourages the further use of this module. If you, like me, just need the MongoDB connection and nothing else I want to recommend to uninstall the plugin and then define your own Nitro plugin; that's super easy:
Create a file /server/plugins/connectMongo.ts (this will be auto-imported):
import mongoose from "mongoose";
export default defineNitroPlugin(async () => {
const config = useRuntimeConfig();
if (!config.mongodbUri) {
console.warn(
"Mongodb URI not found in runtime config, skipping mongodb connection"
);
return;
}
try {
await mongoose.connect(config.mongodbUri);
console.info("Mongodb connected");
} catch (e) {
console.error("Mongodb connection error: ", e);
}
});
Now update the nuxt.config.ts so that it provides the MongoDB URI within the runtimeConfig:
runtimeConfig: {
authSecret: process.env.AUTH_SECRET,
mongodbUri: process.env.MONGODB_URI,
},
That's it. Hope you like it.