Node.js 应用程序无法部署到 Heroku

Node.js application fails to deploy to Heroku

我正在尝试将应用程序部署到 Heroku。应用程序只有在更改引擎版本后才能成功部署。如果我尝试在不修改 "engines" 部分的情况下进行部署,部署将失败并出现以下错误:

remote:        ERROR in ./resources/assets/sass/app.scss
remote:        Module build failed: ModuleBuildError: Module build failed: Error: ENOENT: no such file or directory, scandir '/tmp/build_7b88e5ab3110a2273bdb09b2d4f7c673/node_modules/node-sass/vendor'

这是我的package.json

{
  "private": true,
  "name": "instagram",
  "description": "<p align=\"center\"><img src=\"https://laravel.com/assets/img/components/logo-laravel.svg\"></p>",
  "version": "1.0.0",
  "main": "webpack.config.js",
  "directories": {
    "test": "tests"
  },
  "engines" : {
    "npm" : "5.0.4",
    "node": "7.4.0",
    "yarn": "0.24.6"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrap": "^3.3.7",
    "bootstrap-notify": "^3.1.3",
    "bootstrap-sass": "^3.3.7",
    "bootstrap-switch": "^3.3.4",
    "css-loader": "^0.28.4",
    "cssnano": "^3.10.0",
    "extract-text-webpack-plugin": "^2.1.2",
    "file-loader": "^0.11.2",
    "font-awesome": "^4.7.0",
    "jquery": "^3.2.1",
    "knockout": "^3.4.2",
    "node-sass": "^4.5.3",
    "nprogress": "^0.2.0",
    "optimize-css-assets-webpack-plugin": "^2.0.0",
    "sass-loader": "^6.0.6",
    "select2": "^4.0.3",
    "uglifyjs-webpack-plugin": "^0.4.6",
    "url-loader": "^0.5.9",
    "webpack": "^2.2.0",
    "webpack-cleanup-plugin": "^0.5.1"
  },
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  }
}

也许除了更改引擎版本之外,您还有其他解决方案吗?

我找到了解决方案 - 在 Heroku 上禁用节点模块缓存:

heroku config:set NODE_MODULES_CACHE=false
$ git commit -am 'disable node_modules cache' --allow-empty
$ git push heroku master

这是解决这个问题的方法:

"scripts": {
  "heroku-postbuild": "yarn add --force node-sass"
}

或者,对于 npm

"scripts": {
  "heroku-postbuild": "npm rebuild node-sass"
}

参见:

https://github.com/sass/node-sass/issues/1387#issuecomment-185451183

和:

https://github.com/rails/webpacker/issues/422#issuecomment-304018204