nuxt-server-utils icon indicating copy to clipboard operation
nuxt-server-utils copied to clipboard

Error on run regarding [ @nuxt/kit ]

Open ramyahm2010 opened this issue 1 year ago • 8 comments

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,

ramyahm2010 avatar Feb 10 '24 07:02 ramyahm2010

I have also same issue

majadul1989 avatar Feb 22 '24 02:02 majadul1989

I'm also getting the same issue. Any update on this?

navkuun avatar Feb 27 '24 14:02 navkuun

I'm also getting the same issue, any update?

lorenzocarreri avatar Mar 28 '24 15:03 lorenzocarreri

Same issue here, any update?

VinGitonga avatar Apr 06 '24 07:04 VinGitonga

+1. Any update on this?

quyumkehinde avatar Apr 17 '24 16:04 quyumkehinde

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.

quyumkehinde avatar Apr 17 '24 19:04 quyumkehinde

For anyone running into this issue, you can follow these steps to automate @quyumkehinde fix until @jahidanowar publishes a new version.

  1. Create a folder called prebuild in your root directory
  2. Create a mongoose.mjs inside 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);
  }
});
  1. Create a script.sh inside 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
  1. Create a new script in package.json with: "prebuild": "bash prebuild/script.sh"
  2. Run the prebuild script before your build script and after npm install

danilovilhena avatar Jul 25 '24 00:07 danilovilhena

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.

svenho avatar Jul 29 '24 16:07 svenho