-
Hello, I have some runtime config values that should be validated before the app starts. The problem is that the runtimeConfig for the module is not loaded during setup of the module itself. My impression from reading the docs is that a good practice is to augment default runtime options with env using Just tried the code below on a clean new nuxt app using the latest nuxt version 3.11.2. Please advice, how do I load the runtimeConfig during module setup? import { defineNuxtModule } from "@nuxt/kit";
export default defineNuxtModule<{
bar: string;
runtimeConfig: { foo: string };
}>({
meta: {
name: "some-name",
version: "1.0.0",
configKey: "moduleName",
},
// Default configuration options of the Nuxt module
defaults: {
bar: "default bar",
runtimeConfig: {
foo: "default foo",
},
},
setup(options, nuxt) {
const config = nuxt.options.runtimeConfig;
const env = Object.entries(process.env).filter(([k]) => k.startsWith("NUXT"));
console.log(config, env);
// logs { public: {}, app: { baseURL: '/', buildAssetsDir: '/_nuxt/', cdnURL: '' } } [ [ 'NUXT_MODULE_NAME_FOO', 'FOOENV' ] ]
// @ts-expect-error
if (config.moduleName?.foo === "foo default") {
throw new Error("Invalid config, override with env");
}
},
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
It's a bit confusing that you named one of the module options |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reponse! I will try to clarify :) The runtime config will be populated with values from the environment. And I'm following the pattern from the module author guide here https://nuxt.com/docs/guide/going-further/modules#exposing-options-to-runtime The expected outcome is that So, for me, the example from the module author guide seems misleading, or there is a bug. It makes no sense to send an empty object into I hope this helps and clears up my question! |
Beta Was this translation helpful? Give feedback.
Ok. I got it working: https://stackblitz.com/edit/github-pgdfdr?file=src%2Fmodule.ts
run these commands:
I think the problem is that the specially named env vars mentioned here don't seem to work in the build step.
So, if having this in nuxt.config:
Then staring your app via
NUXT_MODULE_NAME_FOO=something npm run dev
. Logging outnuxt.options.runtimeConfig.moduleNameFoo
inside the module one can see that it's just that (same) empty string.Doing this will make the var present for the module:
This behavior makes sense. The automa…