I enabled js2-mode in emacs by adding this snippet in .emacs,

(add-to-list 'auto-mode-alist '("\\.js$'" . js2-mode))

But still for .js files, emacs tries to load javascript-mode alias js-mode as the major mode. And god knows, how did javascript-mode function go missing in my emacs. It crashes like this,

Debugger entered--Lisp error: (error "Autoloading failed to define function javascript-mode")
  set-auto-mode-0(javascript-mode nil)
  after-find-file(nil t)
  find-file-noselect-1(#<buffer a.js> "~/a.js" nil nil "~/a.js" (1840059 2049))
  find-file-noselect("/home/sk/a.js" nil nil)
  call-interactively(ido-find-file nil nil)

This leads to another question, does emacs consider the last (or) first regex entry in auto-mode-alist for loading the major mode?

  • What is your Emacs version?
    – user227
    Commented Feb 15, 2015 at 10:40
  • emacs version - 24.3.1
    – Saravana
    Commented Feb 15, 2015 at 11:20
  • Well, try to re-install Emacs. If a built-in package is gone, there's something wrong.
    – user227
    Commented Feb 15, 2015 at 11:26

1 Answer 1


There is a trailing quotation mark in your regular expression, so it will never match files with the .js extension. Use the following regular expression instead:

(add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))

Note the absence of the dollar—which matches the end of a line rather than of the string—and the backslash before the quotation mark: \\' properly matches the end of a string.

I'd recommend you to use the rx macro for regular expressions, which makes them more readable:

(add-to-list 'auto-mode-alist `(,(rx ".js" string-end) . js2-mode))

Furthermore, I'd recommend you to reinstall Emacs completely. If a built-in package like javascript-mode is gone, there might be something fundamentally broken in your installation.

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.