I have my tab width set to two spaces and this has worked fine for many years. I have just upgraded to Emacs 27.1, and now js-mode is displaying tabs as three spaces wide, ignoring my preferred settings (which still apply to other modes, it's only js-mode that isn't respecting my preference.)

I can't work out how to make it go back to 2. The tab width is still set to 2 in my init.el file, which hasn't changed during the Emacs upgrade.

If I press M-x and run set-variable then set tab-width to 2, the file goes back to the correct tab width. But I can't work out how to make it apply to all files. If I press ? instead of 2 when setting tab-width then it tells me it is already set to 2 (which it is, in my init.el) however it seems I have to set it to 2 again, even though it's ready 2, for every .js file that I open.

How can I make it so that js-mode respects the tab widths I have set in init.el, as it did in earlier Emacs versions?

I've looked through all the options but I can only see two that are related to the tab width and they are already set to 2.

Is anyone else running Emacs 27.1 or newer? Have you successfully changed the tab width for .js files?

  • 1
    I had a similar problem, except that I have my settings to 4, but it kept getting reset to 2. I used debug-on-variable-change on tab-width and found that it was invariably being set to 2 by js-mode because of git.savannah.gnu.org/cgit/emacs.git/tree/lisp/progmodes/…. Can you do the same and see what is changing it when you open a JS file? I ended up setting c-default-style to user, which for me defaults to 4.
    – Matt
    Oct 24, 2020 at 7:46
  • @Matt: Thanks for the suggestion! Looks like the same issue although for me it appears to be trying to set it to 4, then somehow ending up as 3 anyway. My c-default-style is already set to my own custom one but strangely if I use M-x set-variable c-default-style it won't accept anything, including my custom one in init.el or user...
    – Malvineous
    Oct 29, 2020 at 16:31

3 Answers 3


This looks to me like an issue with emacs, but I have not yet nailed it down. At any rate, it looks like the problem is the code in http://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/progmodes/js.el?h=emacs-27.1#n4614 is running after all the other traditional packages (like editorconfig, etc) that I have used to set this value properly for different projects.

My current workaround is setting the following in my .emacs ..

(setq-default c-default-style '((js-mode . "user")
                                (java-mode . "java")
                                (awk-mode . "awk")
                                (other . "gnu")))

But this is a pure workaround (btw user just sets indent-size to 4). You would have to use another setting if you want something other than 4. It's not great because it is not project aware. Looking for a better solution in the meantime.


I was able to solve this by adding the following to my init.el file:

(add-hook 'js-mode-hook
  (function (lambda ()
    (setq indent-tabs-mode t
          tab-width 2))))

Seems it works by overriding the faulty default indent value every time you open a JS file.


js-mode's js-indent-level defaults to four spaces ( https://github.com/emacs-mirror/emacs/blob/5e5f5b28e92a3bb927bb03c783c426c6c3baeadc/lisp/progmodes/js.el#L434 ).

Adding the following to init.el sets tabs to two spaces in js-mode in emacs 30.0.50.

;; set js-mode indent level
(add-hook 'js-mode-hook
  (lambda ()
    (setq js-indent-level 2)))
  • 1
    Use just (lambda...), not (function (lambda...)) in Elisp.
    – Drew
    Apr 7, 2023 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.