Search code examples
emacsprettier

require in .emacs config file not working


I have installed prettier for emacs using the instructions here: https://github.com/prettier/prettier-emacs. Doing which prettier gives me the location of prettier. Then I put

(require 'prettier-js)

and

(add-hook 'js2-mode-hook 'prettier-js-mode)
(add-hook 'web-mode-hook 'prettier-js-mode)

in my ~/.emacs file. I also put prettier-js.el in my ~/ directory. Then I restart emacs and try the command

M-x customize-group prettier-js

but prettier-js is not a customize-group option, suggesting that it is not installed. How can I install prettier-js?


Solution

  • in my ~/.emacs file. I also put prettier-js.el in my ~/ directory.

    This is most likely your problem. With the exception of .emacs, your home directory is not an appropriate place to put elisp libraries. By default Emacs will not look for them there, and you shouldn't tell it that it's safe to look for them there either.

    Make a new directory under your ~/.emacs.d/ directory for elisp libraries. I suggest: ~/.emacs.d/lisp

    Put prettier-js.el in there instead: ~/.emacs.d/lisp/prettier-js.el

    Then in your init file add this line ahead of the require:

    (add-to-list 'load-path (expand-file-name "~/.emacs.d/lisp"))
    (require 'prettier-js)
    

    Now Emacs will be able to find it (and any other libraries you put in that directory).

    You should also byte-compile the .el file using M-x byte-compile-file