Why does datadog lambda instrumentation return a `datadog-lambda-js/handler.handler is undefined or not exported`? - node.js

The Datadog AWS Lambda instrumentation seems unreliable to me. Every few invocations, I get the following error:
"errorType": "Runtime.HandlerNotFound",
"errorMessage": "/opt/nodejs/node_modules/datadog-lambda-js/handler.handler is undefined or not exported",
"stack": [
"Runtime.HandlerNotFound: /opt/nodejs/node_modules/datadog-lambda-js/handler.handler is undefined or not exported",
" at HandlerNotFound.ExtendedError [as constructor] (/opt/nodejs/node_modules/datadog-lambda-js/runtime/errors.js:113:28)",
" at new HandlerNotFound (/opt/nodejs/node_modules/datadog-lambda-js/runtime/errors.js:131:42)",
" at load (/opt/nodejs/node_modules/datadog-lambda-js/runtime/user-function.js:151:15)",
" at Object.<anonymous> (/opt/nodejs/node_modules/datadog-lambda-js/handler.js:65:59)",
" at Module._compile (internal/modules/cjs/loader.js:999:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
" at Module.require (internal/modules/cjs/loader.js:887:19)",
" at require (internal/modules/cjs/helpers.js:74:18)"
]
}
To start the instrumentation, I run the following code.
datadog-ci lambda instrument \
-f my-lambda-name \
-r my-region \
-v 50 -e 15 \
--service my-service \
--env my-env \
--version 1.0
Any idea what I'm doing wrong?

Depending on the runtime environment, you should point to a different version of the datadog layer extensions.
Python, for example, uses -v 50 -e 15, but Node.js handlers should use a different version, -v 66 -e 16.
All of this is available in the documentation: https://docs.datadoghq.com/serverless/installation/nodejs/?tab=datadogcli

Related

AWS Lambda function error: Cannot find module 'lambda'

I am trying to deploy a REST API in AWS using serverless. Node version 14.17.5.
My directory structure:
When I deploy the above successfully I get the following error while trying to access the api.
2021-09-28T18:32:27.576Z undefined ERROR Uncaught Exception {
"errorType": "Error",
"errorMessage": "Must use import to load ES Module: /var/task/lambda.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/lambda.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.\nInstead rename lambda.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.\n",
"code": "ERR_REQUIRE_ESM",
"stack": [
"Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/lambda.js",
"require() of ES modules is not supported.",
"require() of /var/task/lambda.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.",
"Instead rename lambda.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.",
"",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)",
" at Module.load (internal/modules/cjs/loader.js:937:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
" at Module.require (internal/modules/cjs/loader.js:961:19)",
" at require (internal/modules/cjs/helpers.js:92:18)",
" at _tryRequire (/var/runtime/UserFunction.js:75:12)",
" at _loadUserApp (/var/runtime/UserFunction.js:95:12)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1072:14)"
]
}
As per the suggestion in the error I tried changing the lambda.js to lambda.cjs. Now I get the following error
2021-09-28T17:32:36.970Z undefined ERROR Uncaught Exception {
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'lambda'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'lambda'",
"Require stack:",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1072:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)",
" at Module.load (internal/modules/cjs/loader.js:937:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
" at internal/main/run_main_module.js:17:47"
]
}
serverless.yml
service: APINAME #Name of your App
useDotenv: true
configValidationMode: error
provider:
name: aws
runtime: nodejs14.x # Node JS version
memorySize: 512
timeout: 15
stage: dev
region: us-east-1 # AWS region
lambdaHashingVersion: 20201221
functions:
api:
handler: lambda.handler
events:
- http: ANY /{proxy+}
- http: ANY /
lambda.js
import awsServerlessExpress from 'aws-serverless-express'
import app from './index.js'
const server = awsServerlessExpress.createServer(app)
export const handler = (event, context) => {
awsServerlessExpress.proxy(server, event, context)
}
aws-cli commands
docker run --rm -it amazon/aws-cli --version
docker run --rm -it amazon/aws-cli configure
docker run --rm -it amazon/aws-cli serverless deploy
serverless commands:
docker run --rm -it amazon/aws-cli serverless deploy
serverless config credentials --provider aws --key <KEY> --secret <SECRET>
node ./node_modules/serverless/bin/serverless config credentials --provider aws --key <KEY> --secret <SECRET>
After reading up a couple of answers I have tried the following:
Made sure package.json includes "type": "module"
Deleted node_modules and package-lock.json and reinstalled all of them (since the version of node was updated during development)
What am I doing wrong?
Converted all imports to require() and all exports to module.exports
Removed "type": "module" from package.json
Everything works like a charm. It is not a solution to the question asked but making things work became more important.
Instead rename lambda.js to end in .cjs, change the requiring code to use
import(),
and remove
"type": "module"
from /var/task/package.json. It is there in the error.

Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task IN AWS Lambda

