Skip to content

Commit 91ff08b

Browse files
authored
Update README.md to include commonjs examples
1 parent c26c3f0 commit 91ff08b

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@ npm install replicate
2323
Create the client:
2424

2525
```js
26+
// CommonJS (default or using .cjs extension)
27+
const Replicate = require("replicate");
28+
29+
// ESM (where `"module": true` in package.json or using .mjs extension)
2630
import Replicate from "replicate";
31+
```
2732

33+
```
2834
const replicate = new Replicate({
2935
// get your token from https://replicate.com/account
3036
auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN
@@ -69,9 +75,11 @@ console.log(prediction.output);
6975

7076
To run a model that takes a file input, pass a URL to a publicly accessible file. Or, for smaller files (<10MB), you can convert file data into a base64-encoded data URI and pass that directly:
7177

72-
7378
```js
74-
import { promises as fs } from "fs";
79+
const fs = require("node:fs/promises");
80+
81+
// Or when using ESM.
82+
// import fs from "node:fs/promises";
7583

7684
// Read the file into a buffer
7785
const data = await fs.readFile("path/to/image.png");
@@ -90,6 +98,10 @@ const output = await replicate.run(model, { input });
9098
// ['https://replicate.delivery/mgxm/e7b0e122-9daa-410e-8cde-006c7308ff4d/output.png']
9199
```
92100

101+
## TypeScript
102+
103+
Currently in order to support the module format used by `replicate` you'll need to set `esModuleInterop` to `true` in your tsconfig.json.
104+
93105
## API
94106

95107
### Constructor
@@ -121,8 +133,12 @@ you can install a fetch function from an external package like
121133
and pass it to the `fetch` option in the constructor.
122134

123135
```js
124-
import Replicate from "replicate";
125-
import fetch from "cross-fetch";
136+
const Replicate = require("replicate");
137+
const fetch = require("fetch");
138+
139+
// Using ESM:
140+
// import Replicate from "replicate";
141+
// import fetch from "cross-fetch";
126142

127143
const replicate = new Replicate({ fetch });
128144
```

0 commit comments

Comments
 (0)