Whenever I am trying to create a build
or running npm start
I am getting the API_SERVER
is not defined. I have already defined the API_SERVER
inside the .env
file but still, it's not picking it while building. npm run dev
is working fine.
This is the error I am getting.
Here is my next.config.js
file
const { withPlugins, optional } = require('next-compose-plugins')
// eslint-disable-next-line import/no-extraneous-dependencies
const { PHASE_PRODUCTION_BUILD } = require('next-server/constants')
const sass = require('@zeit/next-sass')
const withCSS = require('@zeit/next-css')
const { requireEnvVar } = require('./lib/nodeUtils')
/**
* Custom Next.js Configuration
* @see https://nextjs.org/docs#custom-configuration
*/
const nextConfig = {
/**
* Runtime configuration
* @see https://nextjs.org/docs#exposing-configuration-to-the-server--client-side
*/
publicRuntimeConfig: {
// Will be available on both server and client
apiUrl: requireEnvVar('API_SERVER'),
gtmId: requireEnvVar('GTM_ID'),
},
/**
* Disable file-system routing
* @see https://nextjs.org/docs#disabling-file-system-routing
*/
useFileSystemPublicRoutes: false,
/**
* Custom webpack config
* @see https://nextjs.org/docs#customizing-webpack-config
*/
webpack(config, { webpack }) {
/**
* Only load specific locales for moment.js
* @see https://stackoverflow.com/a/25426019/956688
*/
config.plugins.push(
new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /en/)
)
return config
},
}
/**
* Load multiple plugins with next-compose-plugins
* @see https://github.com/cyrilwanner/next-compose-plugins
*/
module.exports = withPlugins(
[
[sass],
[withCSS],
/**
* Analyzing the webpack bundle
* @see https://github.com/zeit/next-plugins/tree/master/packages/next-bundle-analyzer
*
* Load @zeit/next-bundle-analyzer as an optional plugin only during production build
* @see https://github.com/cyrilwanner/next-compose-plugins#optional-plugins
* @see https://github.com/cyrilwanner/next-compose-plugins#phases-array
*/
[
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
optional(() => require('@zeit/next-bundle-analyzer')),
{
analyzeServer: ['server', 'both'].includes(process.env.BUNDLE_ANALYZE),
analyzeBrowser: ['browser', 'both'].includes(
process.env.BUNDLE_ANALYZE
),
bundleAnalyzerConfig: {
server: {
analyzerMode: 'static',
reportFilename: '../../bundles/server.html',
},
browser: {
analyzerMode: 'static',
reportFilename: '../bundles/client.html',
},
},
},
[PHASE_PRODUCTION_BUILD],
],
],
nextConfig
)
Here is my .env
file
API_SERVER=http://localhost:3005
NEXTAUTH_URL=http://localhost:3000
GTM_ID=GTM-XXXXXXX
Here is the nodeUtils.js
code
exports = module.exports = {}
/**
* Load the value of an environment variable and throw an error if not defined
*
* @param {string} varName
* @return {*}
*/
exports.requireEnvVar = function requireServerEnvVar(varName) {
const envVar = process.env[varName]
if (!envVar) {
throw new Error(`${varName} environment variable must be defined!`)
}
return envVar
}
This is my package.json
script
"scripts": {
"dev": "node server.js",
"build": "next build",
"clean": "rimraf ./.next ./node_modules/.cache",
"start": "HTTPS=true cross-env NODE_ENV=production node server.js",
"lint": "eslint --ext js,jsx --ignore-path .gitignore .",
"lint-fix": "npm run lint -- --fix",
"format-check": "prettier --check \"**/*.{js,jsx,json,css,scss,md}\"",
"format-write": "prettier --write \"**/*.{js,jsx,json,css,scss,md}\"",
"test": "echo \"Error: no test specified\" && exit 1",
"analyze-both": "cross-env BUNDLE_ANALYZE=both next build",
"analyze-browser": "cross-env BUNDLE_ANALYZE=browser next build",
"analyze-server": "cross-env BUNDLE_ANALYZE=server next build"
},
Any help is appreciated.