Next.js Discord

Discord Forum

HOW TO IMPORT NEXT.CONFIG.JS TO MIDDLEWARE

Unanswered
American Shorthair posted this in #help-forum
Open in Discord
Avatar
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
@ncls. Hi again! What exactly do you need from the NextConfig?
Avatar
American ShorthairOP
Jwt secret to verify the tokens
I can hard code it but is not as good
Avatar
It should be inside your .env file. You can get it from there using a library like [dotenv](https://npmjs.com/package/dotenv)
Avatar
@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
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
I think you actualy don't need the dotenv library. Next.js should have built-in support for enviroment variables