使用ts节点部署Heroku

gupuwyp2  于 2022-11-13  发布在  其他
关注(0)|答案(9)|浏览(148)

我正在Heroku中尝试部署Angular Universal Starter的示例。
任务npm start失败,因为它无法识别ts-node。
有没有办法使用ts-node在Heroku上进行部署?

qojgxg4l

qojgxg4l1#

您可以使用以下命令创建Procfile

web: npm start

并让启动脚本运行ts-node

"start": "ts-node src/index.ts",

并在依赖项上安装typescript和ts-node

"ts-node": "^3.3.0",
"typescript": "^2.4.2"
2wnc66cl

2wnc66cl2#

你不能直接把ts-node部署到heroku。要么构建你自己的buildpack,要么把typescript编译成javascript。我推荐后者。只需在发布时运行tsc -p .命令。

编辑您也可以创建一个名为index.js的文件并添加以下内容:

require('ts-node/register');
require('./server.ts');

那么它应该可以工作了。不要忘记运行npm install --save-dev ts-node

不建议在生产环境中使用此选项。请仅在开发环境中使用。

20jt8wwn

20jt8wwn3#

软件包. json:

"start":"ts-node index.ts",
"dependencies": { "ts-node" : "~<version number>" }

为我工作:)

7lrncoxx

7lrncoxx4#

要解决heroku中与缺少ts-node相关的问题,您需要在package.json中使用tsc命令构建TypeScript代码。

"scripts": {
    ...
    "build": "tsc",
  }

然后在Procfile中,您可以使用node命令运行Heroku为您构建的文件,方法是在package.json中运行build命令。

web: node dist/src/server.js

就我个人而言,我认为这是将基于TypeScript的应用程序部署到Heroku的最佳解决方案,如果您还需要tsconfig.json,您可以看看我的解决方案。

{
  "extends": "@araclx/tsconfig",
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "noUnusedParameters": false,
    "noUnusedLocals": false,
    "lib": ["ES2018"],
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist"
  }
}
1szpjjfi

1szpjjfi5#

也许我迟到了,但我在这周遇到了同样的问题,这是我在网上收集了多种解决方案来解决这个问题的方法。希望它能帮助一些人:
将节点和Yarn或npm版本添加到package.json

"engines": {
    "node": "12.14.0",
    "yarn": "1.x"
  },

package.json上的项目依赖项中添加了ts-node和typescript

"ts-node": "^9.0.0",
"typescript": "^4.1.2"

添加了postinstall脚本,该脚本在Heroku调用的yarn或npm安装后调用,这将在选定的目标文件夹(本例中为dist)上构建您的应用程序
并将启动脚本更改为服务已编译的应用程序,而不是TS应用程序

"scripts": {
    "postinstall": "tsc",
    "start": "cross-env NODE_ENV=production node dist/index.js"
  },

指向PROCFILE上的启动脚本

web: yarn start

最后是tsconfig.json,您可以在其中选择构建目标"outDir": "dist",

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "target": "es2018",
        "moduleResolution": "node",
        "module": "commonjs",
        "sourceMap": true,
        "rootDir": "src",
        "outDir": "dist",
        "lib": ["esnext", "dom", "es2018", "esnext.asynciterable"],
        "esModuleInterop": true
    },
    "exclude": ["node_modules"]
}
juud5qan

juud5qan6#

由于未知原因,Heroku将ts-node作为dev依赖项进行清理,即使您将其列在 dependencies 而不是 devDependencies 中也是如此。您应该使用以下命令指示Heroku不要清理devDependencies:

$ heroku config:set NPM_CONFIG_PRODUCTION=false YARN_PRODUCTION=false
ahy6op9u

ahy6op9u7#

我 最近 在 2022 年 遇到 了 这个 问题 , 这 是 最 上面 的 答案 , 所以 我 正在 添加 我 的 解决 方案 。

# # # 首先 , 为了 消除 您 不 应该 在 生产 中 使用 ts 节点 的 误解 。

请 参阅 此处 引用 的@Benny Neugebauer 的 this answer
根据 ts-node 的 作者 Blake Embrey 的 说法 , 您 可以 在 生产 中 使用 它 , 但是 您 应该 将 它 与 ' - - transpile-only 标志 一起 使用 。
示例 :

ts-node --transpile-only ./src/start.ts

中 的 每 一 个
此外 , 您 应该 从 package.json 中 的 "start": "src/start.ts" 中 删除 .ts , 并 添加 类型 as per the typescript doc

...
    "start":   "ts-node --transpile-only ./src/start",
    "typings": "src/start",
...

格式
并且 您 还 可以 按照 说明 使 tsconfig 文件 包含 以下 内容 :

"compilerOptions": {
...
    /* Strict Type-Checking Options */
    "strict": false,                                 /* Enable all strict type-checking options. */
...
    },
  "exclude": ["node_modules"]

格式
但 仍然 会 出现 错误

# # # 真正 的 问题 :

如果 您 使用 heroku-22 服务 器 、 freehobby 的 基本 配置 , 并 从 节点 切换 到 TS 构建 , 您 可能 会 注意 到 部署 和 启动 后 的 生产 错误 , 如下 所 示 :

TSError: ⨯ Unable to compile TypeScript:
 server.ts(1,29): error TS7016: Could not find a declaration file for module 'body-parser'. '/app/node_modules/body-parser/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/body-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'body-parser';`
 src/app.ts(2,26): error TS7016: Could not find a declaration file for module 'express'. '/app/node_modules/express/index.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module 'express';`
...
diagnosticCodes: [ 7016, 7016, 2307, 7016 ]
Process exited with status 1
State changed from starting to crashed
...

格式
问题 是 Heroku Buildpack heroku/nodejsangular/universal-starter 使用 的 , 没有 一 个 额外 的 步骤 就 没有 运行 ts-node 的 能力 。

heroku config:set NPM_CONFIG_PRODUCTION=false YARN_PRODUCTION=false --app <YOUR_APP_NAME>

格式
@anynomius 在 他们 的 回答 here 中 提到 的 是 最终 帮助 我 并 修复 了 我 遇到 的 问题 的 东西 。

3htmauhk

3htmauhk8#

对于我们的项目,问题是,ts-node只在package.json中的devDependencies中,但Heroku在运行时需要此依赖关系,因此应将其添加到dependencies中。

p4rjhz4m

p4rjhz4m9#

以下是适合我的解决方案
步骤:1 -安装以下两个软件包

npm i ts-node typescript --save-prod

步骤2:-将以下脚本添加到脚本内的package.json

"scripts": {
    "start": "npm index.js",
    "heroku-start": "ts-node \"./src/server.ts\"",
}

步骤3:-在Procfile中添加以下命令(位于根级别)

web: npm run heroku-start

第4步:提交更改并在Heroku上部署

相关问题