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 "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
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
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
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
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
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
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)
183
184Run {#action debugger::Start} ({#kb debugger::Start}) to see a contextual list of these predefined debug tasks.
185
186As for all languages, configurations from `.vscode/launch.json` are also available for debugging in Zed.
187
188If 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.
189
190### Debug the current file
191
192```json
193[
194 {
195 "adapter": "JavaScript",
196 "label": "Debug JS file",
197 "type": "node",
198 "request": "launch",
199 "program": "$ZED_FILE",
200 "skipFiles": ["<node_internals>/**"]
201 }
202]
203```
204
205This implicitly runs the current file using `node`.
206
207### Launch a web app in Chrome
208
209```json
210[
211 {
212 "adapter": "JavaScript",
213 "label": "Debug app in Chrome",
214 "type": "chrome",
215 "request": "launch",
216 "file": "$ZED_WORKTREE_ROOT/index.html",
217 "webRoot": "$ZED_WORKTREE_ROOT",
218 "console": "integratedTerminal",
219 "skipFiles": ["<node_internals>/**"]
220 }
221]
222```
223
224## See also
225
226- [Yarn documentation](./yarn.md) for a walkthrough of configuring your project to use Yarn.
227- [TypeScript documentation](./typescript.md)