arashsheyda / nuxt-mongoose

A Nuxt module for simplifying the use of Mongoose in your project.
https://docs.arashsheyda.me/nuxt-mongoose
76 stars 14 forks source link

Use runtime config for the MONGODB_URI? #44

Open nathan1658 opened 9 months ago

nathan1658 commented 9 months ago

It seem the URI is required during build time. Is it possible to use runtime config so when I dockerize the application i can do something like

docker run --env NUXT_MONGODB_URI=xxxxxxx
arashsheyda commented 9 months ago

yes you can use runtime config with the variable MONGODB_URI

nathan1658 commented 9 months ago

Hi, can you elaborate more on how can I achieve this? This is my nuxt.config:

export default defineNuxtConfig({
  mongoose: {
    uri: process.env.MONGODB_URI,
    options: {},
  },
})

And this is my dockerfile:

FROM node:lts as builder

WORKDIR /app

COPY . .
RUN npm i
RUN npm run build

# Nuxt 3 production
FROM node:lts

WORKDIR /app

COPY --from=builder /app/.output  /app/.output

ENV NITRO_PORT=80

EXPOSE 80

CMD [ "node", ".output/server/index.mjs" ]

And this is what i want to archive:

docker run --rm -p 8081:80 \
--env NUXT_MONGODB_URI="mongodb+srv://xxxxxxxx" \
nuxt3-test

However, during the docker build process, I can see the error like Missing MongoDB URI. May I know how can I resolve it? Thank you so much!

arashsheyda commented 9 months ago

make sure to use the same variable name in your nuxt.config and docker command, they both should be MONGODB_URI in this case (you can change it to whatever you want, just make sure that it is the same in nuxt.config.mongoose.uri)

docker run -rm -p 8081:80 -e MONGODB_URI=your-mongodb-uri nuxt3-test
export default defineNuxtConfig({
  mongoose: {
    uri: process.env.MONGODB_URI, // this is the uri by default, you can remove this lines
    options: {},
  },
})
nathan1658 commented 9 months ago

Hello,

It appears that the MONGODB_URI is determined at build time and it cannot be modified subsequently. I have tried to update my .env file to:

MONGODB_URI=mongodb://aaaaa

Afterwards, I ran:

npm run build

However, when I attempted to change the MONGODB_URI and run the server:

MONGODB_URI=mongodb://bbbb node .output/server/index.mjs  

The application still used mongodb://aaaaa as the database URI.

Furthermore, in my specific case, I observed that the MONGODB_URI environment variable must be present during build time. If not, I encounter the following error:

(node-resolve plugin) Could not resolve import "#nuxt/mongoose" in xxxxxx .ts using imports defined in xxxxx/package.json.

If I then try to run the server with a different MONGODB_URI:

MONGODB_URI=mongodb://bbbb node .output/server/index.mjs  

I receive another error:

[nuxt] [request error] [unhandled] [500] MODEL is not defined
  at ./.output/server/chunks/index.get9.mjs:13:20  
  at ./.output/server/chunks/nitro/node-server.mjs:2140:47  
  at async Object.handler (./.output/server/chunks/nitro/node-server.mjs:2202:19)  
  at async Server.toNodeHandle (./.output/server/chunks/nitro/node-server.mjs:2391:7)

I would appreciate any guidance or suggestions on how to address these issues. Thanks in advance for your assistance.

Joshua-Noakes1 commented 8 months ago

@arashsheyda I'm having the same issue as Nathan when trying to build a docker container using Github Actions, I can't change the mongo uri after the build. I'm happy to provide logs from my container if it helps.

kevinstory commented 6 months ago

Can we please reopen this issue as it is not resolved? Runtime values will not overwrite variables set at build time.

kevinstory commented 6 months ago

Actually, from my testing just now, it looks like this can be overwritten with the environment variable named NUXT_MONGOOSE_URI and not MONGODB_URI.

arashsheyda commented 6 months ago

@kevinstory oh that's great then!! thanks a lot (sorry I don't have docker to test)

gamegine commented 6 months ago

works with the latest version (1.0.6)

export default defineNuxtConfig({
    modules: ["nuxt-mongoose"],
    runtimeConfig: {
        mongoose: { uri: process.env.NUXT_MONGOOSE_URI },
    },
});

change the config env from MONGODB_URI to NUXT_MONGOOSE_URI as specified in https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables.

can also be but not tested with the options

runtimeConfig: {
  mongoose: { 
      uri: process.env.NUXT_MONGOOSE_URI },
      options: process.env.NUXT_MONGOOSE_OPTIONS },
  }
}