@@ -23,8 +23,14 @@ npm install replicate
23
23
Create the client:
24
24
25
25
``` 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)
26
30
import Replicate from " replicate" ;
31
+ ```
27
32
33
+ ```
28
34
const replicate = new Replicate({
29
35
// get your token from https://replicate.com/account
30
36
auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN
@@ -69,9 +75,11 @@ console.log(prediction.output);
69
75
70
76
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:
71
77
72
-
73
78
``` 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";
75
83
76
84
// Read the file into a buffer
77
85
const data = await fs .readFile (" path/to/image.png" );
@@ -90,6 +98,10 @@ const output = await replicate.run(model, { input });
90
98
// ['https://replicate.delivery/mgxm/e7b0e122-9daa-410e-8cde-006c7308ff4d/output.png']
91
99
```
92
100
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
+
93
105
## API
94
106
95
107
### Constructor
@@ -121,8 +133,12 @@ you can install a fetch function from an external package like
121
133
and pass it to the ` fetch ` option in the constructor.
122
134
123
135
``` 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";
126
142
127
143
const replicate = new Replicate ({ fetch });
128
144
```
0 commit comments