【问题标题】:Syntax Error: Unexpected Token async()语法错误:意外的令牌 async()
【发布时间】:2018-02-12 07:09:42
【问题描述】:

我一直在关注 this 使用 GraphQL 的教程,它告诉我在我的 src/index.js 文件中编写这段代码:

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();

虽然当我尝试使用 node ./src/index.js 运行代码时,它给了我这个错误:

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)

我在网上搜索过,似乎 if 可能是由 here 的 node.js 版本引起的,但我检查了我的 noed.js 版本,它是 8.3.0 所以应该支持它而不必使用如果我没记错的话,巴别塔。

这是我的package.json 文件:

{
  "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"
  }
}

【问题讨论】:

  • async 被视为函数名,箭头函数不接受名称。
  • @Teemu - 你确定吗?
  • 哪个版本的nodejs
  • @Teemu - 我现在明白了 - 我确实从语法中假设意图是使用(糟糕)async 函数 - 似乎确实如此
  • @YellowPillow 不要使用 Homebrew 来安装 Node.js,它只会带来悲伤。使用官方安装程序。

标签: javascript node.js asynchronous


【解决方案1】:

async functions 仅从节点 8.3 开始可用

您的代码等同于(没有 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;
    });
};

【讨论】:

  • 今天我尝试在 Node 6.x 中运行适用于 8.x 的代码时发生了这种情况,现在完全支持异步功能。
【解决方案2】:

通过命令node yourAppFile -harmony 启动您的应用程序! async函数在Node7下harmony模式下可用。+

【讨论】:

  • 这对 OP 有何帮助 - 运行 6.11.2(不会启用异步)或者如果他运行 8.3 就像 brew 告诉他的那样,他不需要标志
猜你喜欢
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 2019-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
相关资源
最近更新 更多