nuxt-mongoose icon indicating copy to clipboard operation
nuxt-mongoose copied to clipboard

Use runtime config for the MONGODB_URI?

Open nathan1658 opened this issue 1 year ago • 9 comments

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

nathan1658 avatar Jan 28 '24 16:01 nathan1658

yes you can use runtime config with the variable MONGODB_URI

arashsheyda avatar Feb 01 '24 15:02 arashsheyda

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!

nathan1658 avatar Feb 03 '24 05:02 nathan1658

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: {},
  },
})

arashsheyda avatar Feb 05 '24 16:02 arashsheyda

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.

nathan1658 avatar Feb 06 '24 14:02 nathan1658

@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.

Joshua-Noakes1 avatar Mar 12 '24 02:03 Joshua-Noakes1

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

kevinstory avatar May 14 '24 18:05 kevinstory

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.

kevinstory avatar May 14 '24 19:05 kevinstory

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

arashsheyda avatar May 14 '24 19:05 arashsheyda

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 },
  }
}

gamegine avatar May 21 '24 11:05 gamegine