【问题标题】:How to get systemd to work with ts-node on raspberry pi?如何让 systemd 在树莓派上使用 ts-node?
【发布时间】:2021-04-26 20:11:53
【问题描述】:

我有一个树莓派,我正试图将它用作一个用 typescript 编写的 nodejs 应用程序的伪主机。 稍加阅读,我创建并启用了 systemd 服务,以便即使我没有 ssh 进入 pi 时也能保持进程运行。

systemd 服务如下所示:

[Unit]
Description=test ts app
Documentation=https://example.com
After=network.target

[Service]
Environment=NODE_PORT=3000
Type=simple
User=pi
Restart=on-failure
WorkingDirectory=/home/pi/Documents/NodeApp/
ExecStart=/home/pi/.nvm/versions/node/v14.16.1/lib/node_modules/ts-node/dist/bin.js src/server.ts

[Install]
WantedBy=multi-user.target

我已经设置了 ts-node 的全局路径,因为它没有找到它。使用这个 ExecStart 会出错,因为它没有看到 ts-node 路径后面的参数。

Apr 26 22:54:35 raspi npm[2814]: /home/pi/.nvm/versions/node/v14.16.1/lib/node_modules/ts-node/dist/bin.js:14
Apr 26 22:54:35 raspi npm[2814]: function main(argv = process.argv.slice(2), entrypointArgs = {}) {
Apr 26 22:54:35 raspi npm[2814]:                    ^
Apr 26 22:54:35 raspi npm[2814]: SyntaxError: Unexpected token =

节点版本:v14.16.1 Npm 版本:6.14.12。 不太确定接下来如何解决这个问题,任何点击都会有所帮助。谢谢!

【问题讨论】:

    标签: node.js raspberry-pi systemd ts-node


    【解决方案1】:

    通过将这个 shebang 添加到我的 .ts 应用程序的开头,我能够让 ts-node systemd 服务工作:#!/usr/bin/env ts-node-script

    (记得 chmod +x 你的 .ts 文件)

    我从这个答案中得到了它:

    Error while executing typescript file with ts-node (after installing the project as global module)

    我也将您的问题用作资源,所以感谢您提出这个问题。

    我的另一个建议(和当前的后备)是您可以将 ts 文件编译为 javascript 并仅使用 node.js 文件。如果您的 ts 程序要持续运行,则可能会出现 [警告:传闻] 内存泄漏问题。

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多