|
1
|
import { Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `${pageNumber} Use pnpm with Medusa`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this chapter, you'll learn how to use `pnpm` as the package manager for your Medusa application. |
|
10
|
|
|
11
|
## What is pnpm? |
|
12
|
|
|
13
|
[pnpm](https://pnpm.io/) is a fast, disk space-efficient package manager. It uses a unique symlinked storage mechanism to save disk space and speed up installations. |
|
14
|
|
|
15
|
Medusa supports `pnpm`, `yarn`, and `npm` as package managers. Our team recommends using either `yarn` or `pnpm` for better performance and faster installations. |
|
16
|
|
|
17
|
To use `pnpm` with Medusa, you need to add some configuration to your project. This guide walks you through the necessary steps for both standalone projects and monorepos. |
|
18
|
|
|
19
|
### Who is This Guide For? |
|
20
|
|
|
21
|
As of [Medusa v2.13.0](https://github.com/medusajs/medusa/releases/tag/v2.13.0), new Medusa projects support `pnpm`. You can create a new Medusa project with `pnpm` using the following command: |
|
22
|
|
|
23
|
```bash |
|
24
|
pnpm dlx create-medusa-app@latest |
|
25
|
``` |
|
26
|
|
|
27
|
This guide is intended for developers who want to switch an existing Medusa project from `npm` or `yarn` to `pnpm`. Choose the section that matches your project's setup: |
|
28
|
|
|
29
|
- [Standalone Project](#standalone-project): Your Medusa application is a single package in its own directory. |
|
30
|
- [Monorepo](#monorepo): Your Medusa application is one of multiple packages managed in a workspace. |
|
31
|
|
|
32
|
--- |
|
33
|
|
|
34
|
## Standalone Project |
|
35
|
|
|
36
|
Follow the steps in this section if your Medusa application is a standalone, single-package project. |
|
37
|
|
|
38
|
<Prerequisites |
|
39
|
items={[ |
|
40
|
{ |
|
41
|
text: "pnpm Installed", |
|
42
|
link: "https://pnpm.io/installation", |
|
43
|
} |
|
44
|
]} |
|
45
|
/> |
|
46
|
|
|
47
|
### Step 0: Update Medusa to the Latest Version |
|
48
|
|
|
49
|
If you're using a Medusa version prior to v2.13.0, you should update to the latest version to ensure compatibility with `pnpm`. |
|
50
|
|
|
51
|
Refer to the [Update Medusa](../../update/page.mdx) guide to learn how to update your Medusa project. |
|
52
|
|
|
53
|
If you're updating to v2.13.0, you also need to run the [replace-zod-imports codemod](../../codemods/replace-zod-imports/page.mdx). |
|
54
|
|
|
55
|
### Step 1: Remove Existing Lock File |
|
56
|
|
|
57
|
Delete the existing lock file in your project directory. Depending on your current package manager, this will be either `package-lock.json` for `npm` or `yarn.lock` for `yarn`. |
|
58
|
|
|
59
|
When you install dependencies with `pnpm` later, a new `pnpm-lock.yaml` file will be created automatically. |
|
60
|
|
|
61
|
### Step 2: Add .npmrc File |
|
62
|
|
|
63
|
Medusa requires some packages to be installed in the top-level `node_modules` directory. To ensure this works correctly with `pnpm`, you need to create a `.npmrc` file that specifies the `public-hoist-pattern` configuration. |
|
64
|
|
|
65
|
Create a `.npmrc` file in the root of your Medusa project with the following content: |
|
66
|
|
|
67
|
```ini title=".npmrc" |
|
68
|
public-hoist-pattern[]=*@medusajs/* |
|
69
|
public-hoist-pattern[]=@tanstack/react-query |
|
70
|
public-hoist-pattern[]=react-i18next |
|
71
|
public-hoist-pattern[]=react-router-dom |
|
72
|
``` |
|
73
|
|
|
74
|
This configuration tells `pnpm` to hoist the following packages to the top-level `node_modules` directory: |
|
75
|
|
|
76
|
1. All packages under the `@medusajs` scope. |
|
77
|
2. `@tanstack/react-query`: Tanstack Query, used in [Medusa Admin customizations](../../fundamentals/admin/tips/page.mdx#send-requests-to-api-routes). It's hoisted to avoid version and instance conflicts with the Medusa Admin. |
|
78
|
3. `react-i18next`: Used for [internationalization in Medusa Admin customizations](../../fundamentals/admin/translations/page.mdx). It's hoisted to ensure compatibility with the Medusa Admin. |
|
79
|
4. `react-router-dom`: Used for [routing customizations in the Medusa Admin](../../fundamentals/admin/routing/page.mdx). It's hoisted to avoid version and instance conflicts with the Medusa Admin. |
|
80
|
|
|
81
|
### Step 3: Install Dependencies with pnpm |
|
82
|
|
|
83
|
Finally, install your project dependencies using `pnpm`. Run the following command in your project directory: |
|
84
|
|
|
85
|
```bash |
|
86
|
pnpm install |
|
87
|
``` |
|
88
|
|
|
89
|
This will install all your dependencies in `node_modules` and create a `pnpm-lock.yaml` file to lock the versions of your installed packages. |
|
90
|
|
|
91
|
--- |
|
92
|
|
|
93
|
## Monorepo |
|
94
|
|
|
95
|
Follow the steps in this section if your Medusa application is part of a monorepo, where it's one of multiple packages managed in a workspace. |
|
96
|
|
|
97
|
<Prerequisites |
|
98
|
items={[ |
|
99
|
{ |
|
100
|
text: "pnpm Installed", |
|
101
|
link: "https://pnpm.io/installation", |
|
102
|
} |
|
103
|
]} |
|
104
|
/> |
|
105
|
|
|
106
|
### Step 1: Set pnpm as the Package Manager |
|
107
|
|
|
108
|
In the root `package.json` file of your monorepo, set the `packageManager` field to the `pnpm` version you have installed: |
|
109
|
|
|
110
|
```json title="package.json" |
|
111
|
{ |
|
112
|
"packageManager": "pnpm@10.11.1" |
|
113
|
} |
|
114
|
``` |
|
115
|
|
|
116
|
If your `package.json` has a `workspaces` field, remove it. With `pnpm`, you define the workspace packages in a `pnpm-workspace.yaml` file instead, as explained in the next step. |
|
117
|
|
|
118
|
### Step 2: Define Workspace Packages |
|
119
|
|
|
120
|
Create a `pnpm-workspace.yaml` file at the root of your monorepo to define the packages in your workspace. |
|
121
|
|
|
122
|
For example, if your packages are under an `apps` directory: |
|
123
|
|
|
124
|
```yaml title="pnpm-workspace.yaml" |
|
125
|
packages: |
|
126
|
- "apps/*" |
|
127
|
``` |
|
128
|
|
|
129
|
Adjust the patterns to match the directories where your packages are located. |
|
130
|
|
|
131
|
### Step 3: Add .npmrc File |
|
132
|
|
|
133
|
Medusa requires some packages to be installed in the top-level `node_modules` directory. To ensure this works correctly with `pnpm`, create a `.npmrc` file at the root of your monorepo with the following content: |
|
134
|
|
|
135
|
```ini title=".npmrc" |
|
136
|
public-hoist-pattern[]=*@medusajs/* |
|
137
|
public-hoist-pattern[]=@tanstack/react-query |
|
138
|
public-hoist-pattern[]=react-i18next |
|
139
|
public-hoist-pattern[]=react-router-dom |
|
140
|
``` |
|
141
|
|
|
142
|
This configuration tells `pnpm` to hoist the following packages to the top-level `node_modules` directory: |
|
143
|
|
|
144
|
1. All packages under the `@medusajs` scope. |
|
145
|
2. `@tanstack/react-query`: Tanstack Query, used in [Medusa Admin customizations](../../fundamentals/admin/tips/page.mdx#send-requests-to-api-routes). It's hoisted to avoid version and instance conflicts with the Medusa Admin. |
|
146
|
3. `react-i18next`: Used for [internationalization in Medusa Admin customizations](../../fundamentals/admin/translations/page.mdx). It's hoisted to ensure compatibility with the Medusa Admin. |
|
147
|
4. `react-router-dom`: Used for [routing customizations in the Medusa Admin](../../fundamentals/admin/routing/page.mdx). It's hoisted to avoid version and instance conflicts with the Medusa Admin. |
|
148
|
|
|
149
|
### Step 4: Remove Existing Lock Files |
|
150
|
|
|
151
|
Delete the existing lock file and the `node_modules` directory in your monorepo. Depending on your previous package manager, the lock file is either `package-lock.json` for `npm` or `yarn.lock` for `yarn`. |
|
152
|
|
|
153
|
When you install dependencies with `pnpm` in the next step, a new `pnpm-lock.yaml` file will be created automatically. |
|
154
|
|
|
155
|
### Step 5: Install Dependencies with pnpm |
|
156
|
|
|
157
|
Finally, install your monorepo's dependencies using `pnpm`. Run the following command in the root of your monorepo: |
|
158
|
|
|
159
|
```bash |
|
160
|
pnpm install |
|
161
|
``` |
|
162
|
|
|
163
|
This will install the dependencies of all packages in your workspace and create a `pnpm-lock.yaml` file to lock the versions of your installed packages. |
|
164
|
|
|
165
|
### Workspace Package Dependencies |
|
166
|
|
|
167
|
If your monorepo contains packages that import `@medusajs/framework` in their compiled output (for example, custom plugins or shared packages), declare `@medusajs/framework` as a `peerDependency` in those packages rather than a `devDependency`: |
|
168
|
|
|
169
|
```json title="apps/my-plugin/package.json" |
|
170
|
{ |
|
171
|
"peerDependencies": { |
|
172
|
"@medusajs/framework": "*" |
|
173
|
} |
|
174
|
} |
|
175
|
``` |
|
176
|
|
|
177
|
Declaring it only as a `devDependency` can cause a `Cannot find module '@medusajs/framework/utils'` error at runtime, because `devDependencies` are not included in production deployments. |
|
178
|
|
|
179
|
--- |
|
180
|
|
|
181
|
## Test Your Setup |
|
182
|
|
|
183
|
You can now use `pnpm` for your Medusa development. For example, you can start the Medusa server in development with the following command: |
|
184
|
|
|
185
|
```bash |
|
186
|
pnpm dev |
|
187
|
``` |
|
188
|
|
|
189
|
In a monorepo, run this command from your Medusa application's package directory, such as `apps/backend`. |
|
190
|
|
|
191
|
You can also run commands from the Medusa CLI tool with `pnpm`. For example, to run migrations: |
|
192
|
|
|
193
|
```bash |
|
194
|
pnpm medusa db:migrate |
|
195
|
``` |
|
196
|
|
|
197
|
Refer to the [Medusa CLI reference](!resources!/medusa-cli) for a full list of available commands. |