最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - publicRuntimeConfig in next.config.js is always undefined in prodstaging - Stack Overflow

programmeradmin3浏览0评论

I am deploying a node project that uses next.js to openshift where I set the environment variable MY_ENV. I have added the publicRuntimeConfig configuration to next.config.js to access it client side. It works in my local but when its containerized and deployed publicRuntimeConfig is undefined.

Here is my configuration from next.config.js

module.exports = {
  publicRuntimeConfig: { // Will be available on both server and client
      isProd: process.env.MY_ENV ? process.env.MY_ENV.includes('prod'): false,
      isStaging: process.env.MY_ENV ? process.env.MY_ENV.includes('staging') : false
    },
  webpack: (config, { dev }) => {
    const eslintRule = {
      test: /\.js$/,
      enforce: 'pre',
      exclude: /node_modules/,
      loader: 'eslint-loader',
      options: {
        emitWarning: dev,
      },
    };
    const cssRule = {
      test: /\.css$/,
      use: {
        loader: 'css-loader',
        options: {
          sourceMap: false,
          minimize: true,
        },
      },
    };

    config.node = {
      fs: 'empty'
    };

    config.module.rules.push(eslintRule);
    config.module.rules.push(cssRule);
    return config;
  }
};

This is how I am trying to get the publicRuntimeConfig on my pages.

import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();

console.log(publicRuntimeConfig.isProd); //publicRuntimeConfig is undefined here. 

Any help is appreciated.

UPDATE/FIX

publicRuntimeConfig was undefined in higher environments because it was not part of the package that was getting deployed.

I am deploying a node project that uses next.js to openshift where I set the environment variable MY_ENV. I have added the publicRuntimeConfig configuration to next.config.js to access it client side. It works in my local but when its containerized and deployed publicRuntimeConfig is undefined.

Here is my configuration from next.config.js

module.exports = {
  publicRuntimeConfig: { // Will be available on both server and client
      isProd: process.env.MY_ENV ? process.env.MY_ENV.includes('prod'): false,
      isStaging: process.env.MY_ENV ? process.env.MY_ENV.includes('staging') : false
    },
  webpack: (config, { dev }) => {
    const eslintRule = {
      test: /\.js$/,
      enforce: 'pre',
      exclude: /node_modules/,
      loader: 'eslint-loader',
      options: {
        emitWarning: dev,
      },
    };
    const cssRule = {
      test: /\.css$/,
      use: {
        loader: 'css-loader',
        options: {
          sourceMap: false,
          minimize: true,
        },
      },
    };

    config.node = {
      fs: 'empty'
    };

    config.module.rules.push(eslintRule);
    config.module.rules.push(cssRule);
    return config;
  }
};

This is how I am trying to get the publicRuntimeConfig on my pages.

import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();

console.log(publicRuntimeConfig.isProd); //publicRuntimeConfig is undefined here. 

Any help is appreciated.

UPDATE/FIX

publicRuntimeConfig was undefined in higher environments because it was not part of the package that was getting deployed.

Share Improve this question edited Mar 13, 2019 at 18:55 Grinish Nepal asked Mar 12, 2019 at 2:19 Grinish NepalGrinish Nepal 3,0633 gold badges34 silver badges51 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

Does undefined Error occur in pages?

what about try to getConfig from next/config ?

import getConfig from 'next/config';

const getNodeEnv = () => {
  const { publicRuntimeConfig } = getConfig();

  const isProd = publicRuntimeConfig.isProd || false;
  const isStaging = publicRuntimeConfig. isStaging || false;

  return { isProd, isStaging }
};

const env = getNodeEnv()

console.log(env)
发布评论

评论列表(0)

  1. 暂无评论