Error: The AWS Lambda handler is throwing a TypeError, indicating that the callback must be a function

I clicked on the link: How Can I create an AWS Lambda Script for Running a Protractor / Selenium Browser Automation Script? Instead of implementing my own code, I used the following code snippet in the handler section based on the provided answer:

'use strict';
module.exports.runtest = (event, context, callback) => {

  var npm = require('npm');
  var path = require('path');
  var childProcess = require('child_process');
  var args = ['conf.js'];

  npm.load({}, function() {
    var child = childProcess
    .fork(path.join(npm.root, 'protractor/bin/protractor'), args)
    .on('close', function(errorCode) {
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          message: `Selenium Test executed on BrowserStack!  Child process Error Code: ${errorCode}`,
        }),
      };
      callback(null, response);
    });
    process.on('SIGINT', child.kill);
  });
};

However, when I invoke the lambda function, it throws the error displayed below:

{
  "errorType": "TypeError",
  "errorMessage": "callback must be a function if provided",
  "trace": [
    "TypeError: callback must be a function if provided",
    "    at Object.load (/var/task/node_modules/npm/lib/npm.js:163:13)",
    "    at Runtime.module.exports.runtest [as handler] (/var/task/handler.js:10:7)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

Can anyone provide assistance with this issue?

Answer №1

When utilizing the npm.load function, it is important to note that it only accepts a single argument, which should be a callback function. In this case, there seems to be an issue where you are passing in {} as the first argument, resulting in a TypeError indicating that the provided value is not a function.

The reason why the code works fine on another page could be attributed to potential differences in npm versions, especially considering the age of the post you are referencing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Express JS server has encountered a user access issue and is denying permission for localhost to disconnect

Hey there, I'm currently working on resolving a disconnect issue with my Express.js server. When using the provided code to handle disconnect, I am encountering an error stating "access denied for user in localhost." var connection; function handleD ...

404 error: Docker container hosting static ReactJS files unable to locate page

I have been attempting to containerize a Go application that serves static files on port 8000. Despite trying suggestions from other sources, such as using router.Run("0.0.0.0:8000") or router.Run(":8000"), I have not been successful. H ...

Best practices for executing an asynchronous forEachOf function within a waterfall function

I've been working with the async library in express js and I'm encountering an issue when using two of the methods alongside callbacks. The variable result3 prints perfectly at the end of the waterfall within its scope. However, when attempting t ...

I'm encountering a npm error on Windows_NT 10.0.19042, does anyone know how to troubleshoot this issue?

After downgrading to [email protected], I encountered an error message that keeps popping up whenever I try to update npm or install new packages. What steps can I take to resolve this issue? npm ERR! Windows_NT 10.0.19042 npm ERR! argv "C:\ ...

What is the process for defining primary keys when using DynamoDB.DocumentClient in NodeJS?

I have been delving into a plethora of documentation, specifically focusing on AWS.DynamoDB.DocumentClient, and I am grappling with how to create a Table / DocumentClient with a designated primary key, such as id. From what I gather, primary keys are manda ...

Tips on avoiding asynchronous issues in NodeJS: Ensure task a is finished before initiating task b

I have a situation where I need to ensure that task B code only executes after task A has completed. Task A involves converting an audio file, while Task B relies on the converted audio for further processing. The issue arises because Task A saves the n ...

How can I effectively troubleshoot an npm script using the features of vscode?

Currently, I am facing an issue with debugging an npm script using vscode. To troubleshoot this, I decided to set up a debug configuration and utilize the debugger feature in vscode. The npm script in question is: "scripts": { ... "dev": "node ta ...

Unable to deploy a sails.js application on an Amazon EC2 or DigitalOcean Ubuntu server using forever or pm2

After installing sails.js (v0.9.16) globally on amazon-ec2 / digitalocean ubuntu, I created a test project and successfully lifted the server: sudo npm -g install sails sails new sailstest cd sailstest sails lift I was able to access the sails app home ...

Developing a compressed file in JavaScript

async purchaseMultiple(decoded, purchaseData){ const user = await Database.user.findOne({where: { id_user: decoded.id_user }}); if( ! user) return [404, 'ERROR: User [' + decoded.id_user + '] not found']; if(user.credi ...

"Utilize Node.js to generate a nested array structure with groupings

Here is an array data that I have: [ { tempId: 1, nik: '11002', employeeName: 'Selly Amaliatama', basic_salary: 3500000, id_component: 'AD0128114156', componentName: 'Tunjangan Maka ...

"Need assistance with npm ERR! Can anyone provide some help,

Recently, I created a notepad file in my directory called "package.json". Inside this file, I typed the following: { “name”: “Bot”, “version”: “1.0.0”, “description”: “My First Discord bot”, “main”: “bot.js”, “author”: ...

Does the HTTP Request client process requests synchronously?

Seeking assistance with http post and get requests in Node.js. I am new to this technology. Can someone clarify if the process is synchronous or asynchronous? Visit this link for more information ...

Best practices for using spaces in Node.js within WebStorm conventions

After researching, I discovered that the convention in Node.js is to use double spaces for indentation. However, by default, it uses 4 spaces. How can this be configured? Here's an example: if ('development' == app.get('env')) { ...

Group data by two fields with distinct values in MongoDB

I have developed a Typescript Node.js application and I am looking to organize documents by two fields, "one_id" and "two_id", based on a specific "one_id" value. Below is the data within my collection: { "_id":"5a8b2953007a1922f00124fd", "one_id ...

Tips for integrating role-based authorization in Node.js using token-based authentication?

What is the best way to incorporate multiple authentications in a nodejs system for an educational institution, with three different roles - student, parent, and admin - using a REST API strategy such as token-based authentication? ...

Sequelize is providing a date that is earlier than the one initially received

Hello everyone, I'm facing an issue with Sequelize where it seems to be returning the date from the backend as the day before the actual date passed. Additionally, it is adding a default time of 23:00:00. To elaborate, when a date is selected in the f ...

Tracking Command Cooldowns in Discord.js: How to Calculate Time Remaining

Is it possible to modify the cooldown feature so that it shows the remaining time until the user can work again, rather than displaying a fixed wait time message? const { RichEmbed } = require("discord.js"); const { stripIndents } = require("common-tag ...

Is there a way to resolve the CORS issue using the CORS module?

I recently added the cors module to my project and implemented it like so: var express = require('express'), favicon = require('serve-favicon'), cors = require('cors'); var app = express(); if (app.get('env&apos ...

File system explorer tool for servers

In the process of developing a node.js web interface for an internal project at my current company, I am looking to create a functionality where users can select files from the server memory disk for processing. I envision something akin to a file browser ...

The error message "window is not defined in Angular Universal" indicates that the window object

While attempting to utilize @nguniversal/express-engine, I encountered an issue in the main.js file after installing and running it. The error message reads: C:\Folder\ssr\dist\ssr\server\main.js:179450 })(window, functio ...