0

I was trying to get emacs to point to a version of eslint that isn't installed globally (but located in the node_modules of the directory one level up), so I followed this: Flycheck with file relative eslint executable, but flycheck is still only finding the global version (at "/usr/local/bin/eslint"). Below are the lines I added to my init.el related to flycheck.

;; configure flycheck for linting, make sure it starts globally                                                                                                                                         
(add-hook 'after-init-hook #'global-flycheck-mode)                                                                                                                                                      

;; the following didn't work                                                                                                                                                                            
;; I had to install eslint globally to get it to work and it wasnt ideal :(                                                                                                                             
(defun my/use-eslint-from-node-modules ()                                                                                                                                                               
  (let* ((root (locate-dominating-file                                                                                                                                                                  
                (or (buffer-file-name) default-directory)                                                                                                                                               
                "node_modules"))                                                                                                                                                                        
         (eslint (and root                                                                                                                                                                              
                      (expand-file-name "node_modules/eslint/bin/eslint.js"                                                                                                                             
                                        root))))                                                                                                                                                        
    (when (and eslint (file-executable-p eslint))                                                                                                                                                       
      (setq-local flycheck-javascript-eslint-executable eslint))))                                                                                                                                      
(add-hook 'flycheck-mode-hook #'my/use-eslint-from-node-modules)

I am using Mac OSX Mojave with Emacs 26.1.1

Drew
  • 75,699
  • 9
  • 109
  • 225

1 Answers1

1

My guess is that you are starting up emacs by clicking on an icon in the dock or from spotlight. In that case I suspect you're environment is not what you're expecting. Look in your *Messages* file and see if you have any errors looking for node. I had this in mine:

File mode specification error: (file-missing Searching for program No such file or directory node)

You can see what path emacs is using: C-h v load-path RET. Then open up a terminal and do which node and see if you find node's path in the list emacs provides.

If you have node on your path in terminal, you can open up emacs from there and it will use that environment:

open -a /usr/local/bin/emacs

You can confirm whether or not flycheck is working by opening up a JavaScript file then M-x flycheck-verify-setup and it will show you the checkers that it has enabled.

If none of that works, make sure you have flycheck installed, and verify the code that you are using to configure it. I've seen many variations. Here is what I'm currently using for use with React:

(defun configure-web-mode-flycheck-checkers ()
  ;; in order to have flycheck enabled in web-mode, add an entry to this
  ;; cond that matches the web-mode engine/content-type/etc and returns the
  ;; appropriate checker.
  (-when-let (checker (cond
                       ((string= web-mode-content-type "jsx")
                        'javascript-eslint)))
    (flycheck-mode)
    ;; use the locally installed eslint
    (let* ((root (locate-dominating-file
                  (or (buffer-file-name) default-directory)
                  "node_modules"))
           (eslint (and root
                        (expand-file-name "node_modules/.bin/eslint"
                                          root))))
      (when (and eslint (file-executable-p eslint))
        (setq-local flycheck-javascript-eslint-executable eslint)))

    (flycheck-select-checker checker)))

(add-hook 'web-mode-hook #'configure-web-mode-flycheck-checkers)
Ricky Nelson
  • 121
  • 4