Skip to content

Commit

Permalink
Merge pull request #626 from hwchase17/nc/cjs-build
Browse files Browse the repository at this point in the history
Add CJS build, add package testing CJS exports
  • Loading branch information
nfcampos committed Apr 6, 2023
2 parents a5eeaff + 010fd18 commit d221617
Show file tree
Hide file tree
Showing 52 changed files with 734 additions and 209 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,5 @@ jobs:
run: yarn install --immutable
- name: Build
run: yarn run build --filter="!docs"
- name: Test exports
run: yarn workspace test-exports run test
- name: Test langchain
run: yarn workspace langchain run test
- name: Test
run: yarn run test:unit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
dist/
dist-cjs/
lib/
.turbo
.eslintcache
Expand Down
12 changes: 12 additions & 0 deletions .watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ignore_dirs": [
"langchain/dist",
"langchain/dist-cjs",
"docs/build",
"node_modules",
"langchain/.turbo",
"docs/.turbo",
"test-exports/.turbo",
"test-exports-cjs/.turbo"
]
}
37 changes: 14 additions & 23 deletions docs/docs/getting-started/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,38 @@ We currently support LangChain on Node.js 18 and 19. Go [here](https://github.co

### TypeScript

If you are using TypeScript we suggest updating your `tsconfig.json` to include the following:

```json
{
"compilerOptions": {
...
"target": "ES2020", // or higher
"module": "nodenext",
}
}
```
LangChain is written in TypeScript and provides type definitions for all of its public APIs.

## Loading the library

### ESM in Node.js

LangChain is an ESM library currently targeting Node.js environments. To use it, you will need to use the `import` syntax, inside a project with `type: module` in its `package.json`.

```typescript
import { OpenAI } from "langchain";
```

### CommonJS in Node.js

If your project is using CommonJS, you can use LangChain only with the dynamic `import()` syntax.
LangChain provides an ESM build targeting Node.js environments. You can import it using the following syntax:

```typescript
const { OpenAI } = await import("langchain");
import { OpenAI } from "langchain/llms";
```

If you're using TypeScript in a CommonJS project, you'll need to add the following to your `tsconfig.json`:
If you are using TypeScript in an ESM project we suggest updating your `tsconfig.json` to include the following:

```json
{
"compilerOptions": {
...
"moduleResolution": "node16",
"target": "ES2020", // or higher
"module": "nodenext",
}
}
```

### CommonJS in Node.js

LangChain provides a CommonJS build targeting Node.js environments. You can import it using the following syntax:

```typescript
const { OpenAI } = require("langchain/llms");
```

### Other environments

LangChain currently supports only Node.js-based environments. This includes Vercel Serverless functions (but not Edge functions), as well as other serverless environments, like AWS Lambda and Google Cloud Functions.
Expand Down
2 changes: 1 addition & 1 deletion examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
"include": [
"./src"
]
}
}
8 changes: 6 additions & 2 deletions langchain/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ module.exports = {
plugins: ["@typescript-eslint", "tree-shaking"],
ignorePatterns: [
".eslintrc.cjs",
"create-entrypoints.js",
"check-tree-shaking.js",
"scripts",
"node_modules",
"dist",
"dist-cjs",
"*.js",
"*.cjs",
"*.d.ts",
],
rules: {
"tree-shaking/no-side-effects-in-initialization": [
Expand Down
22 changes: 21 additions & 1 deletion langchain/.gitignore
Original file line number Diff line number Diff line change
@@ -1,40 +1,60 @@
agents.cjs
agents.js
agents.d.ts
base_language.cjs
base_language.js
base_language.d.ts
tools.cjs
tools.js
tools.d.ts
chains.cjs
chains.js
chains.d.ts
embeddings.cjs
embeddings.js
embeddings.d.ts
llms.cjs
llms.js
llms.d.ts
prompts.cjs
prompts.js
prompts.d.ts
vectorstores.cjs
vectorstores.js
vectorstores.d.ts
text_splitter.cjs
text_splitter.js
text_splitter.d.ts
memory.cjs
memory.js
memory.d.ts
document.cjs
document.js
document.d.ts
docstore.cjs
docstore.js
docstore.d.ts
document_loaders.cjs
document_loaders.js
document_loaders.d.ts
chat_models.cjs
chat_models.js
chat_models.d.ts
schema.cjs
schema.js
schema.d.ts
sql_db.cjs
sql_db.js
sql_db.d.ts
callbacks.cjs
callbacks.js
callbacks.d.ts
output_parsers.cjs
output_parsers.js
output_parsers.d.ts
retrievers.cjs
retrievers.js
retrievers.d.ts
index.cjs
index.js
index.d.ts
index.d.ts
Loading

1 comment on commit d221617

@vercel
Copy link

@vercel vercel bot commented on d221617 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.