I am encountering a compiler error while attempting to compile an async/await function using Babel.
Below is the function in question:
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;
}
}
The error message received is as follows:
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)
... (remaining error log omitted for brevity)
Additionally, here is my .babelrc
configuration:
{
"presets": ["latest", "react"],
"plugins": ["transform-decorators-legacy", "transform-object-rest-spread", "transform-class-properties"]
}
Although the documentation for babel-preset-latest suggests that it should handle transform-async-to-generator with preset-es2017, I am still facing compilation issues. Any insights or assistance on resolving this problem would be highly appreciated.