【发布时间】:2021-01-28 20:51:24
【问题描述】:
我有一个 React Express TypeScript 应用程序,我一直在努力将其部署到 Heroku。目的是在不手动创建客户端build(React 脚本)和服务器dist(tsc)文件夹的情况下部署应用程序,即这些文件夹应在 Heroku 部署期间构建。目前,build 文件夹已成功构建,但dist 文件夹未构建成功。我通过在 bash 模式下运行部署的应用程序并浏览文件夹来仔细检查了这一点。 tsc 似乎没有在服务器端运行,但在部署期间没有关于此的警告或错误。
为了提供一些概述,我有以下文件夹结构(为简洁起见,省略了许多文件夹和文件):
|-- client
| |-- public
| |-- src
| |-- package.json
| '-- tsconfig.json
|-- server
| |-- dist <-- **THIS FOLDER DOES NOT BUILD DURING DEPLOYMENT**
| |-- src
| |-- package.json
| '-- tsconfig.json
'-- package.json
我的package.json 文件(在根文件夹中)如下所示:
{
... ,
"scripts": {
"start": "node server/dist/index.js",
"tsc": "tsc",
"build": "react-scripts build",
"heroku-prebuild": "npm install --prefix server && npm install --prefix client",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false && NODE_ENV=production && npm run tsc --prefix server && npm run build --prefix client"
},
... ,
"engines": {
"node": "12.13.1",
"npm": "6.12.1"
}
}
...server/tsconfig.json 文件如下所示:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": [
"dist",
"node_modules"
],
"include": [
"src"
]
}
我的理解是heroku-postbuild 中的npm run tsc --prefix server 应该可以解决问题,但事实并非如此。我可能在这里遗漏了一些东西。也许我的文件夹结构不正确,但我觉得这不应该是部署应用程序的问题。
如果能获得任何帮助或指向正确方向的指示,我将不胜感激。
编辑:
我还应该提到,TypeScript 已作为依赖项添加到 server/package.json:
{
...,
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.2.0",
"eslint": "^7.9.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-plugin-import": "^2.22.0",
"nodemon": "^2.0.4"
},
"dependencies": {
"@types/chai": "^4.2.13",
"@types/express": "^4.17.8",
"@types/jest": "^26.0.14",
"@types/socket.io": "^2.1.11",
"@types/socket.io-client": "^1.4.34",
"@types/supertest": "^2.0.10",
"@typescript-eslint/parser": "^4.4.1",
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"express": "^4.17.1",
"jest": "^26.5.3",
"socket.io": "^2.3.0",
"socket.io-client": "^2.3.1",
"supertest": "^5.0.0",
"ts-jest": "^26.4.1",
"typescript": "^4.0.3"
}
}
【问题讨论】:
-
你混淆了标志的顺序,你应该使用
npm --prefix server run tsc -
@FalseDev 感谢您的回复。我刚刚尝试过,但没有任何区别。 PS 如果标志的顺序很重要,客户端
build文件夹将不会被构建。客户端和服务器文件夹中的依赖项安装也是如此。 -
把它放在你的构建脚本中而不是
post-build,post-build 不允许文件系统修改,并且用于数据库迁移之类的事情 -
@FalseDev Heroku 文档指出,如果有
heroku-postbuild脚本,build脚本将被跳过,postbuild脚本将运行。我也在构建日志中看到了这一点。在此步骤中创建了client/build文件夹这一事实也告诉我,在postbuild脚本中使用这些命令没有问题。总结一下我在这里想说的是,除了tsc命令之外,它似乎可以正常工作。我还可以提一下,我之前在postbuild脚本中使用了heroku-prebuild命令,没有任何问题。
标签: typescript heroku deployment tsc