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._pile (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!
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._pile (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!
Share Improve this question edited Apr 20, 2023 at 15:51 InSync 11.1k4 gold badges18 silver badges56 bronze badges asked Apr 20, 2023 at 15:44 Muhammad Gajendra AdhirajasaMuhammad Gajendra Adhirajasa 711 gold badge1 silver badge3 bronze badges 5-
Looks like you're mixing
require()
-style module syntax with ES6 module syntax; they are not patible. – Pointy Commented Apr 20, 2023 at 15:46 - first of all, thank you for the response. If i want to use require, which other syntax do i need to change, i tried to change 'require' to 'import' but i got an error because of the aws-sdk, so i need to use require() – Muhammad Gajendra Adhirajasa Commented Apr 20, 2023 at 16:07
-
So with the old CommonJS module system, you use
require()
to import a module, and then setmodule.exports = { something }
, orexports.something = something
to export. – Pointy Commented Apr 20, 2023 at 16:25 - I think i used that code to export something from another codes beside index, i wrote exports.register = register; in the end of my register code to export the register function but i still got the error – Muhammad Gajendra Adhirajasa Commented Apr 20, 2023 at 17:53
- 1 @Pointy i just solved the problem by changing the index export module, i didnt realize that i wrote export const on the index file, thank you so much! – Muhammad Gajendra Adhirajasa Commented Apr 20, 2023 at 23:25
2 Answers
Reset to default 3Your error says
"SyntaxError: Unexpected token 'export'"
so try changing export const handler to module.exports.handler
Try to export as the below syntax
module.exports = {
yourFunctionName
};