Forever errors with babel-node
Asked Answered
L

4

10

I have a simple node server:

//server.js

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

When I try to run this using Forever:

forever start -c "babel-node --experimental" server.js , it errors out due to use of 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. For pm2, I followed this issue https://github.com/Unitech/PM2/issues/1167, but it didn't work either. What am I doing wrong here?

Laplante answered 16/6, 2015 at 8:35 Comment(2)
With PM2 just do: pm2 start app.js --interpreter ./node_modules/.bin/babel-nodeBlather
It appears you forgot to define .babelrc file with presets by defaultStalagmite
W
10
forever start -c "node -r babel-register" ./src/index.js

Also works.

Walt answered 18/10, 2016 at 1:55 Comment(0)
J
8

This works for on-the-fly transpilation for me: forever start -c node_modules/.bin/babel-node server.js

Another solution is using the Require Hook like this:

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

require('./server.js');

Then run forever start server-wrapper.js.

Jumper answered 13/7, 2015 at 15:4 Comment(0)
A
1

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.

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).

So I see no profit to work with es6 scripts via on demand compilation versus precompilation with gulp or any other js building system.

Arciform answered 19/6, 2015 at 9:42 Comment(2)
Did you mean to say "compilation" instead of "compilcation" throughout?Catarinacatarrh
The profit is obvious: You don't need to precompile.Jumper
D
0

In your package.json file under scripts tag add entry like below

in package.json under scripts tag

"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

then do a npm install then run the server by executing npm start

Dregs answered 8/3, 2019 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.