Lerna to Lambda Bundler
lerna-to-lambda (l2l) is a specialized utility designed for developers working with Lerna monorepos who need to deploy individual packages as AWS Lambda functions. At version 0.3.1, this tool facilitates the creation of compact, standalone deployment archives by intelligently bundling a specific monorepo package with only its necessary dependencies. It resolves the common challenge of dealing with hoisted `node_modules` in Lerna projects by copying direct and recursive subdependencies into a single `node_modules` structure within the output directory. A key differentiator is its ability to correctly handle both external npm packages and internal, inter-dependent monorepo packages, ensuring a self-contained runtime environment for Lambda. By default, it excludes `aws-sdk`, assuming it's provided by the Lambda runtime. While a powerful bundling solution, it is not a package manager itself; users must ensure all dependencies are installed via `yarn` or `npm` prior to execution. Its release cadence is likely feature-driven given its current version number.
Common errors
-
Error: Cannot find module 'my-internal-monorepo-package'
cause The internal Lerna package dependency was not correctly identified or copied into the Lambda bundle's `node_modules`.fixEnsure 'my-internal-monorepo-package' is listed in the `package.json` dependencies of the target Lambda package. Verify that Lerna has correctly linked it before running `l2l`. -
Runtime.ImportModuleError: Error: Cannot find module 'some-external-npm-dependency'
cause An external npm package required by your Lambda function was not installed or bundled correctly before deployment.fixRun `yarn install` or `npm install` in your monorepo root to ensure all external dependencies are installed. Confirm `l2l` is running *after* this installation step in your build process. -
EACCES: permission denied, mkdir '/path/to/output'
cause The `l2l` command does not have sufficient permissions to create the output directory or write files within it.fixEnsure the user executing `l2l` has write permissions to the specified output directory. You might need to adjust directory permissions or run the command with elevated privileges (e.g., `sudo`, or correct user setup in CI/CD).
Warnings
- gotcha lerna-to-lambda is not a package manager; it does not install or verify package versions. Users must ensure all dependencies (both internal monorepo and external npm) are correctly installed via `yarn` or `npm` *before* running `l2l`.
- breaking The dependency topology within the bundled `node_modules` can shift. Lerna-hoisted dependencies may be 'unhoisted' and moved into sub-`node_modules` directories within the package bundle. This rearrangement can potentially lead to unexpected behavior or break certain packages that rely on specific `node_modules` structures.
- gotcha By default, `l2l` omits the `aws-sdk` dependency and its subdependencies from the bundle. This assumes `aws-sdk` is provided by the target AWS Lambda runtime environment. If your Lambda environment does not provide `aws-sdk` or requires a specific version not implicitly available, your function will fail.
Install
-
npm install lerna-to-lambda -
yarn add lerna-to-lambda -
pnpm add lerna-to-lambda
Imports
- l2l (CLI command)
import { l2l } from 'lerna-to-lambda'npx l2l -i build -o lambda
- Options (type)
import { Options } from 'lerna-to-lambda';import type { Options } from 'lerna-to-lambda'; - (No default export)
import l2l from 'lerna-to-lambda'
// No default export from 'lerna-to-lambda'
Quickstart
yarn add -W --dev lerna-to-lambda
// In package.json scripts for a Lerna package:
"scripts": {
"clean": "rimraf build lambda",
"compile": "tsc -p tsconfig.build.json",
"package": "l2l -i build -o lambda",
"build": "yarn run clean && yarn run compile && yarn run package"
}