|
1
|
import { DetailsList, Prerequisites } from "docs-ui" |
|
2
|
import CmaOptionTroubleshooting from "../troubleshooting/_sections/nextjs/cma-option.mdx" |
|
3
|
import CorsErrorTroubleshooting from "../troubleshooting/_sections/other/cors-errors.mdx" |
|
4
|
import ModuleXErrorTroubleshooting from "../troubleshooting/_sections/common-installation-errors/module-x-error.mdx" |
|
5
|
import Node25Troubleshooting from "../troubleshooting/_sections/create-medusa-app-errors/nextjs-node-25.mdx" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Next.js Starter Storefront`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
Since Medusa v2.14.0, the Next.js Starter storefront is installed in a monorepo with the Medusa application when using `create-medusa-app`. The [standalone Next.js Starter Storefront](https://github.com/medusajs/nextjs-starter-medusa) is now deprecated. |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The Medusa application is made up of a Node.js server and an admin dashboard. The storefront is hosted separately from the Medusa application. You have the flexibility to choose the frontend tech stack that you and your team are proficient in, and implement unique design systems and user experience. |
|
20
|
|
|
21
|
The Next.js Starter Storefront is a good place to start building your storefront. It provides rich commerce features and a sleek design. Developers and businesses can use it as-is or build on top of it to tailor it for the business's unique use case, design, and customer experience. |
|
22
|
|
|
23
|
<Note> |
|
24
|
|
|
25
|
This guide helps technical users set up the Next.js Start storefront. If you're not a technical user, refer your technical team to this documentation instead. |
|
26
|
|
|
27
|
</Note> |
|
28
|
|
|
29
|
## Installation |
|
30
|
|
|
31
|
### Approach 1: Install with Medusa Application |
|
32
|
|
|
33
|
<Prerequisites items={[ |
|
34
|
{ |
|
35
|
text: "Node.js v20.19.0+ or v22.12.0+ LTS, less than v25", |
|
36
|
link: "https://nodejs.org/en/download" |
|
37
|
}, |
|
38
|
{ |
|
39
|
text: "Git CLI tool", |
|
40
|
link: "https://git-scm.com/downloads" |
|
41
|
}, |
|
42
|
{ |
|
43
|
text: "PostgreSQL", |
|
44
|
link: "https://www.postgresql.org/download/" |
|
45
|
} |
|
46
|
]} /> |
|
47
|
|
|
48
|
If you don't have a Medusa application installed, run the following command to install both the Medusa application and the Next.js Starter storefront: |
|
49
|
|
|
50
|
```bash npx2yarnExec |
|
51
|
npx create-medusa-app@latest --with-nextjs-starter |
|
52
|
``` |
|
53
|
|
|
54
|
You'll be asked for the project's name, then the installation will start. |
|
55
|
|
|
56
|
The command will create a `{project-name}` directory that holds a monorepository. The Medusa backend will be installed in the `apps/backend` directory, and the Next.js Starter storefront will be installed in the `apps/storefront` directory. |
|
57
|
|
|
58
|
Once the installation is done, the Medusa Admin dashboard will open in your default browser at `http://localhost:9000/app`, and the Next.js Starter storefront will be running at `http://localhost:8000`. |
|
59
|
|
|
60
|
For other information related to the Medusa application's installation, refer to the [Installation guide](!docs!/learn/installation). |
|
61
|
|
|
62
|
### Approach 2: Install separately |
|
63
|
|
|
64
|
<Prerequisites items={[ |
|
65
|
{ |
|
66
|
text: "Node.js v20.19.0+ or v22.12.0+ LTS, less than v25", |
|
67
|
link: "https://nodejs.org/en/download" |
|
68
|
}, |
|
69
|
{ |
|
70
|
text: "Git CLI tool", |
|
71
|
link: "https://git-scm.com/downloads" |
|
72
|
}, |
|
73
|
{ |
|
74
|
text: "An existing Medusa monorepo with the backend running.", |
|
75
|
}, |
|
76
|
{ |
|
77
|
text: "At least one region in the Medusa application.", |
|
78
|
}, |
|
79
|
]} /> |
|
80
|
|
|
81
|
If you already have a Medusa monorepo installed and want to add the Next.js Starter storefront to it, follow these steps. |
|
82
|
|
|
83
|
1. From the root of your monorepo, clone the [DTC Starter](https://github.com/medusajs/dtc-starter) into a temporary directory to extract the storefront: |
|
84
|
|
|
85
|
```bash |
|
86
|
git clone https://github.com/medusajs/dtc-starter.git --depth=1 dtc-temp |
|
87
|
``` |
|
88
|
|
|
89
|
2. Copy the storefront into your project's `apps` directory, then remove the temporary clone: |
|
90
|
|
|
91
|
```bash |
|
92
|
cp -r dtc-temp/apps/storefront apps/storefront |
|
93
|
rm -rf dtc-temp |
|
94
|
``` |
|
95
|
|
|
96
|
3. Make sure your workspace configuration includes the `apps/storefront` directory. If your `pnpm-workspace.yaml` or the `workspaces` field in `package.json` already uses a glob like `apps/*`, no change is needed. Otherwise, add it explicitly: |
|
97
|
|
|
98
|
```yaml title="pnpm-workspace.yaml" |
|
99
|
packages: |
|
100
|
- 'apps/*' |
|
101
|
``` |
|
102
|
|
|
103
|
4. From the monorepo root, install the dependencies: |
|
104
|
|
|
105
|
```bash npm2yarn |
|
106
|
npm install |
|
107
|
``` |
|
108
|
|
|
109
|
5. Create the storefront's environment file from its template: |
|
110
|
|
|
111
|
```bash |
|
112
|
cp apps/storefront/.env.template apps/storefront/.env.local |
|
113
|
``` |
|
114
|
|
|
115
|
6. In `apps/storefront/.env.local`, set the URL of your Medusa backend and your publishable API key. You can retrieve the publishable API key from the Medusa Admin dashboard by going to **Settings** > **API Key Management**: |
|
116
|
|
|
117
|
```bash title="apps/storefront/.env.local" |
|
118
|
NEXT_PUBLIC_MEDUSA_BACKEND_URL=http://localhost:9000 |
|
119
|
NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=pk_123... |
|
120
|
``` |
|
121
|
|
|
122
|
7. While the Medusa application is running, start the Next.js Starter storefront from the `apps/storefront` directory: |
|
123
|
|
|
124
|
```bash npm2yarn |
|
125
|
cd apps/storefront |
|
126
|
npm run dev |
|
127
|
``` |
|
128
|
|
|
129
|
Your Next.js Starter storefront is now running at `http://localhost:8000`. |
|
130
|
|
|
131
|
--- |
|
132
|
|
|
133
|
## Troubleshooting |
|
134
|
|
|
135
|
<DetailsList |
|
136
|
sections={[ |
|
137
|
{ |
|
138
|
title: "Installing Next.js Starter with create-medusa-app not working", |
|
139
|
content: <CmaOptionTroubleshooting /> |
|
140
|
}, |
|
141
|
{ |
|
142
|
title: "CORS Error", |
|
143
|
content: <CorsErrorTroubleshooting /> |
|
144
|
}, |
|
145
|
{ |
|
146
|
title: `Resolve "Cannot find module X" Errors`, |
|
147
|
content: <ModuleXErrorTroubleshooting /> |
|
148
|
}, |
|
149
|
{ |
|
150
|
title: `Issues when installing with Node v25+`, |
|
151
|
content: <Node25Troubleshooting /> |
|
152
|
} |
|
153
|
]} |
|
154
|
/> |
|
155
|
|
|
156
|
## Payment Providers |
|
157
|
|
|
158
|
### Stripe Integration |
|
159
|
|
|
160
|
The Next.js Starter storefront is compatible with Medusa's Stripe Module Provider. |
|
161
|
|
|
162
|
<Prerequisites items={[ |
|
163
|
{ |
|
164
|
text: "Stripe Module Provider installed in the Medusa application.", |
|
165
|
link: "../commerce-modules/payment/payment-provider/stripe/page.mdx" |
|
166
|
}, |
|
167
|
]} /> |
|
168
|
|
|
169
|
In your Next.js Starter project, set the following environment variables for the Stripe integration: |
|
170
|
|
|
171
|
```bash |
|
172
|
NEXT_PUBLIC_STRIPE_KEY=<YOUR_PUBLISHABLE_KEY> |
|
173
|
``` |
|
174
|
|
|
175
|
Where `<YOUR_PUBLISHABLE_KEY>` is your Stripe publishable key. You can retrieve it from your [Stripe dashboard](https://dashboard.stripe.com/) by going to Developers → API Keys. |
|
176
|
|
|
177
|
Then, restart the Next.js Starter storefront. You can now use Stripe during checkout. |
|
178
|
|
|
179
|
<Note title="Tip"> |
|
180
|
|
|
181
|
Refer to [this guide](./guides/customize-stripe/page.mdx) to learn how to customize the Stripe integration and components. |
|
182
|
|
|
183
|
</Note> |
|
184
|
|
|
185
|
--- |
|
186
|
|
|
187
|
## Change Medusa Application URL |
|
188
|
|
|
189
|
By default, the Medusa application runs at `http://localhost:9000`. This value is defined in your Next.js Starter storefront under the environment variable `MEDUSA_BACKEND_URL`. |
|
190
|
|
|
191
|
To change the URL of the Medusa application in the storefront, set the `MEDUSA_BACKEND_URL` environment variable: |
|
192
|
|
|
193
|
```bash |
|
194
|
MEDUSA_BACKEND_URL=https://example.com |
|
195
|
``` |
|
196
|
|
|
197
|
--- |
|
198
|
|
|
199
|
## Change Medusa Publishable API Key |
|
200
|
|
|
201
|
All requests made from a storefront must pass a publishable API key in the request's header. This API key is created in the Medusa application. If you've installed the Next.js Starter storefront using `create-medusa-app`, the publishable API key is set in the environment variables automatically. |
|
202
|
|
|
203
|
If you change your Medusa application or its database, or you lose the publishable API key you had before, make sure to change the `NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY` environment variable to the new publishable API key: |
|
204
|
|
|
205
|
```bash |
|
206
|
NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY=pk_123... |
|
207
|
``` |