0

I have had to modify the code on basic Node.js file in order to make it work and I am wondering why?

this fails:

const server = http.createServer((req, res) => {

this works:

var server = http.createServer(function(req, res){

Error:

/my-app/tmp/hello2.js:6 var server = http.createServer((req, res) => { ^ SyntaxError: Unexpected token > 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:945:3

Complete code

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

//const server = http.createServer((req, res) => {
// above *wont work*?? below works
var server = http.createServer(function(req, res){
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

// server.listen(port, hostname, () => {
// above *wont work*?? below works
server.listen(port, hostname, function() {
  console.log(`Server running at http://${hostname}:${port}/`);
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
  • What is the error? – Diego ZoracKy Mar 27 '17 at 14:21
  • What error do you get and what version of node are you using? – Skabbi Mar 27 '17 at 14:21
  • /my-app/tmp/hello2.js:6 var server = http.createServer((req, res) => { ^ SyntaxError: Unexpected token > 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:945:3 – Michael Nelles Mar 27 '17 at 14:23
  • 1
    Please add the code and error to the QUESTION instead of posting comments. Please see my update of your question – mplungjan Mar 27 '17 at 14:24
  • Although the question is a duplicate, the answers to the other question is outdated. We no longer need Babel for stuff like this. – DatBassie Mar 27 '17 at 18:30

1 Answers1

3

This is because your Node.js does not support as standard some functionalities of ES6

Two solutions

  1. You have to edit your package.json

    {
      "dependencies": {
      "babel-cli": "^6.0.0",
      "babel-preset-es2015": "^6.0.0"
     },
     "scripts": {
       "start": "babel-node --presets es2015 app.js"
     }
    }
    

    And run npm start

    More info on: How to run Node.js app with ES6 features enabled?

  2. Update your Node.js

    $ sudo npm cache clean -f
    $ sudo npm install -g n
    $ sudo n stable
    
Community
  • 1
  • 1
renno
  • 2,659
  • 2
  • 27
  • 58
  • 3
    I think updating to a newer version of Node should be enough, arrow functions are definitely supported in any stable version of Node. – DatBassie Mar 27 '17 at 14:25
  • That is another solution for sure. I'll edit my answer – renno Mar 27 '17 at 14:28
  • had the same problem. i installed new version of node that supported es6. 'nvm use v11.0.0' – Deke Mar 30 '19 at 22:17