Reputation: 186
I have some async functions that I have used in multiple projects, however when I added them to another project they suddenly aren't working.
An example of one of these functions is:
const ddbGet = async (params) => {
try {
const data = await docClient.get(params).promise();
return data;
} catch (err) {
console.log("Failure", err.message);
return false;
}
};
The error that gets thrown is:
const ddbGet = async (params) => {
^
SyntaxError: Unexpected token (
However I know there are no problems with the functions' syntax as they are used successfully elsewhere.
I have seen some answers to other questions that have suggested some issues with JSHint and ESLint however I don't believe I was using either of these, but just to be sure I installed ESLint and specified the ECMA version as suggested in those answers and this error was still thrown.
I have also made sure I'm using the latest version of Node.js.
And if I remove those functions an error gets thrown due to an asynchronous function in the node_modules folder.
async handshake(transportName, req, closeConnection) {
`^^^^^^^^^`
SyntaxError: Unexpected identifier
Does anybody know what the problem could be? Thanks.
Upvotes: 0
Views: 507
Reputation: 186
I've found the solution with a little more research after Gaëtan Boyals put me on the right path!
He was correct that the default version of Node.js was the issue and the solution to fix this was nvm alias default 17.2.0
Upvotes: 0
Reputation: 1228
Since the documentation, or rather AWS Cloud9's Getting Started isn't kept up-to-date (using an old script to install NVM) and seeing that the project was very old (as per the extended comment section of the question), it surely was the NodeJS version.
This is confirmed by the nvm ls
output the OP gave us (I only formatted it):
v6.15.1
-> v17.2.0 system
default -> 6 (-> v6.15.1)
node -> stable (-> v17.2.0) (default)
stable -> 17.2 (-> v17.2.0) (default)
iojs -> N/A (default)
lts/* -> lts/argon (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.9 (-> N/A)
lts/fermium -> v14.18.3 (-> N/A)
lts/gallium -> v16.13.2 (-> N/A)
The line default -> 6 (-> v6.15.1)
informs us that the default NodeJS version used (for the whole system) is v6.15.1
.
Unfortunately, NodeJS does NOT handle async/await syntax natively before v7.6
.
You could run nvm install 17.2.0
and then nvm use 17.2.0
(or simply the nvm use
part, but I don't know if the v17.2.0
we see in the output have effectively been installed via NVM), which is likely to fix this particular problem.
Upvotes: 2