我想在服务器端编译 src,而不是上传预编译的 dist 目录。
这是我在 package.json 中的脚本:
"scripts": {
"test": "echo \"No test specified\" && exit 0",
"start": "node dist/app.js",
"postinstall": "tsc"
}
以下是依赖项:
"dependencies": {
"@types/express": "^4.11.1",
"@types/pg": "^7.4.4",
"@types/socket.io": "^1.4.31",
"body-parser": "^1.18.2",
"express": "^4.16.2",
"pg": "^7.4.1",
"socket.io": "^2.0.4",
"tslint": "^5.9.1",
"typescript": "^2.7.2"
}
由于“npm install在安装过程中会将node_modules/.bin文件夹添加到PATH环境变量中”,Heroku应该可以直接调用它。
但是这是我得到的错误:
Building dependencies
Installing node modules (package.json + package-lock)
> [email protected] postinstall /tmp/build_afa42c7943d4b71d2b48a016ae3b9e50
> tsc
sh: 1: tsc: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] postinstall: `tsc`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /tmp/npmcache.LTxbD/_logs/2018-02-25T10_36_06_374Z-debug.log
-----> Build failed
您需要从 npm 脚本调用
tsc
。否则,Heroku 会尝试查找名为 tsc
的全局依赖项。
在您的
package.json
中创建一个新的 npm 脚本:
"tsc": "tsc"
现在将
"postinstall": "tsc"
替换为:
"postinstall": "npm run tsc"
Typescript 应作为开发依赖项安装
有
web: node server.js
在你的 procfile 中
确保将 npm build 添加为安装后脚本
告诉
npm
typescript 已在本地安装将修复 tsc 未找到问题,因为 npm 正在尝试在 heroku 上全局查找它。
像这样。
"tsc": "./node_modules/typescript/bin/tsc",
"build": "tsc",
"postinstall": "npm run build",
花了一些时间将我的简单打字稿 create-react-app 部署到 Heroku。这对我有用。
package.json - 根本不需要安装后
在命令行中为您的应用程序安装构建包 跑步: heroku buildpacks:添加 zidizei/typescript heroku buildpacks:添加 heroku/nodejs
您还可以搜索构建包 跑步: heroku buildpacks:搜索打字稿
我的服务器看起来像这样(/root/server.js)
const express = require('express')
const app = express()
const PORT = process.env.PORT || 3000
app.use(express.static('build'));
app.listen(PORT, () => console.log(`Listening on port ${PORT}`))
package.json
"scripts": {
"start": "node server.js",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
另外,在推送到 Heroku 之前,请运行“npm run build”。
如果你要使用 webpack 开发服务器,我的解决方案将不起作用,它必须是自定义服务器,在我的例子中它是 EXPRESS。
只需安装 typescript 作为依赖项即可工作
对于我来说,在 package.json 中
"scripts": {
"tsc": "./node_modules/typescript/bin/tsc",
"postinstall": "npm run tsc"
},
对我有用。
我发现有 2 个解决方案可以解决这个问题:
我也尝试了这里的所有答案来修复
sh: 1: tsc: not found
错误,但我唯一能做的就是将打字稿从开发依赖项移至依赖项。
我将 typescript 安装为 devDependency 并在 Package.json 中:
"scripts": {
//other scripts
"build": "./node_modules/typescript/bin/tsc",
}