gpt4 book ai didi

javascript - 尽管有正确的预设/插件,但 Babel 在 async/await 语法上抛出错误

转载 作者:行者123 更新时间:2023-11-30 00:02:29 24 4
gpt4 key购买 nike

我正在尝试使用 Babel 来编译异步/等待函数,但我收到编译器错误。

这是我的功能:

async function login(username, password) {
try {
const response = await request
.post("/api/login")
.set("Accept", "application/json")
.send({username, password})
.end();

const {user} = response.body;
console.log("Login success:", user);
this.user = user;
this.loginError = null;
} catch(error) {
console.log(`Login failed: ${error}`);
this.user = null;
this.loginError = error;
}
}

这里是错误:

ERROR in ./src/store/store.js
Module parse failed: /home/james/projects/Issue-Tracker/node_modules/babel-loader/index.js!/home/james/projects/Issue-Tracker/src/store/store.js The keyword 'await' is reserved (20:25)
You may need an appropriate loader to handle this file type.
SyntaxError: The keyword 'await' is reserved (20:25)
at Parser.pp$4.raise (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp$3.parseIdent (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:2182:14)
at Parser.pp$3.parseExprAtom (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1774:21)
at Parser.pp$3.parseExprSubscripts (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1715:21)
at Parser.pp$3.parseMaybeUnary (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1692:19)
at Parser.pp$3.parseExprOps (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1637:21)
at Parser.pp$3.parseMaybeConditional (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1620:21)
at Parser.pp$3.parseMaybeAssign (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1597:21)
at Parser.pp$3.parseMaybeAssign (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1608:25)
at Parser.pp$3.parseExpression (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:1573:21)
at Parser.pp$1.parseStatement (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:727:47)
at Parser.pp$1.parseBlock (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:981:25)
at Parser.pp$1.parseTryStatement (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:897:23)
at Parser.pp$1.parseStatement (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:702:31)
at Parser.pp$1.parseSwitchStatement (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:873:36)
at Parser.pp$1.parseStatement (/home/james/projects/Issue-Tracker/node_modules/acorn/dist/acorn.js:700:34)
@ ./src/store/index.js 7:13-31

这是我的.babelrc:

{
"presets": ["latest", "react"],
"plugins": ["transform-decorators-legacy", "transform-object-rest-spread", "transform-class-properties"]
}

根据 babel-preset-latest 的文档,它包括 preset-es2017,其中包括 transform-async-to-generator,所以我不明白为什么这不会编译。

如有任何帮助,我们将不胜感激。

最佳答案

package.json

{
"dependencies": {
"babel-cli": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.18.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-rest-spread": "^6.16.0",
"babel-preset-latest": "^6.16.0",
"babel-preset-react": "^6.16.0"
}
}

.babelrc

{
"presets": ["latest", "react"],
"plugins": [
"transform-decorators-legacy",
"transform-object-rest-spread",
"transform-class-properties"
]
}

如果您要构建的文件的名称是 file.js 并且:

$ node_modules/.bin/babel file.js -o output.js

你应该有 output.js

"use strict";

var login = function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(username, password) {
var response, user;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return request.post("/api/login").set("Accept", "application/json").send({ username: username, password: password }).end();

case 3:
response = _context.sent;
user = response.body.user;

console.log("Login success:", user);
this.user = user;
this.loginError = null;
_context.next = 15;
break;

case 10:
_context.prev = 10;
_context.t0 = _context["catch"](0);

console.log("Login failed: " + _context.t0);
this.user = null;
this.loginError = _context.t0;

case 15:
case "end":
return _context.stop();
}
}
}, _callee, this, [[0, 10]]);
}));

return function login(_x, _x2) {
return _ref.apply(this, arguments);
};
}();

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

关于javascript - 尽管有正确的预设/插件,但 Babel 在 async/await 语法上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39879839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com