Skip to main content
Skip to main content

Install in Node.js-Based Application

In this document, you’ll learn how to setup and use the Product Module in a Node.js based application.

Prerequisites

Before installing the Product Module in your application, make sure you have the following prerequisites:

  • Node.js v16 or greater
  • PostgreSQL database. You can use an existing Medusa database, or set up a new PostgreSQL database.

Install Package

In your Node.js-based applications, such as a Next.js application, you can install the Product Module with the following command:

npm install @medusajs/product

Add Database Configuration

Add the following environment variable to your application:

POSTGRES_URL=<DATABASE_URL>

Where <DATABASE_URL> is your database connection URL of the format postgres://[user][:password]@[host][:port]/[dbname]. You can learn more about the connection URL format in this guide.

You can also set the following optional environment variables:

  • POSTGRES_SCHEMA: a string indicating the PostgreSQL schema to use. By default, it's public.
  • POSTGRES_DRIVER_OPTIONS: a stringified JSON object indicating the PostgreSQL options to use. The JSON object is then parsed to be used as a JavaScript object. By default, it's {"connection":{"ssl":false}} for local PostgreSQL databases, and {"connection":{"ssl":{"rejectUnauthorized":false}}} for remote databases.
Note

If POSTGRES_DRIVER_OPTIONS is not specified, the PostgreSQL database is considered local if POSTGRES_URL includes localhost. Otherwise, it's considered remote.

Run Database Migrations

Note

You can skip this step if you use an existing Medusa database.

Migrations are used to create your database schema. Before you can run migrations, add in your package.json the following scripts:

"scripts": {
//...other scripts
"product:migrations:run": "medusa-product-migrations-up",
"product:seed": "medusa-product-seed ./seed-data.js"
}

The first command runs the migrations, and the second command allows you to seed your database with demo products optionally.

However, you’d need the following seed file added to the root of your project directory:

Seed File

Then run the commands you added to migrate the database schema and optionally seed data:

npm run product:migrations:run
# optionally
npm run product:seed

Next.js Application: Adjust Configurations

the Product Module uses dependencies that aren’t Webpack optimized. Since Next.js uses Webpack for compilation, you need to add the Product Module as an external dependency.

To do that, add the serverComponentsExternalPackages option in next.config.js:

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverComponentsExternalPackages: [
"@medusajs/product",
],
},
}

module.exports = nextConfig

Start Development

You can refer to the Example Usages documentation page for examples of using the Product Module.

You can also refer to the Module Interface Reference for a detailed reference on all available methods.

Was this section helpful?