简体   繁体   English

如何在 pm2 中使用 babel-node

[英]How to use babel-node with pm2

I want to manage my node process with pm2 package.我想用 pm2 package 管理我的节点进程。 If I don't have any es6 syntax in my code so I don't need to add babel-node, and without any es6 syntax code is am able to run my code with pm2 with this line of code如果我的代码中没有任何 es6 语法,所以我不需要添加 babel-node,并且没有任何 es6 语法代码能够使用 pm2 使用这行代码运行我的代码

pm2 start server.js

But as soon as I add any line of code of es6 syntax like this但是只要我添加任何这样的 es6 语法代码行

import express from 'express';

I get the error of unexpected token import.我收到意外令牌导入的错误。

As you know to solve this issue we have to add babel-node package.如您所知,要解决此问题,我们必须添加 babel-node package。

But when I use this line of command to compile my code但是当我使用这行命令来编译我的代码时

pm2 start server.js --interpreter babel-node

I get this error我收到这个错误

Error: spawn babel-node ENOENT
at _errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)

The strange thing about this is that if I don't use pm2 and run the code with this line of code奇怪的是,如果我不使用 pm2 并使用这行代码运行代码

babel-node server.js

Every thing is OK.一切都好。

I've only just started using pm2 and I found this helped.我刚刚开始使用 pm2,我发现 这有帮助。

You have your npm script to run es6 js, something like你有你的 npm 脚本来运行 es6 js,比如

{
  ...
  "scripts": {
    "server": "babel-node ./src/server.js"
  },
  ...
}

Then to run that with pm2 you use pm2 start npm -- run server Make sure you have babel-cli installed globally.然后用 pm2 运行它,你使用pm2 start npm -- run server确保你已经全局安装了babel-cli

Install babel-cli in your project with yarn add babel-cli and then you can run pm2 start ./server.js --interpreter ./node_modules/babel-cli/bin/babel-node.js在你的项目中使用yarn add babel-cli安装 babel-cli 然后你可以运行pm2 start ./server.js --interpreter ./node_modules/babel-cli/bin/babel-node.js

With "@babel/cli": "^7.0.0" the path is ./node_modules/@babel/cli/bin/babel.js使用 "@babel/cli": "^7.0.0" 路径是./node_modules/@babel/cli/bin/babel.js

check pm2 log files to trace the error.检查 pm2 日志文件以跟踪错误。 the location of log files is at /root/.pm2/日志文件的位置在 /root/.pm2/

I fix this issue in my Node.js project that I use babel.我在使用 babel 的 Node.js 项目中修复了这个问题。

First you must have首先你必须有

"devDependencies": {
   "@babel/core": "^7.11.6",
   "@babel/node": "^7.10.5",
   "@babel/preset-env": "^7.11.5",
   "eslint": "^7.9.0"
}

in your package.json file.在您的 package.json 文件中。

And then you can use然后你可以使用

pm2 start bin/www --interpreter node_modules/@babel/node/bin/babel-node.js

command on your terminal.终端上的命令。 That's it, your project will stand up with babel configurations.就是这样,您的项目将支持 babel 配置。 :) :)

I was reading this I found something related to this point "Error: spawn babel-node ENOENT" I think the problem isn't with pm2, It is related to the path, I mean the problem is with your environment!我正在阅读这篇文章,我发现了与这一点相关的内容“错误:生成 babel-node ENOENT” 我认为问题不在于 pm2,它与路径有关,我的意思是问题在于您的环境!

well, how to fix it!嗯,怎么解决!

first, let me be really clear about this, I have been working with pm2 for about 4 years and it is the best in my opinion, but in windows environment it is creasy!首先,让我真正清楚这一点,我已经使用 pm2 大约 4 年了,在我看来它是最好的,但在 windows 环境中它很糟糕! soo my solution to not lose time, is nodemon for windows, if you are using Linux "WALA nop problems",所以我不浪费时间的解决方案是 Windows 的 nodemon,如果您使用的是 Linux“WALA nop 问题”,

my way!我的方式! in my server(Linux or Ubuntu) for production or testing, I always use pm2 and as a dependence, I install nodemon just in case my team wants to use it on windows!在我的服务器(Linux 或 Ubuntu)中用于生产或测试,我总是使用 pm2 作为依赖项,我安装 nodemon 以防我的团队想在 Windows 上使用它!

示例 babel pm2 nodemon

**red lines are for Linux and green for Windows ** **红线代表 Linux,绿线代表 Windows **

another way to use is to install ubuntu on windows from WINDOWS STORE另一种使用方法是从 WINDOWS STORE 在 windows 上安装 ubuntu

在此处输入图片说明

This allows you to run a Linux terminal on windows but you have to install nodejs on your local instance!这允许您在 Windows 上运行 Linux 终端,但您必须在本地实例上安装 nodejs!

this example was for a friend how was losing time loving pm2, he is using this way and he is happyYYYYY这个例子是给一个朋友如何失去时间爱pm2,他正在使用这种方式并且他很高兴YYYYY

use this command to run a project使用此命令运行项目

pm2 start --interpreter babel-node server.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM