So currently i'm developing a simple login and register system using AWS, but then I encountered this problem in AWS Lambda while testing my backend in postman, the full error code is shown below
{
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Unexpected token 'export'",
"stack": [
"Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token 'export'",
" at _loadUserApp (/var/runtime/UserFunction.js:222:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:300:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:34)",
" at Module._compile (internal/modules/cjs/loader.js:1085:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
" at Module.load (internal/modules/cjs/loader.js:950:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)",
" at internal/main/run_main_module.js:17:47"
]
}
Im currently running Node 14.x in my AWS Lambda, some of my codes are shown below
Index.js
const healthPath = '/health';
const registerPath = '/register';
const loginPath = '/login';
const verifyPath = '/verify';
const registerService = require('./services/register');
const loginService = require('./services/login');
const verifyService = require('./services/verify');
const util = require('./utils/util');
export const handler = async(event) => {
console.log('Request Event : ', event);
let response;
switch(true){
case event.httpMethod === 'GET'&& event.path === healthPath:
response = util.buildResponse(200);
break;
case event.httpMethod === 'POST'&& event.path === registerPath:
const registerBody = JSON.parse(event.body);
response = await registerService.register(registerBody);
break;
case event.httpMethod === 'POST'&& event.path === loginPath:
const loginBody = JSON.parse(event.body);
response = await loginService.login(loginBody);
break;
case event.httpMethod === 'POST'&& event.path === verifyPath:
const verifyBody = JSON.parse(event.body);
response = verifyService.verify(verifyBody);
break;
default:
response = util.buildResponse(404, 'Not Found');
}
return response;
};
Register.js
const util = require('../utils/util');
const bcrypt = require('bcryptjs')
const AWS = require('aws-sdk');
AWS.config.update({
region: 'eu-north-1'
})
const dynamodb = new AWS.DynamoDB.DocumentClient();
const userTable = 'userData';
async function register(userInfo) {
const name = userInfo.name;
const email = userInfo.email;
const username = userInfo.username;
const password = userInfo.password;
//check if all fields are filled by user
if(!username || !name || !email || !password){
return util.buildResponse(401, {
message: 'All fields are required!'
})
}
//check if username already exist or not
const dynamoUser = await getUser(username.toLowerCase().trim());
if(dynamoUser && dynamoUser.username){
return util.buildResponse(401,{
message: 'Username already taken'
})
}
//encrypt the password
const encryptedPw = bcrypt.hashSync(password.trim(), 10);
//creating new user object
const user = {
name: name,
email : email,
username: username.toLowerCase().trim(),
password: encryptedPw
}
//saving user to database
const saveUserResponse = await saveUser(user);
//if there is an error
if(!saveUserResponse){
return util.buildResponse(403,{
message: 'Server Error! Pelase try again later!'
})
}
//if successful
return util.buildResponse(200,{
username:username
})
}
async function getUser(username) {
const params = {
TableName: userTable,
Key: {
username: username
}
}
return await dynamodb.get(params).promise().then(response => {
return response.Item;
}, error => {
console.error('There is an error retrieving user!', error);
})
}
async function saveUser(user){
const params = {
TableName: userTable,
Item: user
}
return await dynamodb.put(params).promise().then(() => {
return true;
}, error => {
console.error('There is an error saving user!', error);
});
}
exports.register = register;
I tried to change the export code from exports to module.exports, upgrading the node version to 18.x, adding "type": "module" in package.json but it results in another error (didn't solve the problem) any help will be appreciated, thank you in advance!
Your error says
"SyntaxError: Unexpected token 'export'"
so try changing export const handler to module.exports.handler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With