undefined is not an object (evaluating 'process.env.NODE_ENV')

胡松涛
  • 85

启动npm环境成功,但是浏览器上报错:

TypeError: undefined is not an object (evaluating 'process.env.NODE_ENV')
keyMirror - keyMirror.js:38

查看:keyMirror:

/**
 * Constructs an enumeration with keys equal to their value.
 *
 * For example:
 *
 *   var COLORS = keyMirror({blue: null, red: null});
 *   var myColor = COLORS.blue;
 *   var isColorValid = !!COLORS[myColor];
 *
 * The last line could not be performed if the values of the generated enum were
 * not equal to their keys.
 *
 *   Input:  {key1: val1, key2: val2}
 *   Output: {key1: key1, key2: key2}
 *
 * @param {object} obj
 * @return {object}
 */
var keyMirror = function(obj) {
  var ret = {};
  var key;
  ("production" !== process.env.NODE_ENV ? invariant(
    obj instanceof Object && !Array.isArray(obj),
    'keyMirror(...): Argument must be an object.'
  ) : invariant(obj instanceof Object && !Array.isArray(obj)));
  for (key in obj) {
    if (!obj.hasOwnProperty(key)) {
      continue;
    }
    ret[key] = key;
  }
  return ret;
};

module.exports = keyMirror;
回复
阅读 4.3k
1 个回答

后来发现是在webpack里配置的问题:

loaders: [{
            test: /\.jsx?$/, 
            loaders: ['react-hot', 'babel'],
            include: path.resolve(ROOT_PATH, 'app/src')
            }
          ...
          ]

加上include: path.resolve(ROOT_PATH, 'app/src')限制解析范围,就好了。
推测也可以提速。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