Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use ES6 default variables with create-react-app? SOLVED #512

Closed
richb-hanover opened this issue Aug 28, 2016 · 2 comments
Closed

Can't use ES6 default variables with create-react-app? SOLVED #512

richb-hanover opened this issue Aug 28, 2016 · 2 comments

Comments

@richb-hanover
Copy link

I have a working piece of code that I want to port to a create-react-app project, and stumbled over the compile error below for my server-side. Am I missing something?

...
> server-api@ server /Users/richb/github/cloudmu-react-redux-starter-kit/server
> node server.js

/Users/richb/github/cloudmu-react-redux-starter-kit/server/utils/protocol.js:683
  function bump(stat, amount=1) {
                            ^
SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
...

[I also had to add "use strict"; to the head of the file to get farther....]

@richb-hanover richb-hanover changed the title Can't use ES6 default variables with create-react-app? Can't use ES6 default variables with create-react-app? SOLVED Aug 28, 2016
@richb-hanover
Copy link
Author

richb-hanover commented Aug 28, 2016

Answered my own question. Of course, create-react-app doesn't handle ES6->ES5 conversion for server code - it's client-side only.

To fix it, I added babel processing for my server code by running the following in my server folder:

npm install --save babel-core babel-cli babel-preset-es2015 babel-preset-stage-0

and also added the following to the server package.json scripts section:

"server": "API_PORT=3001 ./node_modules/.bin/babel-node server.js"

I can now use npm run server and the server-side code works as desired. (I could get rid of the "use strict"; as well.) Here's the total server/package.json file:

{
  "description": "This is for the api server",
  "name": "server-api",
  "babel": {
    "presets": [
      "es2015",
      "stage-0"
    ]
  },
  "dependencies": {
    "babel-cli": "^6.14.0",
    "babel-core": "^6.14.0",
    "babel-preset-es2015": "^6.14.0",
    "babel-preset-stage-0": "^6.5.0",
    "body-parser": "^1.15.2",
    "express": "^4.14.0"
  },
  "scripts": {
    "server": "API_PORT=3001 ./node_modules/.bin/babel-node server.js"
  }
}

@gaearon
Copy link
Contributor

gaearon commented Aug 31, 2016

Yeah, we don’t really support any server solution out of the box, that is up to you.
There are some non-trivial pieces there. For example using babel-node is bad in production.

@gaearon gaearon closed this as completed Aug 31, 2016
@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants