找不到Webpack,部署到Heroku

时间:2016-09-12 19:40:16

标签: node.js express reactjs heroku

非常新的Node并部署到Heroku。我有一个基本的React应用程序设置,我正在尝试部署到Heroku。我推了它,但应用程序失败了。当我查看日志时,我看到了sh: 1: webpack: not found(完整日志here

的内容

我不确定发生了什么,但是相信它与我的package.json有关?从我使用它的启动器模板是这样的:

{
    "name": "express-react-redux-starter",
    "version": "1.0.0",
    "description": "Starter for Express, React, Redux, SCSS applications",
    "scripts": {
      "dev": "webpack-dev-server --config ./webpack/webpack-dev.config.js --watch --colors",
      "build": "rm -rf dist && webpack --config ./webpack/webpack-prod.config.js --colors",
      "start": "PORT=8080 node start ./server.js",
      "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
      "test:watch": "npm run test -- --watch",
      "lint": "eslint src test webpack"
    },
    "keywords": [
      "ExpressJS",
      "ReactJS",
      "Redux",
      "React hot loader",
      "React Router",
      "SCSS",
      "Webpack Devevelopment configuration",
      "Webpack Production configuration",
      "Airbnb Eslint",
      "pm2",
      "mocha",
      "chai"
    ],
    "repository": {
      "type": "git",
      "url": "git+https://github.com/DimitriMikadze/express-react-redux-starter"
    },
    "author": "Dimitri Mikadze",
    "license": "MIT",
    "devDependencies": {
      "autoprefixer": "^6.4.0",
      "autoprefixer-loader": "^3.2.0",
      "babel-core": "^6.8.0",
      "babel-loader": "^6.2.4",
      "babel-preset-es2015": "^6.6.0",
      "babel-preset-react": "^6.5.0",
      "babel-preset-stage-1": "^6.5.0",
      "chai": "^3.5.0",
      "chai-jquery": "^2.0.0",
      "css-loader": "^0.23.1",
      "eslint": "^2.10.2",
      "eslint-config-airbnb": "^9.0.1",
      "eslint-plugin-import": "^1.8.0",
      "eslint-plugin-jsx-a11y": "^1.2.0",
      "eslint-plugin-react": "^5.1.1",
      "extract-text-webpack-plugin": "^1.0.1",
      "html-webpack-plugin": "^2.16.1",
      "jquery": "^2.2.3",
      "jsdom": "^9.0.0",
      "mocha": "^2.4.5",
      "node-sass": "^3.7.0",
      "react-addons-test-utils": "^15.0.2",
      "react-hot-loader": "^1.3.0",
      "sass-loader": "^3.2.0",
      "style-loader": "^0.13.1",
      "url-loader": "^0.5.7",
      "webpack-dev-server": "^1.14.1"
    },
    "dependencies": {
      "classnames": "^2.2.5",
      "express": "^4.13.4",
      "lodash": "^4.15.0",
      "react": "^15.0.2",
      "react-dom": "^15.0.2",
      "react-redux": "^4.4.5",
      "react-router": "^2.4.0",
      "redux": "^3.5.2",
      "webpack": "^1.13.0"
    }
}

我为错误地部署了什么?在我的localhost上工作得很好。但无法弄清楚如何将这一点融入到我的生活中。非常感谢!

8 个答案:

答案 0 :(得分:20)

好的 - 这与package.json中的devDependencies和Dependencies有关 此外,通过将Heroku配置设置为NPM_CONFIG_PRODUCTION: false,我能够解决此问题。互联网!

答案 1 :(得分:10)

正如其他人所说,Heroku将节点作为生产NODE_ENV=production运行,这意味着您的devDependencies不会被安装。 Heroku提供了特定于节点的构建挂钩。我使用heroku-prebuild运行npm install --dev,它将安装所有依赖项和devDependencies。

"scripts": {
  ...,
  "heroku-prebuild": "npm install --dev",
  ...
},

应该避免不在生产模式下运行应用程序。开发人员通常具有“开发模式”特定的代码,这些代码在生产环境中运行时会被过滤掉。另外,如果您正在执行Webpack构建,您肯定希望处于生产模式以利用缩小,丑化等优势。

这是Heroku的详细信息。

  

有时候,开发人员需要的东西比生产更注重生产。   package.json中的preinstall和postinstall挂钩。例如,一些   应用需要在安装前设置额外的身份验证   依赖性。有些需要建造资产,但不需要开发   环境。可以在以下讨论中找到更多示例   GitHub。

     

Node.js开发人员现在可以使用heroku-prebuild和heroku-postbuild   可以根据自己的应用量身定制构建过程。

https://devcenter.heroku.com/changelog-items/844

答案 2 :(得分:9)

发生这种情况是因为heroku默认情况下不安装package.json的开发依赖项,我们需要用suulsulsly告诉heroku(npm)安装我们的dev依赖项(webpack在dev中),所以运行这个命令应该解决 "未找到问题"

npm install --dev

答案 3 :(得分:3)

导致此问题的原因是const wchar_t * const *未安装。您可以通过在项目的根目录中键入以下命令来克服这个问题:

devDependencies

然后,下次部署应用时,您的问题应该得到解决。

答案 4 :(得分:0)

此行为已在2018年1月3日更改:https://devcenter.heroku.com/changelog-items/1376

它将安装devDependencies,但也将它们修剪为部署的一部分。可以通过heroku config:set NPM_CONFIG_PRODUCTION=false跳过修剪。仍然会将NODE_ENV设置为production

答案 5 :(得分:0)

我误解了这个问题,所以让她为纱线使用者服务:

"heroku-prebuild": "yarn install --production=false"

问题不在于根据文档(至少对于npm)之后发生的修剪,而是纱线解释NODE_ENV并即使在预构建阶段也仅安装生产依赖项。

要强制纱线“监视” env变量,必须给--production=false标志。参见https://yarnpkg.com/lang/en/docs/cli/install/

答案 6 :(得分:0)

这对我有用https://github.com/rails/webpacker/issues/512

heroku buildpacks:add --index 1 heroku/nodejs

答案 7 :(得分:-1)

正如其他人所说,Heroku 将在运行指定的 package.json 脚本以启动应用程序之前修剪 devDependencies。

我仍然希望 Heroku 修剪 devDependencies,因为在生产过程中并不需要这些包,而只是在构建过程中。 Heroku 允许使用 customized build process,我可以利用它来实现预期效果。

"scripts": {
  "heroku-prebuild": "npm install --dev",
  "build": "webpack --mode production",
  "start": "node src/app.js",
}

预构建将安装开发依赖项,然后构建脚本将简单地使用这些开发依赖项来生成 webpack 包。之后,在运行启动脚本之前,将定期修剪 devDependencies 以用于生产。