Heroku试图在生产中请求开发文件

时间:2017-05-03 20:38:04

标签: node.js heroku heroku-toolbelt

我正在向heroku部署快速和反应项目,所有似乎都进展顺利,直到我运行postinstall来构建我的项目。 我设置了heroku config:set NODE_ENV=production,但是当我运行git push heroku master时,它会安装所有依赖项,然后进入npm构建,因为它尝试访问仅用于开发环境的require文件。

当我设置dev环境=生产时,这很疯狂。 下面是我的package.json脚本。

  "scripts": {
    "start": "node server/app.js",
    "dev": "nodemon server/app.js",
    "build": "cross-env NODE_ENV=production webpack --config ./webpack/webpack.prod.config.js --progress --colors",
    "heroku-postbuild": "npm run build"
  },

这是我得到的错误。

remote: 
remote: -----> Node.js app detected
remote: 
remote: -----> Creating runtime environment
remote:        
remote:        NPM_CONFIG_LOGLEVEL=error
remote:        NPM_CONFIG_PRODUCTION=true
remote:        NODE_VERBOSE=false
remote:        NODE_ENV=production
remote:        NODE_MODULES_CACHE=true
remote: 
remote: -----> Installing binaries
remote:        engines.node (package.json):  v6.0.0
remote:        engines.npm (package.json):   3.3.4
remote:        
remote:        Resolving node version v6.0.0 via semver.io...
remote:        Downloading and installing node 6.0.0...
remote:        Downloading and installing npm 3.3.4 (replacing version 3.8.6)...
remote: 
remote: -----> Restoring cache
remote:        Loading 2 from cacheDirectories (default):
remote:        - node_modules
remote:        - bower_components (not cached - skipping)
remote: 
remote: -----> Building dependencies
remote:        Installing node modules (package.json)
remote:        (node:224) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.
remote:        Running heroku-postbuild
remote:        
remote:        > Portfolio@1.1.0 heroku-postbuild /tmp/build_918738f52e06005c6e2c99818c8fcedf
remote:        > npm run build
remote:        
remote:        
remote:        > Portfolio@1.1.0 build /tmp/build_918738f52e06005c6e2c99818c8fcedf
remote:        > cross-env NODE_ENV=production webpack --config ./webpack/webpack.prod.config.js --progress --colors
remote:        
remote:        Hash: 75051662c9db78bafa19
remote:        Version: webpack 2.4.1
remote:        Time: 14721ms
remote:        Asset     Size  Chunks                    Chunk Names
remote:        homeslider-2.jpg  78.4 kB          [emitted]
remote:        separator.png   1.8 kB          [emitted]
remote:        bundle.js   479 kB       0  [emitted]  [big]  main
remote:        [3] ./~/react/react.js 56 bytes {0} [built]
remote:        [10] ./~/react-redux/lib/index.js 475 bytes {0} [built]
remote:        [39] ./~/react-router/es/PatternUtils.js 7.34 kB {0} [built]
remote:        [41] ./~/react/lib/React.js 3.32 kB {0} [built]
remote:        [50] ./~/react-router/es/index.js 1.46 kB {0} [built]
remote:        [52] ./app/js/actions/types.js 541 bytes {0} [built]
remote:        [64] ./~/react-dom/index.js 59 bytes {0} [built]
remote:        [209] ./app/js/index.js 1.29 kB {0} [built]
remote:        [244] ./app/js/routes.js 2.83 kB {0} [built]
remote:        [245] ./app/js/store.js 775 bytes {0} [built]
remote:        [472] ./~/react-router/es/Route.js 1.33 kB {0} [built]
remote:        [473] ./~/react-router/es/Router.js 5.28 kB {0} [built]
remote:        [475] ./~/react-router/es/applyRouterMiddleware.js 1.9 kB {0} [built]
remote:        [476] ./~/react-router/es/browserHistory.js 183 bytes {0} [built]
remote:        [563] multi ./app/js/index 28 bytes {0} [built]
remote:        + 549 hidden modules
remote:        
remote:        ERROR in ./server/config/config.js
remote:        Module not found: Error: Can't resolve './config.json' in '/tmp/build_918738f52e06005c6e2c99818c8fcedf/server/config'
remote:        @ ./server/config/config.js 6:15-39
remote:        @ ./app/js/actions/index.js
remote:        @ ./app/js/pages/login.js
remote:        @ ./app/js/routes.js
remote:        @ ./app/js/index.js
remote:        @ multi ./app/js/index
remote: 
remote: npm ERR! Linux 3.13.0-112-generic
remote: npm ERR! argv "/tmp/build_918738f52e06005c6e2c99818c8fcedf/.heroku/node/bin/node" "/tmp/build_918738f52e06005c6e2c99818c8fcedf/.heroku/node/bin/npm" "run" "build"
remote: npm ERR! node v6.0.0
remote: npm ERR! npm  v3.3.4
remote: npm ERR! code ELIFECYCLE
remote: npm ERR! Portfolio@1.1.0 build: `cross-env NODE_ENV=production webpack --config ./webpack/webpack.prod.config.js --progress --colors`
remote: npm ERR! Exit status 2
remote: npm ERR! 
remote: npm ERR! Failed at the Portfolio@1.1.0 build script 'cross-env NODE_ENV=production webpack --config ./webpack/webpack.prod.config.js --progress --colors'.
remote: npm ERR! This is most likely a problem with the Portfolio package,
remote: npm ERR! not with npm itself.

错误:

   remote:        ERROR in ./server/config/config.js
    remote:        Module not found: Error: Can't resolve './config.json' in '/tmp/build_918738f52e06005c6e2c99818c8fcedf/server/config'

这是需要./config.json

的文件

config.js

var env = process.env.NODE_ENV || 'development';

console.log("env = ", env);

if(env === 'development' || env === 'test'){
  var config = require('./config.json');
  var envConfig = config[env];
  Object.keys(envConfig).forEach(function(key){
    process.env[key] = envConfig[key];
  })
}
module.exports = process.env;

因此,只有在process.env.NODE_ENV =开发时,config.js才需要config.json。

如果环境是生产环境,它如何才能访问它?

0 个答案:

没有答案