简体   繁体   English

babel-node的永远错误

[英]Forever errors with babel-node

I have a simple node server: 我有一个简单的节点服务器:

//server.js //server.js

import express  from 'express';
import React    from 'react';
...

When I try to run this using Forever : 当我尝试使用Forever运行时:

forever start -c "babel-node --experimental" server.js , it errors out due to use of import forever start -c "babel-node --experimental" server.js ,它因使用import而出错

/Applications/MAMP/htdocs/React/ReactBoilerplates/koba04/app/server.js:1
(function (exports, require, module, __filename, __dirname) { import express  
                                                              ^^^^^^
SyntaxError: Unexpected reserved word
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
error: Forever detected script exited with code: 8

I have also tried pm2 and nodemon , I get same error there as well. 我也曾尝试PM2nodemon ,我得到同样的错误那里。 For pm2, I followed this issue https://github.com/Unitech/PM2/issues/1167 , but it didn't work either. 对于pm2,我遵循了这个问题https://github.com/Unitech/PM2/issues/1167 ,但它也没有用。 What am I doing wrong here? 我在这做错了什么?

forever start -c "node -r babel-register" ./src/index.js

也有效。

This works for on-the-fly transpilation for me: forever start -c node_modules/.bin/babel-node server.js 这适用于我的动态转换: forever start -c node_modules/.bin/babel-node server.js

Another solution is using the Require Hook like this: 另一种解决方案是使用Require Hook,如下所示:

// server-wrapper.js
require('babel/register');

require('./server.js');

Then run forever start server-wrapper.js . 然后forever start server-wrapper.js运行forever start server-wrapper.js

I suggest to precompile your es6 scripts into es5 scripts and run the app with a forever start server.js command where server.js is a result of precompilation. 我建议将es6脚本预编译为es5脚本,并使用forever start server.js命令运行应用程序,其中server.js是预编译的结果。

If you're using react.js for an isomorphic app you also will be needed to precompile your scripts for browsers either (via browserify , webpack and so on). 如果您使用react.js一个同构的应用程序,你也将需要是(通过预编译您的浏览器脚本browserifywebpack等)。

So I see no profit to work with es6 scripts via on demand compilation versus precompilation with gulp or any other js building system. 因此,我认为没有利润一起工作es6脚本通过按需编译,而使用预编译gulp或任何其他JS建筑体系。

In your package.json file under scripts tag add entry like below scripts标签下的package.json文件中添加如下所示的条目

in package.json under scripts tag scripts标签下的package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "forever start -c babel-node src/index.js",
},

all the dependencies must include in dependencies tag in package.json file 所有依赖项必须包含在package.json文件中的dependencies标记中

then do a npm install then run the server by executing npm start 然后执行npm install然后通过执行npm start运行服务器

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

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