I am trying to run Nightmare JS on AWS Lambda, but my function always returns null and does not seem to be running any of my async code. Here is my code:
exports.handler = (event, context, callback) => {
console.log('starting....')
const Nightmare = require('nightmare')
const nightmare = Nightmare()
console.log('created Nightmare: ', nightmare)
return nightmare
.goto('https://www.myurl.com')
.exists('[data-selector-element]')
.then((exists) => {
console.log('element exists: ', exists)
if (exists) {
return nightmare.click('[data-selector-element]')
.wait(200)
.evaluate(() => {
const title = document.querySelector('h1.header')
return { title }
})
.then((res) => {
context.success(res)
console.log('success:', res)
callback('success: ')
return res
})
} else {
return 'not present'
}
})
}
The function always returns null, and although this process should take at least a couple of seconds, the function usually ends in around 100ms. The first two console logs (above return nightmare.goto...
) are registered by Lambda, but later logs are not.
Is there something I'm doing wrong?
TL;DR - you cannot run Nightmare on Lambda, you can only run it on a regular server where you can install extra dependencies (e.g. web drivers).
This was not working because Nightmare requires various web drivers in order to run, which are not present on Lambda, and as far as I know cannot be installed on Lambda.
There's a long thread on the Nightmare repo here discussing how you CAN run Nightmare headlessly on Linux, but this requires you to install various dependencies, which as far as I'm aware is not possible on Lambda.
Please do leave an answer if you find a way in the future!