0

I have been following this tutorial on using GraphQL and it has told me to write this block of code in my src/index.js file:

const express = require('express');
const bodyParser = require('body-parser');
const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');
const schema = require('./schema');

// 1
const connectMongo = require('./mongo-connector');

// 2
const start = async () => {
  // 3
  const mongo = await connectMongo();
  var app = express();
  app.use('/graphql', bodyParser.json(), graphqlExpress({
    context: {mongo}, // 4
    schema
  }));
  app.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql',
  }));

  const PORT = 3000;
  app.listen(PORT, () => {
    console.log(`Hackernews GraphQL server running on port ${PORT}.`)
  });
};

// 5
start();

Though when I try to run the code using: node ./src/index.js it gives me this error:

const start = async () => {
                    ^

SyntaxError: Unexpected token (
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)

I've searched online and it seems that if might be caused by the node.js version from here but I've checked my noed.js version and it's 8.3.0 so it should be supported without having to use Babel if I'm not mistaken.

Here is my package.json file:

{
  "name": "graphql-js-tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "apollo-server-express": "^1.1.2",
    "body-parser": "^1.17.2",
    "express": "^4.15.4",
    "graphql": "^0.11.2",
    "graphql-tools": "^1.2.2",
    "mongodb": "^2.2.31"
  }
}
YellowPillow
  • 4,100
  • 6
  • 31
  • 57

2 Answers2

5

async functions are only available since node 8.3

your code is equivalent to (without async/await)

const start = () => {
    return connectMongo().then(mongo => {
        var app = express();
        app.use('/graphql', bodyParser.json(), graphqlExpress({
            context: {mongo}, // 4
            schema
        }));
        app.use('/graphiql', graphiqlExpress({
            endpointURL: '/graphql',
        }));

        const PORT = 3000;
        app.listen(PORT, () => {
            console.log(`Hackernews GraphQL server running on port ${PORT}.`)
        });
        return;
    });
};
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • This happened to me today when I was trying to run code in Node 6.x that was intended for 8.x where async functions are now fully supported. – rainabba Apr 13 '18 at 18:20
-1

start your app by the command node yourAppFile -harmony! async function is available in the harmony mode under Node7.+

lx1412
  • 1,160
  • 6
  • 14
  • How does that help the OP - running `6.11.2` (which wont enable async) or if he's running `8.3` like `brew` is telling him, he doesn't need the flag – Jaromanda X Sep 04 '17 at 09:02