Heroku Express + React + Webpack-无法在服务器上运行

时间:2019-11-06 15:41:17

标签: reactjs express heroku webpack

我有我的webpack + ts + express + react应用程序。我可以在本地构建它,而不会出现任何问题。 。一切正常,但是当我将其发布到heroku时,我得到了:

eb.1]:npm错误!代码ELIFECYCLE 2019-11-06T15:32:36.057014 + 00:00 app [web.1]:npm错误!错误2

2019-11-06T15:32:36.058319 + 00:00 app [web.1]:npm错误! v@0.0.0开始:tsc && node ./build/app.js

2019-11-06T15:32:36.058526 + 00:00 app [web.1]:npm错误!退出状态2

2019-11-06T15:32:36.058771 + 00:00 app [web.1]:npm错误!

2019-11-06T15:32:36.058897 + 00:00 app [web.1]:npm错误!在v@0.0.0失败 启动脚本。

2019-11-06T15:32:36.059048 + 00:00 app [web.1]:npm错误! npm可能不是问题。上面可能还有其他日志记录输出。

文件夹结构

api

    • app.ts

客户端

    • src
    • tsconfig
    • package.json

tsconfig

package.json

文件app.ts

app.use(cors());
app.use(morgan('dev'));
app.use(express.json());
app.use(cookieParser());
app.use(morgan('dev'));
app.use(express.static(path.join(__dirname, '../client/build')));

app.use('/api/posts', postRoutes);
app.use('/api/users', userRoutes);
app.use('/api', errorRoutes);

app.get('/*', (req, res: Response) => {
  res.sendFile(path.join(__dirname, '../client/build/index.html'));
});

const port = process.env.PORT || 1646;
app.listen(port, () => {
  // tslint:disable-next-line: no-console
  console.log(`listening on port ${port}`);
});

{
  "name": "v",
  "version": "0.0.0",
  "engines": {
    "node": "8.11.2",
    "npm": "5.6.0"
  },
  "private": true,
  "scripts": {
    "dev:front": "cd client && npm run dev",
    "dev:server": "ts-node-dev --respawn --transpileOnly ./api/app.ts",
    "dev": "concurrently \"npm run dev:server\" \"npm run dev:front\"",
    "tsc": "tsc",
    "start": "tsc && node ./build/app.js",
    "clean": "rimraf ./build",
    "build": "tsc && node ./build/app.js",
    "heroku-postbuild": "cd client && npm run build",
    "heroku-prebuild": "cd client && npm install"
  },
  "dependencies": { //I Putted evrything here to dependencies
    "@material-ui/lab": "^4.0.0-alpha.30",
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",",
    "dotenv": "^8.2.0", ....

tsconfig

"target": "es6"
    "module": "commonjs"
    "outDir": "./build",
"include": [
    "api/"
  ]

package.json客户端

scripts ...
 "build": "npm run clean && webpack -p ---open --config webpack/production.js",
    "clean": "rimraf build/*",
    "tsc": "tsc --noEmit",
    "lint": "tslint './src/**/*.ts*' --format stylish --project . --force",
    "dev": "webpack-dev-server ---open --config webpack/development.js"

1 个答案:

答案 0 :(得分:0)

您的构建过程不应启动您的应用程序。更改

"heroku-postbuild": "cd client && npm run build",

"heroku-postbuild": "npm run tsc",

这将创建所需的构建文件夹。然后,您需要使用以下代码行将一个名为Procfile的新文件添加到项目的根目录:

web: node build/api.js

哪个告诉Heroku如何启动您的应用程序。