libsql-client-ts: "Runtime.ImportModuleError: Error: Cannot find module '@libsql/linux-x64-gnu" when bundling with esbuild for aws lambda
I was switching my database from planetscale to turso but I’m getting the following error
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module '@libsql/linux-x64-gnu'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module '@libsql/linux-x64-gnu'",
"Require stack:",
"- /var/task/index.js",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:1061:17)",
" at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)",
" at async start (file:///var/runtime/index.mjs:1256:23)",
" at async file:///var/runtime/index.mjs:1262:1"
]
}
I use the aws cdk to build and deploy my aws lambda function internally it uses esbuild here is a simple reproduction step
the aws lambda function code /nodejs/index.ts
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
const client = createClient({ url: 'DATABASE_URL', authToken: 'DATABASE_AUTH_TOKEN' });
const db = drizzle(client);
export const handler = async (event) => {
console.log('event', event);
}
the aws cdk code to deploy the function:
import {Construct} from 'constructs';
import {NodejsFunction} from 'aws-cdk-lib/aws-lambda-nodejs';
import {Architecture, Runtime} from 'aws-cdk-lib/aws-lambda';
export class SendUserToDynamo extends Construct {
function: NodejsFunction;
constructor(scope: Construct, id: string) {
super(scope, id);
// lambda function that triggers on aws lambda user created event
this.function = new NodejsFunction(this, 'sendUserToDynamo', {
entry: __dirname + '/nodejs/index.ts',
handler: 'handler',
architecture: Architecture.X86_64,
runtime: Runtime.NODEJS_18_X,
});
}
}
Everything works when I’m using the PlanetScale driver, so the error must come from libsql and not the CDK build step. I tried using a Dockerfile without TypeScript and ESBuild, and it did work
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 27
- Comments: 23
Commits related to this issue
- fix: dynamic libsql import with fallback (#26526) Implements a workaround for an issue [when Prisma is bundled for AWS Lambda](https://github.com/tursodatabase/libsql-client-ts/issues/112). — committed to prisma/prisma by jacek-prisma 2 years ago
- fix: Fix bundling of storage package. (#557) ## Proposed changes Bundling is not compatible with @libsql/client, so work around it in the bundler by installing the package and specifying the correct... — committed to gensx-inc/gensx by jmoseley 2 years ago
I ended up using the web/HTTP client to be able to run the latest version:
Confirming I’m seeing this too, also switching from Planetscale to Turso. Interestingly the error doesn’t happen in local env, only when deployed to Netlify. Running node 16. Pulling
@libsql/clientdown to0.3.2from0.5.3fixed things for me.fyi this is still an active issue.
@libsql/client/webis also not a feasible workaround as it doesn’t support embedded replicas.Just ran into this. The
@libsql/client/webworkaround also doesn’t seem to work. I’m deploying to Lambda using Serverless Framework. Does anyone have a workaround?this works
I was able to solve this by adding the
@libsql/clientas a nodejs dependency which allows SST to do an NPM install of them in the function bundle:0.3.3 also works
The
import { createClient } from '@libsql/client/web'workaround only works when there’s no transitive dependencies onlibsql. It’s possible to get transitive dependencies to work as well by adding an esbuild alias:--alias:'@libsql/client=@libsql/client/web'in the esbuild command (and importing it asimport { createClient } from '@libsql/client').This worked for me too! Thanks!
If you want to use the native client that uses the
libsqlbinary under the hood in Lambda, you’ll have to build the function in a similar arch/environment or use a Lambda layer, also built under the same conditions (i.e. Docker).A bundler like esbuild won’t be able to include the correct version (i.e.
@libsql-linux-arm64-gnu) unless it’s itself running on that same hardware.This worked for me:
then in my function I use
@libsql/clientas normal:which when invoked prints this:
The advantage of using a Lambda layer like that is that you can still continue using
esbuildfor the function(s) - the only caveat there is to make sure to specifyexternalModules: ['@libsql/client'],so that the dependency is not included in the bundle - this is because it comes from the Layer.it does say in the docs that you should use it for vercel functions and the like. the docs could be a bit clearer here. nothing mentions what to use for lambda
Ran into this too and got it working with 0.3.3.