This guide describes how to integrate third-party libraries (e.g., OpenAI) with the Weave TypeScript SDK. Weave supports automatic instrumentation, making setup easier and reducing the need for manual configuration.Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-john-wbdocs-2044-rename-serverless-products.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Use Weave integrations with your TypeScript project
TypeScript projects can use either the CommonJS or ESM module system.If you’re unsure which type of project you have
If you run a TypeScript file directly with a tool such as:package.json and tsconfig.json files.
To determine whether a project uses CommonJS or ESM, check the type field in package.json:
- If
typeis"module", the project uses ESM. - If the
typefield 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.jsonwith CommonJS-compatible settings:These settings configure TypeScript to compile for CommonJS:-
module: "CommonJS"— Compiles modules to CommonJS format (require/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 containstsconfig.jsonas the root of your input files. TypeScript uses this withoutDirto 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 thedistfolder. 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 todist/test.js. -
Run the compiled file with Node.js:
require module loader, Weave can automatically instrument supported libraries without requiring 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.jsonwith Node-compatible ESM settings: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 containstsconfig.jsonas the root of your input files. TypeScript uses this withoutDirto 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 thedistfolder.
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 todist/test.js. -
Run the compiled file with Node.js and preload the Weave instrumentation:
--import flag ensures 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.
Advanced usage and troubleshooting
This section covers edge cases and workarounds for when the TypeScript SDK’s automatic patching doesn’t work as expected. For example, ESM-only environments, bundler setups like Next.js, or constrained runtime environments may cause unexpected issues. If you’re seeing missing traces or integration issues, start here.Use NODE_OPTIONS (only for ESM)
If you’re using an ESM project and cannot pass CLI flags (e.g., due to constraints in CLI tools or frameworks), set the NODE_OPTIONS environment variable:
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 them from being bundled, so Weave can patch them correctly at runtime.
The following example shows how to mark the
openaipackage as external in anext.config.jsconfiguration, which prevents it from being bundled. The module is loaded at runtime, so Weave can automatically patch and track it. Use this setup when working with frameworks like Next.js to enable auto-instrumentation. - If patching still fails, fall back to manual instrumentation.