Open a .tsx file in a stock Emacs 29 and there is a fair chance it lands in fundamental-mode, or in a mode that colours the JSX as if it were a syntax error. JavaScript support in Emacs went through several generations of packages (js2-mode, rjsx-mode, typescript-mode, web-mode for JSX), and older guides mix all of them. The current answer is simpler than any of those guides: built-in tree-sitter modes, one language server, one formatter.
Modes and grammars
Emacs 29.1 added four relevant modes: js-ts-mode for JavaScript (JSX included), typescript-ts-mode for .ts, tsx-ts-mode for .tsx, and json-ts-mode. The older js-mode is still there and still fine, but the tree-sitter modes highlight modern syntax more reliably.
They need grammars. Tell Emacs where they live, then install each once:
(setq treesit-language-source-alist
'((javascript "https://github.com/tree-sitter/tree-sitter-javascript")
(typescript "https://github.com/tree-sitter/tree-sitter-typescript"
"master" "typescript/src")
(tsx "https://github.com/tree-sitter/tree-sitter-typescript"
"master" "tsx/src")
(json "https://github.com/tree-sitter/tree-sitter-json")))
Then M-x treesit-install-language-grammar for javascript, typescript, tsx and json. The TypeScript repository holds two grammars in subdirectories, which is why those entries carry the extra arguments. If highlighting breaks oddly after a grammar update on an older Emacs, the grammar has probably moved ahead of what the mode expects; pinning an older tag of the grammar in place of "master" is the usual fix.
Emacs does not always route the file extensions to these modes on its own, so the config below does it explicitly.
The server: typescript-language-server
One server covers both languages. Install it with its peer dependency:
npm install -g typescript-language-server typescript
It wraps the TypeScript compiler’s language service, the same engine VS Code uses, so completion, go-to-definition, references, rename and type errors behave the way your colleagues on other editors see them. For JavaScript projects it reads jsconfig.json; for TypeScript, tsconfig.json. A project without either still works, file by file, with less cross-file knowledge.
eglot, built into Emacs since 29.1, already knows that these modes use typescript-language-server --stdio. Hooking the modes is all it takes:
(use-package js
:mode ("\\.[cm]?jsx?\\'" . js-ts-mode)
:hook (js-ts-mode . eglot-ensure))
(use-package typescript-ts-mode
:mode (("\\.[cm]?ts\\'" . typescript-ts-mode)
("\\.tsx\\'" . tsx-ts-mode))
:hook ((typescript-ts-mode tsx-ts-mode) . eglot-ensure))
js is the library that defines both js-mode and js-ts-mode, which is why the first block uses that name. Completion candidates go through completion-at-point; the completion page shows how to put a popup on top.
A word on monorepos: eglot starts one server per project, and project.el usually takes the Git root as the project. With several packages each carrying its own tsconfig.json, the server still resolves them, because TypeScript looks for the nearest config file for each source file.
ESLint in the same diagnostics list
You want ESLint’s warnings next to the type errors, in one Flymake list. The flymake-eslint package on MELPA runs the eslint binary and reports through Flymake. There is one trap: eglot takes over Flymake in the buffers it manages and drops other backends. Enable flymake-eslint after eglot has started, from its own hook:
(use-package flymake-eslint
:ensure t
:hook (eglot-managed-mode
. (lambda ()
(when (derived-mode-p 'js-ts-mode 'typescript-ts-mode 'tsx-ts-mode)
(flymake-eslint-enable)))))
flymake-eslint calls whatever eslint it finds on the PATH. Most projects install ESLint locally, so either add node_modules/.bin to your path per project through .dir-locals.el, or set flymake-eslint-executable-name to the local binary.
The alternative is the ESLint language server from the vscode-langservers-extracted package. It is good, but eglot runs one server per buffer, so you would lose the TypeScript server in exchange. The Flymake route keeps both.
Prettier through apheleia
Prettier formats JavaScript, TypeScript, JSON, CSS and Markdown, and nearly every front-end project has a .prettierrc. The cleanest way to run it from Emacs is apheleia, by the Radian project. It formats asynchronously after save and then patches the buffer with a minimal diff, so point does not jump to the top of the file and undo history stays usable.
(use-package apheleia
:ensure t
:config
(apheleia-global-mode +1))
apheleia ships formatter definitions for Prettier and maps the JavaScript and TypeScript modes to them, preferring the project’s own Prettier from node_modules when it is installed. If a buffer does not get formatted, look at its entry in apheleia-mode-alist. Because the typescript-language-server can also format, avoid adding eglot-format-buffer to the save hook as well, or the two will disagree about quotes and trailing commas on every save.
A Node REPL
Sometimes you just want to evaluate an expression. Three options, from lightest to most integrated:
- M-x
comint-runwithnodegives an interactive Node session inside Emacs with history and no package at all. If the output fills with escape codes, setNODE_NO_READLINE=1in Emacs’s environment so Node stops trying to edit lines itself. - js-comint (MELPA) adds
js-comint-replplus commands to send the region, the last expression or the whole buffer to it. - nodejs-repl (MELPA) does much the same with its own set of send commands; pick whichever key layout you like better.
For TypeScript there is no clean equivalent. npx tsx gives a REPL that understands TypeScript syntax and runs fine under comint-run.
Next steps
The package guide covers MELPA and use-package basics if the :ensure t lines are new to you. The HTML and CSS guide reuses Prettier and adds the HTML and CSS servers from the same npm package mentioned above, and the PHP page is the companion if your front end sits inside a Laravel app. The languages overview lists everything else, and the one thing worth doing right after this page is opening a project with a broken import on purpose to see the server and ESLint report it side by side.
Questions readers ask
Does typescript-language-server work for plain JavaScript files?
Yes. It wraps the TypeScript compiler's own language service, which understands JavaScript too, including JSDoc type annotations. A jsconfig.json at the project root tells it which files belong together, the same way tsconfig.json does for TypeScript.
Why do my ESLint warnings disappear when eglot starts?
eglot replaces the Flymake backends of the buffers it manages with its own, so a backend you added earlier is dropped. Enable flymake-eslint from eglot-managed-mode-hook, as in the snippet on this page, and both sets of diagnostics appear together.
Can I use the old js2-mode or rjsx-mode instead?
They still install from MELPA, but js2-mode's own parser lags behind current JavaScript syntax and duplicates work a language server does better. On Emacs 29 or newer the built-in tree-sitter modes are the cleaner base.