This guide describes how to integrate third-party libraries (for example, OpenAI) with the Weave TypeScript SDK. It’s intended for TypeScript developers who want Weave to automatically trace calls to supported libraries in their application.
Weave supports automatic instrumentation, which streamlines setup and reduces the need for manual configuration.
What changed?
As of PR #4554, Weave automatically patches supported libraries such as OpenAI when it loads. You no longer need to manually wrap them:weave.wrapOpenAI(new OpenAI());
Weave handles this automatically in most cases. However, edge cases may occur.
Use Weave integrations with your TypeScript project
The following sections describe how to identify which module system your project uses and then configure that project so Weave can automatically instrument supported third-party libraries. TypeScript projects can use either the CommonJS or ESM module system, and the required setup differs slightly between the two.
If you’re unsure which type of project you have
If you run a TypeScript file directly with a tool such as:
your environment may determine the module system implicitly. For consistent behavior, explicitly define package.json and tsconfig.json files.
To determine whether a project uses CommonJS or ESM, check the type field in package.json:
- If
type is "module", the project uses ESM.
- If the
type field is missing or set to "commonjs", the project defaults to using CommonJS.
Set up a CommonJS project
For CommonJS projects, automatic instrumentation works without additional configuration.
To configure your project for CommonJS:
-
Create or update your
package.json:
-
Create a
tsconfig.json with CommonJS-compatible settings:
{
"compilerOptions": {
"module": "CommonJS",
"target": "es2022",
"rootDir": ".",
"outDir": "dist"
}
}
These settings configure TypeScript to compile for CommonJS:
-
module: "CommonJS". Compiles modules to CommonJS format (require or module.exports).
For details on this compiler option, see TypeScript - Module.
-
target: "es2022" (recommended). Emits modern JavaScript compatible with recent Node.js versions.
For details on this compiler option, see TypeScript - Target.
-
rootDir: ".". Treats the directory that contains tsconfig.json as the root of your input files. TypeScript uses this with outDir to mirror your source folder layout in the output.
For details on this compiler option, see TypeScript - Root Dir.
-
outDir: "dist". Writes emitted JavaScript and other compiler outputs into the dist folder.
For details on this compiler option, see TypeScript - Out Dir.
-
Install Weave and any other required libraries:
-
Compile your TypeScript file.
For an example file
test.ts:
This compiles the file to dist/test.js.
-
Run the compiled file with Node.js:
Your CommonJS project is now configured so that Weave automatically instruments supported libraries when your code runs. Because CommonJS uses the Node.js require module loader, Weave can automatically instrument supported libraries without the --import flag used in ESM projects.
Set up an ESM project
To use Weave with an ESM TypeScript project, configure your project for Node.js ESM, compile your code, and start Node.js with the --import flag so Weave can register its instrumentation before other modules load.
To configure your project for ESM:
-
Create or update your
package.json:
-
Create a
tsconfig.json with Node-compatible ESM settings:
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"target": "es2022",
"rootDir": ".",
"outDir": "dist"
}
}
These settings configure TypeScript to compile for modern Node.js ESM:
-
module: "nodenext". Compiles modules using Node.js ESM semantics.
For details on this compiler option, see TypeScript - Module.
-
moduleResolution: "nodenext". Ensures module resolution follows Node.js ESM rules.
For details on this compiler option, see TypeScript - Module Resolution.
-
target: "es2022" (recommended). Emits modern JavaScript compatible with recent Node.js versions.
For details on this compiler option, see TypeScript - Target.
-
rootDir: ".". Treats the directory that contains tsconfig.json as the root of your input files. TypeScript uses this with outDir to mirror your source folder layout in the output.
For details on this compiler option, see TypeScript - Root Dir.
-
outDir: "dist". Writes emitted JavaScript and other compiler outputs into the dist folder.
For details on this compiler option, see TypeScript - Out Dir.
-
Install Weave and any other required libraries:
-
Compile your TypeScript file.
For an example file
test.ts:
This compiles the file to dist/test.js.
-
Run the compiled file with Node.js and preload the Weave instrumentation:
node --import=weave/instrument dist/test.js
The --import flag ensures that the weave/instrument module loads before other modules, so Weave can automatically instrument supported libraries and integrations.
Weave must be installed locally in the project you run.
Your ESM project is now configured so that Weave preloads its instrumentation before other modules and automatically traces calls to supported libraries.
Advanced usage and troubleshooting
The following sections cover edge cases and workarounds for when the TypeScript SDK’s automatic patching doesn’t work as expected. For example, ESM-only environments, bundler setups such as Next.js, or constrained runtime environments may cause issues. If you see missing traces or integration issues, start here.
Use NODE_OPTIONS (only for ESM)
Use NODE_OPTIONS with caution, because it affects all Node.js processes in the environment and may introduce side effects.
If you use an ESM project and can’t pass CLI flags (for example, due to constraints in CLI tools or frameworks), set the NODE_OPTIONS environment variable:
export NODE_OPTIONS="--import=weave/instrument"
Bundler compatibility
Some frameworks and bundlers, such as Next.js, may bundle third-party libraries in ways that prevent Node.js from patching them at runtime.
If this describes your setup, try the following steps:
-
Mark LLM libraries as external in your bundler configuration. This prevents the bundler from bundling them, so Weave can patch them correctly at runtime.
The following example shows how to mark the
openai package as external in a next.config.js configuration, which prevents the bundler from bundling it. The module loads at runtime, so Weave can automatically patch and track it. Use this setup when you work with frameworks such as Next.js to enable auto-instrumentation.
externals: {
'openai': 'commonjs openai'
}
-
If patching still fails, fall back to manual instrumentation.
Manual patching (fallback option)
Manual patching is the legacy approach. Use it only when auto-patching doesn’t work.
Sometimes, you may still need to use manual instrumentation:
import { wrapOpenAI } from 'weave';
const client = wrapOpenAI(new OpenAI());