Skip to content

Instantly share code, notes, and snippets.

@moyarich
Last active April 18, 2024 00:47
Show Gist options
  • Save moyarich/5de73833628f00c68a25f2e686d11eb2 to your computer and use it in GitHub Desktop.
Save moyarich/5de73833628f00c68a25f2e686d11eb2 to your computer and use it in GitHub Desktop.
Which NPM modules are preinstalled in AWS Lambda
/*
@see https://alestic.com/2014/11/aws-lambda-environment/
*/
import * as cp from "child_process";
import util from "util";
const exec = util.promisify(cp.exec);
export const handler = async (event) => {
try {
// Extract the command from the event
const command = event.command;
// Execute the command and capture the output
//const results = cp.execSync(command).toString();
const results = await exec(command);
const stdout = results.stdout.split("\n");
const stderror = results.stderr.split("\n");
console.log("stdout", stdout);
console.log("stderror", stderror);
// Return success response with command output
return {
statusCode: 200,
body: results,
};
} catch (error) {
// Return error response
return {
statusCode: 500,
body: error.message,
};
}
};
@moyarich
Copy link
Author

moyarich commented Apr 17, 2024

Purpose:

This command is designed to inspect globally installed npm packages and the node paths associated with them in an AWS lambda function

Description:

Check Global NPM Packages:
npm ls -g: Lists all globally installed npm packages.

Display Node Paths:
echo "Node paths: ${NODE_PATH}": Prints the node paths associated with the globally installed npm packages.

Iterate through Node Paths:
dirs=$(echo $NODE_PATH | tr ':' '\\n' | sort): Extracts each directory from the node paths and sorts them for systematic inspection.

Inspect Each Directory:
For each directory obtained from the node paths:

for dir in $dirs; do ... done: Loop iterates through each directory.

echo "---Directory: ${dir} ---\n": Prints the directory being inspected.

(ls -1 $dir 2>&1 || true): Lists the contents of the directory (if it exists). Any errors are also captured and displayed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment