[英]Forever errors with babel-node
我有一個簡單的節點服務器:
//server.js
import express from 'express';
import React from 'react';
...
當我嘗試使用Forever運行時:
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
我也曾嘗試PM2和nodemon ,我得到同樣的錯誤那里。 對於pm2,我遵循了這個問題https://github.com/Unitech/PM2/issues/1167 ,但它也沒有用。 我在這做錯了什么?
forever start -c "node -r babel-register" ./src/index.js
也有效。
這適用於我的動態轉換: forever start -c node_modules/.bin/babel-node server.js
另一種解決方案是使用Require Hook,如下所示:
// server-wrapper.js
require('babel/register');
require('./server.js');
然后forever start server-wrapper.js
運行forever start server-wrapper.js
。
我建議將es6
腳本預編譯為es5
腳本,並使用forever start server.js
命令運行應用程序,其中server.js
是預編譯的結果。
如果您使用react.js一個同構的應用程序,你也將需要是(通過預編譯您的瀏覽器腳本browserify
, webpack
等)。
因此,我認為沒有利潤一起工作es6
腳本通過按需編譯,而使用預編譯gulp
或任何其他JS建築體系。
在scripts
標簽下的package.json
文件中添加如下所示的條目
在scripts
標簽下的package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "forever start -c babel-node src/index.js",
},
所有依賴項必須包含在package.json
文件中的dependencies標記中
然后執行npm install
然后通過執行npm start
運行服務器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.