Run a model from Node.js
Learn how to run a model on Replicate using Node.js.
This guide includes a quickstart to scaffold a new project with a single command in your terminal, followed by a step-by-step guide for setting up a project from scratch. By the end, you’ll have a working Node.js project that can run any model on Replicate.
Node.js 16 or greater: The simplest way to install Node.js is using the installer at nodejs.org.
🐇 Quickstart: Scaffold a project with a one-liner
To get up and running as quickly as possible, you can use create-replicate, an npm package that creates a project directory for you, writes some starter code, installs the dependencies, and runs the code.
Run the following command to scaffold a new project:
npm create replicate
That’s it. You should now have a working Node.js project that generates images with the SDXL model using Replicate’s API.
If you want to use a different model than SDXL, specify it when creating your project:
npm create replicate --model yorickvp/llava-13b
To learn more about scaffolding new Node.js projects, check out the create-replicate documentation.
🐢 Slowstart: Set up a project from scratch
If you prefer to manually set up your Node.js project step by step, follow the instructions below.
Step 1: Authenticate
Authenticate by setting your Replicate API token in an environment variable:
Step 2: Create a new Node.js project
# create the directory
# set up package.json
npm init -y
npm pkg set type=module
npm install replicate
Step 4: Write some code
Create a file called
index.js and add the following code:
import Replicate from "replicate";
const replicate = new Replicate();
console.log("Running the model...");
const output = await replicate.run(
prompt: "An astronaut riding a rainbow unicorn, cinematic, dramatic",
Step 5: Run your code
Next, run your code from your terminal:
You should see the output from the model, which will be a URL to the generated image. It should look something like this:
Running the model...
Now you’re up and running on Replicate with Node.js. 🚀🐢
In this guide you used the SDXL image generation model, but you can adapt the code to run any model on Replicate. Try chatting with images using the LLaVa vision model, or writing AI-generated Pyton code using CodeLlama. There are thousands of public models on Replicate, and you can run any of them using the project you just created.