I am not getting any error in local with node-fetch in the package.json also have node-fetch dependencies but when I deployed in aws lambda then this dependencies not found.
import fetch from 'node-fetch';
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task/query1.js\n- /var/task/query1Main.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'node-fetch'",
"Require stack:",
"- /var/task/query1.js",
"- /var/task/query1Main.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:999:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
" at internal/main/run_main_module.js:17:47"
]
}
You have to declare external packages as layers for a lambda
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
I had the same issue using Node version 18.x and fixed after downgrading to 16.x
This can be done in the lambda page -> scroll down to Runtime settings
I hope you already resolved the issue but I'm posting this in case someone else finds it useful.

Why Cannot find module 'request-promise' ? ( Runtime.ImportModuleError)

I create a simple NodeJS project and want to upload it as an aws lambda, but I get this error when I hit Test:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'request-promise'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'request-promise'",
"Require stack:",
"- /var/task/index.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" 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:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
" at internal/main/run_main_module.js:17:47"
]
}
I import every think, but the lambda can't find modules that already exist in node_modules.
the structure of my project is like this:
project_name>
node_modules
index.js
package.json
package-lock.json
index.js
let request = require("request-promise");
exports.handler = async () => {
// my functions
...
}
Do you have any idea how to solve this issue please!
I solved the issue by installing request
npm install request --save
then I deployed the project and it seems every thing is ok.

installing and running browsershots

I tried to install Brwoswershots (https://github.com/spatie/browsershot) and I did this exactly by following the instructions on github.
Installing NodeJS and npm via yum
Installing browswershoots via composer: composer require
satie/browsershot
crated a file with
use Spatie\Browsershot\Browsershot;
$pathToImage="img/testasdf.png";
// an image will be saved
Browsershot::url('https://example.com')
->setScreenshotType('jpeg', 100)
->save($pathToImage);
It is on http://textblob.com/browsershots.php and brings the error.
Fatal error: Uncaught
Symfony\Component\Process\Exception\ProcessFailedException:
The command "PATH=$PATH:/usr/local/bin NODE_PATH=`npm root -g` node
'/home/textblob/public_html/vendor/spatie/browsershot/src/../bin/browser.js'
'{"url":"https:\/\/example.com",
"action":"screenshot",
"options": {
"type":"jpeg",
"path":"img\/testasdf.png",
"quality":100,
"args":[],
"viewport":{"width":800,"height":600}
}
}'"
failed. Exit Code: 1(General error) Working directory:
/home/textblob/public_html
Output: ================ Error Output: ================
/home/textblob/public_html/vendor/spatie/browsershot/bin/browser.js:5
const getOutput = async (page, request) =>
{ ^ SyntaxError: Unexpected token ( at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:549:28)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.runMa in /home/textblob/public_html/vendor/spatie/browsershot/src/Browsershot.php
on line 565
I could solve this by downgrading to boxshot v2, witch works without puppeteer and so its not a fix, but working for me.

Configure Mocha to continually run tests fails

I'm trying to configure mocha to run the tests instead of running manually using the command:
node_modules\.bin\mocha -w
I get the error as below.
D:\Downloads\Bluemix\dw0015a\dw0015a\node_modules\mocha\lib\utils.js:626
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) '-w'
at Object.lookupFiles (D:\Downloads\Bluemix\dw0015a\dw0015a\node_modules\moc
at D:\Downloads\Bluemix\dw0015a\dw0015a\node_modules\mocha\bin\_mocha:316:30
at Array.forEach (native)
at Object.<anonymous> (D:\Downloads\Bluemix\dw0015a\dw0015a\node_modules\moc
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
I am able to get a behavior similar to what you report is to have the w option preceded by an "en dash" character (Unicode U+2013) rather than a minus character. Like this:
$ mocha –w
/home/x/local/lib/node_modules/mocha/lib/utils.js:626
throw new Error("cannot resolve path (or pattern) '" + path + "'");
^
Error: cannot resolve path (or pattern) '–w'
at Object.lookupFiles (/home/x/local/lib/node_modules/mocha/lib/utils.js:626:15)
[... etc ...]
Make sure you are using the minus character (hex value 0x2d).

Resources