HOW TO IMPORT NEXT.CONFIG.JS TO MIDDLEWARE
Unanswered
American Shorthair posted this in #help-forum
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
Or rather what exactly do you need it for?
@ncls. Hi again! What exactly do you need from the NextConfig?
American ShorthairOP
Jwt secret to verify the tokens
I can hard code it but is not as good
It should be inside your
.env
file. You can get it from there using a library like [dotenv](https://npmjs.com/package/dotenv)@ncls. It should be inside your `.env` file. You can get it from there using a library like [dotenv](https://npmjs.com/package/dotenv)
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
@American Shorthair /** @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
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@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
American ShorthairOP
okay, I will take your advice, thanks!
I think you actualy don't need the dotenv library. Next.js should have built-in support for enviroment variables
@ncls. I think you actualy don't need the dotenv library. Next.js should have built-in support for enviroment variables
American ShorthairOP
yes it has 😄