1# JavaScript
2
3JavaScript support is available natively in Zed.
4
5- Tree-sitter: [tree-sitter/tree-sitter-javascript](https://github.com/tree-sitter/tree-sitter-javascript)
6- Language Server: [typescript-language-server/typescript-language-server](https://github.com/typescript-language-server/typescript-language-server)
7- Debug Adapter: [vscode-js-debug](https://github.com/microsoft/vscode-js-debug)
8
9## Code formatting
10
11Formatting on save is enabled by default for JavaScript, using TypeScript's built-in code formatting.
12But many JavaScript projects use other command-line code-formatting tools, such as [Prettier](https://prettier.io/).
13You can use one of these tools by specifying an _external_ code formatter for JavaScript in your settings.
14See [the configuration docs](../configuring-zed.md) for more information.
15
16For example, if you have Prettier installed and on your `PATH`, you can use it to format JavaScript files by adding the following to your `settings.json`:
17
18```json [settings]
19{
20 "languages": {
21 "JavaScript": {
22 "formatter": {
23 "external": {
24 "command": "prettier",
25 "arguments": ["--stdin-filepath", "{buffer_path}"]
26 }
27 }
28 }
29 }
30}
31```
32
33## JSX
34
35Zed supports JSX syntax highlighting out of the box.
36
37In JSX strings, the [`tailwindcss-language-server`](./tailwindcss.md) is used provide autocompletion for Tailwind CSS classes.
38
39## JSDoc
40
41Zed supports JSDoc syntax in JavaScript and TypeScript comments that match the JSDoc syntax.
42Zed uses [tree-sitter/tree-sitter-jsdoc](https://github.com/tree-sitter/tree-sitter-jsdoc) for parsing and highlighting JSDoc.
43
44## ESLint
45
46You can configure Zed to format code using `eslint --fix` by running the ESLint code action when formatting:
47
48```json [settings]
49{
50 "languages": {
51 "JavaScript": {
52 "code_actions_on_format": {
53 "source.fixAll.eslint": true
54 }
55 }
56 }
57}
58```
59
60You can also only execute a single ESLint rule when using `fixAll`:
61
62```json [settings]
63{
64 "languages": {
65 "JavaScript": {
66 "code_actions_on_format": {
67 "source.fixAll.eslint": true
68 }
69 }
70 },
71 "lsp": {
72 "eslint": {
73 "settings": {
74 "codeActionOnSave": {
75 "rules": ["import/order"]
76 }
77 }
78 }
79 }
80}
81```
82
83> **Note:** the other formatter you have configured will still run, after ESLint.
84> So if your language server or Prettier configuration don't format according to
85> ESLint's rules, then they will overwrite what ESLint fixed and you end up with
86> errors.
87
88If you **only** want to run ESLint on save, you can configure code actions as
89the formatter:
90
91```json [settings]
92{
93 "languages": {
94 "JavaScript": {
95 "code_actions_on_format": {
96 "source.fixAll.eslint": true
97 }
98 }
99 }
100}
101```
102
103### Configure ESLint's `nodePath`:
104
105You can configure ESLint's `nodePath` setting:
106
107```json [settings]
108{
109 "lsp": {
110 "eslint": {
111 "settings": {
112 "nodePath": ".yarn/sdks"
113 }
114 }
115 }
116}
117```
118
119### Configure ESLint's `problems`:
120
121You can configure ESLint's `problems` setting.
122
123For example, here's how to set `problems.shortenToSingleLine`:
124
125```json [settings]
126{
127 "lsp": {
128 "eslint": {
129 "settings": {
130 "problems": {
131 "shortenToSingleLine": true
132 }
133 }
134 }
135 }
136}
137```
138
139### Configure ESLint's `rulesCustomizations`:
140
141You can configure ESLint's `rulesCustomizations` setting:
142
143```json [settings]
144{
145 "lsp": {
146 "eslint": {
147 "settings": {
148 "rulesCustomizations": [
149 // set all eslint errors/warnings to show as warnings
150 { "rule": "*", "severity": "warn" }
151 ]
152 }
153 }
154 }
155}
156```
157
158### Configure ESLint's `workingDirectory`:
159
160You can configure ESLint's `workingDirectory` setting:
161
162```json [settings]
163{
164 "lsp": {
165 "eslint": {
166 "settings": {
167 "workingDirectory": {
168 "mode": "auto"
169 }
170 }
171 }
172 }
173}
174```
175
176## Debugging
177
178Zed supports debugging JavaScript code out of the box.
179The following can be debugged without writing additional configuration:
180
181- Tasks from `package.json`
182- Tests written using several popular frameworks (Jest, Mocha, Vitest, Jasmine, Bun, Node)
183
184Run {#action debugger::Start} ({#kb debugger::Start}) to see a contextual list of these predefined debug tasks.
185
186> **Note:** Bun test is automatically detected when `@types/bun` is present in `package.json`.
187>
188> **Note:** Node test is automatically detected when `@types/node` is present in `package.json` (requires Node.js 20+).
189
190As for all languages, configurations from `.vscode/launch.json` are also available for debugging in Zed.
191
192If your use-case isn't covered by any of these, you can take full control by adding debug configurations to `.zed/debug.json`. See below for example configurations.
193
194### Debug the current file
195
196```json [debug]
197[
198 {
199 "adapter": "JavaScript",
200 "label": "Debug JS file",
201 "type": "node",
202 "request": "launch",
203 "program": "$ZED_FILE",
204 "skipFiles": ["<node_internals>/**"]
205 }
206]
207```
208
209This implicitly runs the current file using `node`.
210
211### Launch a web app in Chrome
212
213```json [debug]
214[
215 {
216 "adapter": "JavaScript",
217 "label": "Debug app in Chrome",
218 "type": "chrome",
219 "request": "launch",
220 "file": "$ZED_WORKTREE_ROOT/index.html",
221 "webRoot": "$ZED_WORKTREE_ROOT",
222 "console": "integratedTerminal",
223 "skipFiles": ["<node_internals>/**"]
224 }
225]
226```
227
228## See also
229
230- [Yarn documentation](./yarn.md) for a walkthrough of configuring your project to use Yarn.
231- [TypeScript documentation](./typescript.md)