Raw
1 ---
2 generate_toc: true
3 ---
4
5 import { CodeTabs, CodeTab, Note, Prerequisites } from "docs-ui"
6
7 export const metadata = {
8 title: `Medusa ESLint Plugin`,
9 }
10
11 # {metadata.title}
12
13 In this section of the documentation, you'll learn how to set up and use the Medusa ESLint plugin in your application.
14
15 The Medusa ESLint plugin (`@medusajs/eslint-plugin`) is the official ESLint plugin for Medusa projects. It codifies Medusa's conventions into static checks that run while you write code in your editor and when you build or develop your application.
16
17 The plugin is useful for both developers and AI agents working on your codebase, as it provides immediate feedback on code that violates Medusa's conventions.
18
19 The plugin catches mistakes that often lead to runtime errors or unexpected behavior, such as using loops in a workflow's constructor, returning a non-serializable value from a step, or naming an API route file incorrectly. Each [rule](./rules/page.mdx) maps to a Medusa convention.
20
21 ---
22
23 ## How to Install the Plugin
24
25 <Note title="Tip">
26
27 Projects created with Medusa [v2.16.0](https://github.com/medusajs/medusa/releases/tag/v2.16.0) or later already have the plugin installed and an `eslint.config.ts` file set up. You can skip the installation steps and go straight to the [usage guidelines](#usage-guidelines).
28
29 </Note>
30
31 ### 1. Installation
32
33 <Prerequisites
34 items={[
35 {
36 text: "Node.js v20.19.0+ or v22.12.0+",
37 link: "https://nodejs.org/en/download",
38 },
39 {
40 text: "TypeScript v4.8+",
41 link: "https://www.typescriptlang.org/download",
42 },
43 ]}
44 />
45
46 To use the plugin in an existing project, install it along with ESLint and `jiti`:
47
48 <Note>
49
50 In monorepo projects, install the plugin and its dependencies in the root `package.json`. In standalone Medusa projects, install them within the project.
51
52 </Note>
53
54 ```bash npm2yarn
55 npm install --save-dev @medusajs/eslint-plugin eslint jiti
56 ```
57
58 Where:
59
60 - `@medusajs/eslint-plugin` is the plugin that provides Medusa's lint rules.
61 - `eslint` is a peer dependency of the plugin. The plugin supports ESLint v8.57 and later, including the [flat config format](https://eslint.org/docs/latest/use/configure/configuration-files) introduced in v9.
62 - `jiti` allows ESLint to load a TypeScript configuration file (`eslint.config.ts`). Install it if you want to write your ESLint configuration in TypeScript.
63
64 ### 2. Add an ESLint Configuration File
65
66 #### a. Configuration for Monorepo Projects
67
68 After installing the plugin, create an `eslint.config.ts` file at the root of your monorepo that spreads one of the plugin's [presets](#presets):
69
70 ```ts title="eslint.config.ts"
71 import { defineConfig } from "eslint/config"
72 import medusa from "@medusajs/eslint-plugin"
73
74 export default defineConfig([...medusa.configs.recommended])
75 ```
76
77 <Note>
78
79 The plugin ignores `node_modules`, `dist`, `apps/storefront`, and test directories by default.
80
81 </Note>
82
83 The plugin only supports ESLint's flat config format.
84
85 Then, in the Medusa application package, create a similar `eslint.config.ts` file:
86
87 ```ts title="apps/medusa-app/eslint.config.ts"
88 import { defineConfig } from "eslint/config"
89 import medusa from "@medusajs/eslint-plugin"
90
91 export default defineConfig([...medusa.configs.recommended])
92 ```
93
94 This setup is necessary to support both `medusa lint` command and code editor hints.
95
96 You can add overide rules in the application package's configuration file to customize linting, as explained in the [usage guidelines](#usage-guidelines).
97
98 To avoid type errors on this file, make sure to add it to `apps/backend/tsconfig.json`'s `exclude` array:
99
100 ```json title="apps/backend/tsconfig.json"
101 {
102 "exclude": [
103 // ...
104 "eslint.config.*"
105 ]
106 }
107 ```
108
109 #### b. Configuration for Standalone Medusa Projects
110
111 After installing the plugin, create an `eslint.config.ts` file at the root of your project that spreads one of the plugin's [presets](#presets):
112
113 ```ts title="eslint.config.ts"
114 import { defineConfig } from "eslint/config"
115 import medusa from "@medusajs/eslint-plugin"
116
117 export default defineConfig([...medusa.configs.recommended])
118 ```
119
120 The plugin only supports ESLint's flat config format.
121
122 You can add your own configuration blocks after the preset to customize linting, as explained in the [usage guidelines](#usage-guidelines).
123
124 #### Configuration with ESLint v8.57
125
126 The plugin's presets use the flat config format, which is the default since ESLint v9. If you're using ESLint v8.57, you can still use the same `eslint.config.ts` file, but you must opt in to the flat config format.
127
128 The [Medusa CLI lint commands](#lint-your-project) load the `eslint.config.ts` file directly, so they work with ESLint v8.57 without any extra setup.
129
130 However, when you run the `eslint` command directly or use an editor integration, ESLint v8 doesn't use the flat config file by default. To enable it, set the `ESLINT_USE_FLAT_CONFIG` environment variable to `true`:
131
132 ```bash
133 ESLINT_USE_FLAT_CONFIG=true npx eslint .
134 ```
135
136 <Note>
137
138 The plugin supports ESLint v8.57 and later. Earlier v8 versions don't support the flat config format.
139
140 </Note>
141
142 ### 3. Set Up Your IDE
143
144 It's highly recommended to install an ESLint plugin or extension in your code editor. This way, you see the plugin's errors and warnings inline as you write code, and you can auto-fix issues during development instead of waiting for a build or a manual lint run.
145
146 For example:
147
148 - If you use [VS Code](https://code.visualstudio.com/), install the [ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).
149 - If you use a [JetBrains IDE](https://www.jetbrains.com/) such as WebStorm, enable ESLint in the IDE's settings.
150
151 To auto-fix issues whenever you save a file in VS Code, enable the following in your editor settings:
152
153 ```json title=".vscode/settings.json"
154 {
155 "editor.codeActionsOnSave": {
156 "source.fixAll.eslint": "explicit"
157 }
158 }
159 ```
160
161 ---
162
163 ## Presets
164
165 The plugin provides three presets that you spread into your ESLint configuration. Choose the preset based on your package type and how thorough you want the linting to be.
166
167 ### recommended
168
169 The `recommended` preset is the default for Medusa projects. It enables rules scoped to your project's directories, such as `src/workflows` and `src/api`. These rules don't require TypeScript type information, so they're fast to run.
170
171 Use this preset in all Medusa projects:
172
173 ```ts title="eslint.config.ts"
174 import { defineConfig } from "eslint/config"
175 import medusa from "@medusajs/eslint-plugin"
176
177 export default defineConfig([...medusa.configs.recommended])
178 ```
179
180 ### strict
181
182 The `strict` preset extends `recommended` with additional rules that require TypeScript type information or that check across multiple files. These rules are more thorough, but slower to run.
183
184 To use the type-aware rules, your ESLint configuration must set `parserOptions.project`. Use this preset when you want the most thorough checking:
185
186 ```ts title="eslint.config.ts"
187 import { defineConfig } from "eslint/config"
188 import medusa from "@medusajs/eslint-plugin"
189
190 export default defineConfig([...medusa.configs.strict])
191 ```
192
193 ### modules
194
195 The `modules` preset is meant for packages whose only purpose is to provide a module or a module provider, such as a published module package. In these packages, a module's source code lives directly under `src`, rather than under `src/modules` as it does in a Medusa application.
196
197 This preset applies only the rules that govern a module's service, module definition, and data models, scoped to the package's layout. It's zero-config and doesn't require TypeScript type information.
198
199 ```ts title="eslint.config.ts"
200 import { defineConfig } from "eslint/config"
201 import medusa from "@medusajs/eslint-plugin"
202
203 export default defineConfig([...medusa.configs.modules])
204 ```
205
206 <Note title="Don't use this preset if" type="error">
207
208 - Your package is a Medusa application. Use the [recommended](#recommended) preset instead.
209 - Your package is a plugin that defines modules along with other customizations, such as API routes or workflows. Use the [recommended](#recommended) preset instead, as it scopes module rules to the `src/modules` directory.
210
211 </Note>
212
213 ---
214
215 ## Lint your Project
216
217 The Medusa CLI provides commands to lint your project. The commands require a flat ESLint config file (`eslint.config.ts`, `eslint.config.js`, or another supported extension). Config detection uses ESLint's own resolution, which walks ancestor directories, so a config at a monorepo root applies to nested projects.
218
219 ### medusa lint
220
221 Use the `medusa lint` command to lint your project:
222
223 ```bash npx2yarn
224 npx medusa lint
225 ```
226
227 By default, the command lints the whole project. You can pass paths to lint specific files or directories:
228
229 ```bash npx2yarn
230 npx medusa lint src/workflows src/api
231 ```
232
233 The command accepts the following options:
234
235 - `--fix`: Auto-fix lint issues where possible.
236 - `--quiet`: Report lint errors only, suppressing warnings.
237
238 The command exits with a non-zero code if it finds lint errors.
239
240 ### Lint while Developing
241
242 The `medusa develop` command lints your project once at startup, before the development server starts. If the linter finds errors, the development server doesn't start.
243
244 The command accepts the following lint options:
245
246 - `--lint` and `--no-lint`: Whether to run the linter before starting the development server. By default, linting is enabled.
247 - `--fix` and `--no-fix`: Auto-fix lint issues where possible before starting. By default, auto-fixing is disabled.
248
249 For example, to start the development server without linting:
250
251 ```bash
252 npx medusa develop --no-lint
253 ```
254
255 ### Lint while Building
256
257 The `medusa build` command lints your project after generating types and before building. Unlike `develop`, linting during `build` is non-blocking: lint problems are surfaced as warnings, but they never fail the build.
258
259 The command accepts the following lint options:
260
261 - `--lint` and `--no-lint`: Whether to run the linter before building. By default, linting is enabled.
262 - `--fix` and `--no-fix`: Auto-fix lint issues where possible before building. By default, auto-fixing is disabled.
263 - `--quiet`: Report lint errors only, suppressing warnings.
264
265 For example, to build your project without linting:
266
267 ```bash
268 npx medusa build --no-lint
269 ```
270
271 <Note title="Tip">
272
273 The linter caches results at `.medusa/cache/.eslintcache` to speed up repeated runs.
274
275 </Note>
276
277 ---
278
279 ## Usage Guidelines
280
281 ### Rule Severities
282
283 Each rule has one of two severities:
284
285 - `error`: The rule flags code that heavily violates Medusa's conventions and may lead to runtime errors or unexpected results.
286 - `warn`: The rule flags code that you should review, but that won't necessarily break your application.
287
288 The severity of each rule is mentioned in its [reference page](./rules/page.mdx).
289
290 ### Change a Rule's Severity or Disable it
291
292 To change a rule's severity or disable it, add a configuration block after the preset in your Medusa application's `eslint.config.ts` file.
293
294 <Note title="Tip">
295
296 If your project is a monorepo, add the configuration block in `apps/backend/eslint.config.ts`. If your project is a standalone Medusa application, add the configuration block in `eslint.config.ts` at the root of your project.
297
298 </Note>
299
300 For example, to disable the `admin-env-vars-import-meta` rule:
301
302 ```ts title="eslint.config.ts"
303 import { defineConfig } from "eslint/config"
304 import medusa from "@medusajs/eslint-plugin"
305
306 export default defineConfig([
307 ...medusa.configs.recommended,
308 {
309 rules: {
310 "@medusajs/admin-env-vars-import-meta": "off",
311 },
312 },
313 ])
314 ```
315
316 You can also disable a rule for a single line using an inline comment:
317
318 ```ts
319 // eslint-disable-next-line @medusajs/admin-env-vars-import-meta
320 console.log(process.env.MEDUSA_BACKEND_URL)
321 ```
322
323 Or disable a rule for a whole file:
324
325 ```ts
326 /* eslint-disable @medusajs/admin-env-vars-import-meta */
327 // The rest of the file's code
328 ```
329
330 ### Don't Disable Error Rules
331
332 Rules with the `error` severity flag code that heavily violates Medusa's conventions. Disabling them can lead to runtime errors or unexpected behavior in your application. If a rule's severity is `error`, fix the underlying issue instead of disabling the rule.
333
334 Only consider disabling rules with the `warn` severity, and only when you're certain the rule reports a false positive in your case. Each warning rule's [reference page](./rules/page.mdx) shows how to turn it off.
335
336 ---
337
338 ## Available Rules
339
340 Refer to the [Rules reference](./rules/page.mdx) for the full list of rules, including what each rule targets, why it's important, and its severity.