ESM Support for NodeJS sdk

I followed Get Started to use javascript sdk but i’m getting error during import of library.
I’m using 0.8.31 rockset/client and my function lambda is built with ESM format.
This is the error I received:

Error: (0 , import_client.default) is not a function

import rockset from "@rockset/client";
import { Config } from 'sst/node/config';

const rockset_client = rockset(Config.ROCKSET_APIKEY, 'https://api.use1a1.rockset.com');

Is rockset compatible with ESM or is there some other issue?

The issue here is with import { Config } from 'sst/node/config'; not with the usage of the client. Easiest way to test this is to remove the import and just insert any other string as the key and it should at least run. If that doesn’t work, then it’s something with the environment since that syntax is correct. Can you test it and post here?

Same issue if I remove Config. I’m using node18 format esm

How are you building your lambda function? Are you using a zip or docker image?

We were able to get the below working using ESM and deployed using a zip

import rockset from "@rockset/client";

const handler = (event, _, callback) => {
  const rs = rockset.default("asdf");
  console.log(rs);
};
export {handler};

You may find these docs useful: [Build a Lambda deployment package for Node.js]

Also, I noticed in your screenshot you are missing ‘const rs = rockset.default(“asdf”);’ . Adding this will not fix the issue, but be sure to include it since missing it may cause problems down the line.

I’m using SST , a superset of AWS CDK (you can mix CDK constructor and SST constructor).
When I make a lambda I use the constructor Function Function | SST.
I just specify an handler (a simple javascript function) and it is built with esbuild and deployed.

In this moment I’m making calls with axios to works with Rockset until understand how works with sdk.

Node version is 18 and code is bundled with ESBuild

Thank you for your detailed response! We were able to bundle a package with the client as a dependency using esbuild without any issues. Can you verify that your code works when just using esbuild without sst. If it does, then we know it’s a problem with SST + the client.

esbuild blah.ts --bundle --platform=node --target=node18 --outfile=out.js
node out.js

blah.ts is a very simple script that just imports the client and logs the client object

Sorry for late answer.
I’ve isolated rockset and it works properly

When I run
npx esbuild index.ts --bundle --platform=node --target=node18 --outfile=out.js
node out.js

When I run with sst the function in bundled but when it is triggered I get URL error

Error: URL is not a constructor

This is a part of file list.mjs:

import { createRequire as topLevelCreateRequire } from 'module';
const require = topLevelCreateRequire(import.meta.url);
import { fileURLToPath as topLevelFileUrlToPath } from "url"
const __dirname = topLevelFileUrlToPath(new URL(".", import.meta.url))

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined")
    return require.apply(this, arguments);
  throw new Error('Dynamic require of "' + x + '" is not supported');
});
var __esm = (fn, res) => function __init() {
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};

Esbuild (inside sst) generates mjs, so I tried adding format esm:

npx esbuild index.ts --bundle --platform=node --format=esm --target=node18 --outfile=out.mjs

When execute out.mjs I get:

file:///Users/gmarino/Workspace/rockset-client-nodejs-example/out.mjs:12
  throw new Error('Dynamic require of "' + x + '" is not supported');
        ^

Error: Dynamic require of "url" is not supported
    at file:///Users/gmarino/Workspace/rockset-client-nodejs-example/out.mjs:12:9
    at node_modules/.pnpm/@rockset+client@0.8.31/node_modules/@rockset/client/dist/codegen/api.js (file:///Users/gmarino/Workspace/rockset-client-nodejs-example/out.mjs:3111:15)
    at __require2 (file:///Users/gmarino/Workspace/rockset-client-nodejs-example/out.mjs:15:50)
    at node_modules/.pnpm/@rockset+client@0.8.31/node_modules/@rockset/client/dist/index.js (file:///Users/gmarino/Workspace/rockset-client-nodejs-example/out.mjs:9648:15)
    at __require2 (file:///Users/gmarino/Workspace/rockset-client-nodejs-example/out.mjs:15:50)
    at file:///Users/gmarino/Workspace/rockset-client-nodejs-example/out.mjs:9753:29
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:526:24)
    at async loadESM (node:internal/process/esm_loader:91:5)

Hi again!

For the esbuild part of your posts, this is not an issue that is specific to the rockset client:
https://github.com/evanw/esbuild/issues/1966
https://github.com/evanw/esbuild/issues/1921
https://github.com/evanw/esbuild/pull/2067

We were able to get the same error by removing the rockset dependency and trying to use uniqid instead like one person recommended in the issue comments above. That error can be stopped by adding the following code block to the top of the generated .mjs file

const { require, __filename, __dirname } = await (async () => {
	const { createRequire } = await import("node:module");
	const { fileURLToPath } = await import("node:url");

	return {
		require: createRequire(import.meta.url),
		__filename: fileURLToPath(import.meta.url),
		__dirname: fileURLToPath(new URL(".", import.meta.url)),
	};
})();

For sst if you can somehow insert that code block into the built package then maybe that will resolve the issue. Try using https://docs.sst.dev/constructs/Function#functionhooks but were not sure what those are capable of doing. This is assuming that the sst issue is caused by the same thing. In the meantime, we will add sst compatibility as a known issue.

Thank you, please let me know if I can help you in anyway

Thank you for offering! We have created a github issue for tracking: https://github.com/rockset/rockset-js/issues/66