我想在Heroku上部署使用TypeScript制作的Node.js服务器。但这是行不通的。我已经阅读了许多有关Stack Overflow的教程和其他问题,但都无济于事。在下面,我发布了tsconfig.json和package.json的代码。
package.json
"main": "build/index.js",
"scripts": {
"start": "node build/index.js",
"dev": "nodemon server.js",
"build-ts": "tsc",
"start:dev": "nodemon --config \"./nodemon.json\"",
"test": "echo \"Error: no test specified\" && exit 1",
"heroku-postbuild": "npm run build-ts"
},
"keywords": [],
"author": "Gabriel Meyer",
"license": "MIT",
"devDependencies": {
"@types/cors": "^2.8.6",
"@types/express": "^4.17.3",
"@types/node": "^12.12.31",
"nodemon": "^2.0.2",
"prettier": "^2.0.2",
"ts-node": "^8.8.1",
"tslib": "^1.11.1",
"tslint": "^6.1.0",
"typescript": "^3.8.3"
},
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"uuid": "^7.0.2",
"websocket": "^1.0.31"
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"strict": true,
"baseUrl": ".",
"rootDir": "src",
"outDir": "build",
"sourceMap": true,
"removeComments": true,
"experimentalDecorators": true,
"target": "ES6",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"importHelpers": true,
"strictPropertyInitialization": false,
"types": ["node"],
"typeRoots": ["node_modules/@types"]
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
index.ts
import express from 'express';
const app = express();
const port: string | number = process.env.PORT || 5000;
app.use('*', (req, res) => {
res.send('<h1>Welcome to your server!</h1>');
});
// create a server object:
app.listen(port, () => console.log(`hosting @${port}`));
答案 0 :(得分:0)
现在我找到了答案:
我的错误是,我已经将所有与Typescript相关的依赖项放入了 devDependencies 中。 Heroku需要 typescript , ts-node 和 tslib 作为常规依赖项,才能将.ts文件转换为.js文件。见下文:
package.json
"dependencies": {
"ts-node": "^8.8.1",
"tslib": "^1.11.1",
"typescript": "^3.8.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"uuid": "^7.0.2",
"websocket": "^1.0.31"
}