jahidanowar / nuxt-server-utils

A collection of utilities for Nuxt server.
https://nuxt-server-utils.jahid.dev/
22 stars 4 forks source link

Error on run regarding [ @nuxt/kit ] #1

Open ramyahm2010 opened 9 months ago

ramyahm2010 commented 9 months ago

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,

majadul1989 commented 8 months ago

I have also same issue

navkuun commented 8 months ago

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

lorenzocarreri commented 7 months ago

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

VinGitonga commented 7 months ago

Same issue here, any update?

quyumkehinde commented 6 months ago

+1. Any update on this?

quyumkehinde commented 6 months ago

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.

danilovilhena commented 3 months ago

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);
    }
    });
  3. 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


4. Create a new script in `package.json` with: `"prebuild": "bash prebuild/script.sh"`
5. Run the `prebuild` script before your `build` script and after `npm install`
svenho commented 3 months ago

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.