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
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
49{
50 "languages": {
51 "JavaScript": {
52 "formatter": {
53 "code_action": "source.fixAll.eslint"
54 }
55 }
56 }
57}
58```
59
60You can also only execute a single ESLint rule when using `fixAll`:
61
62```json
63{
64 "languages": {
65 "JavaScript": {
66 "formatter": {
67 "code_action": "source.fixAll.eslint"
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
92{
93 "languages": {
94 "JavaScript": {
95 "formatter": {
96 "code_actions": {
97 "source.fixAll.eslint": true
98 }
99 }
100 }
101 }
102}
103```
104
105### Configure ESLint's `nodePath`:
106
107You can configure ESLint's `nodePath` setting:
108
109```json
110{
111 "lsp": {
112 "eslint": {
113 "settings": {
114 "nodePath": ".yarn/sdks"
115 }
116 }
117 }
118}
119```
120
121### Configure ESLint's `problems`:
122
123You can configure ESLint's `problems` setting.
124
125For example, here's how to set `problems.shortenToSingleLine`:
126
127```json
128{
129 "lsp": {
130 "eslint": {
131 "settings": {
132 "problems": {
133 "shortenToSingleLine": true
134 }
135 }
136 }
137 }
138}
139```
140
141### Configure ESLint's `rulesCustomizations`:
142
143You can configure ESLint's `rulesCustomizations` setting:
144
145```json
146{
147 "lsp": {
148 "eslint": {
149 "settings": {
150 "rulesCustomizations": [
151 // set all eslint errors/warnings to show as warnings
152 { "rule": "*", "severity": "warn" }
153 ]
154 }
155 }
156 }
157}
158```
159
160### Configure ESLint's `workingDirectory`:
161
162You can configure ESLint's `workingDirectory` setting:
163
164```json
165{
166 "lsp": {
167 "eslint": {
168 "settings": {
169 "workingDirectory": {
170 "mode": "auto"
171 }
172 }
173 }
174 }
175}
176```
177
178## Debugging
179
180Zed supports debugging JavaScript code out of the box.
181The following can be debugged without writing additional configuration:
182
183- Tasks from `package.json`
184- Tests written using several popular frameworks (Jest, Mocha, Vitest, Jasmine)
185
186Run {#action debugger::Start} ({#kb debugger::Start}) to see a contextual list of these predefined debug tasks.
187
188As for all languages, configurations from `.vscode/launch.json` are also available for debugging in Zed.
189
190If 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.
191
192### Debug the current file
193
194```json
195[
196 {
197 "adapter": "JavaScript",
198 "label": "Debug JS file",
199 "type": "node",
200 "request": "launch",
201 "program": "$ZED_FILE",
202 "skipFiles": ["<node_internals>/**"]
203 }
204]
205```
206
207This implicitly runs the current file using `node`.
208
209### Launch a web app in Chrome
210
211```json
212[
213 {
214 "adapter": "JavaScript",
215 "label": "Debug app in Chrome",
216 "type": "chrome",
217 "request": "launch",
218 "file": "$ZED_WORKTREE_ROOT/index.html",
219 "webRoot": "$ZED_WORKTREE_ROOT",
220 "console": "integratedTerminal",
221 "skipFiles": ["<node_internals>/**"]
222 }
223]
224```
225
226## See also
227
228- [Yarn documentation](./yarn.md) for a walkthrough of configuring your project to use Yarn.
229- [TypeScript documentation](./typescript.md)