HOW TO IMPORT NEXT.CONFIG.JS TO MIDDLEWARE
Unanswered
American Shorthair posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ShorthairOP
import { NextResponse } from "next/server";
import { jwtVerify } from "jose";
import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig();
export async function middleware(request) {
}
I am trying to import my config from next.confug.js file into my middleware.js file, but when I try to do so I get the next error
Cannot destructure property 'publicRuntimeConfig' of 'next_config__WEBPACK_IMPORTED_MODULE_2___default(...)(...)' as it is undefined.
I am using it in many other js file without problem, I guess is something related to edge run, same that happened to me when trying to use jsonwebtoken module. Any help is help, thanks
11 Replies
![Avatar](https://cdn.discordapp.com/avatars/602953499407286331/b2ddc5d07aefa902d90e1613e7f970dd.webp?size=256)
ncls.
Hi again! What exactly do you need from the NextConfig?
Or rather what exactly do you need it for?
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ShorthairOP
Jwt secret to verify the tokens
I can hard code it but is not as good
![Avatar](https://cdn.discordapp.com/avatars/602953499407286331/b2ddc5d07aefa902d90e1613e7f970dd.webp?size=256)
ncls.
It should be inside your
.env
file. You can get it from there using a library like [dotenv](https://npmjs.com/package/dotenv)![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ShorthairOP
is not possible to use next.config.js file? as I already have all the important variables stored in there
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
serverRuntimeConfig: {
dbConfig: {
host: 'localhost',
port: 3306,
user: 'root',
password: '', // @@@
database: 'clinic-db'
},
secret: 'test'
},
publicRuntimeConfig: {
apiUrl: process.env.NODE_ENV === 'development'
? 'http://localhost:3000/api' // development api
: 'http://localhost:3000/api' // production api
}
}
module.exports = nextConfig
![Avatar](https://cdn.discordapp.com/avatars/602953499407286331/b2ddc5d07aefa902d90e1613e7f970dd.webp?size=256)
ncls.
I would suggest storing all sensitive information inside a
.env
file. Makes it easier to load and more secure in case you want to commit to GitHub one day or similar![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ShorthairOP
okay, I will take your advice, thanks!
![Avatar](https://cdn.discordapp.com/avatars/602953499407286331/b2ddc5d07aefa902d90e1613e7f970dd.webp?size=256)
ncls.
I think you actualy don't need the dotenv library. Next.js should have built-in support for enviroment variables
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
American ShorthairOP
yes it has 😄