Typescript 为什么';tsc在Heroku运行吗?
我有一个React Express TypeScript应用程序,我一直在努力部署到Heroku。目的是部署应用程序,而无需手动创建客户端Typescript 为什么';tsc在Heroku运行吗?,typescript,heroku,deployment,tsc,Typescript,Heroku,Deployment,Tsc,我有一个React Express TypeScript应用程序,我一直在努力部署到Heroku。目的是部署应用程序,而无需手动创建客户端build(React脚本)和服务器dist(tsc)文件夹,即这些文件夹应在Heroku中部署期间生成。目前,成功构建了build文件夹,但没有成功构建dist文件夹。我在bash模式下运行部署的应用程序并查看文件夹,从而再次检查了这一点。tsc似乎不在服务器端运行,但在部署期间没有关于这方面的警告或错误 为了提供一些概述,我提供了以下文件夹结构(为了简洁起
build
(React脚本)和服务器dist
(tsc)文件夹,即这些文件夹应在Heroku中部署期间生成。目前,成功构建了build
文件夹,但没有成功构建dist
文件夹。我在bash模式下运行部署的应用程序并查看文件夹,从而再次检查了这一点。tsc似乎不在服务器端运行,但在部署期间没有关于这方面的警告或错误
为了提供一些概述,我提供了以下文件夹结构(为了简洁起见,省略了许多文件夹和文件):
…并且server/tsconfig.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"
}
}
{
"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"
}
}
我还没有找到解决办法。所以我最终找到了一个解决办法。
如果有人正在处理同样的问题,我所做的就是在服务器上手动运行
tsc
,以创建server/dist
文件夹。这不是我一直在寻找的解决方案,但它现在起作用。您混淆了标志的顺序,应该使用npm--prefix server run tsc
@FalseDev感谢您的回复。我只是试了一下,没什么区别。PS如果标志的顺序很重要,则不会生成客户端build
文件夹。同样,客户端和服务器文件夹中的依赖项安装也是如此。请将其放入构建脚本中,而不是后期构建
,后期构建不允许修改文件系统,并且用于数据库之类的内容migrations@FalseDevHeroku文档说明,如果存在Heroku后期构建
脚本,将跳过build
脚本,而运行postbuild
脚本。我在构建日志中也看到了这一点。在此步骤中创建了client/build
文件夹这一事实也告诉我,在postbuild
脚本中使用这些命令没有问题。总结一下,我想说的是,除了tsc
命令之外,它似乎正常工作。我还可以提到,我以前在postbuild
脚本中使用了heroku prebuild
命令,没有任何问题。
{
...,
"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"
}
}