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

2 Answers 2

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)
0

Add the following to .dir-locals.el at the root of your project directory:

((nil .
      ((eval .
             (add-to-list 'exec-path
                          (concat (locate-dominating-file default-directory dir-locals-file)
                                  "node_modules/.bin/"))))))

Alternatively, instead of solving this within Emacs, you can solve it by creating an eslint script that redirect to the local eslint. Write the following to ~/.local/bin/eslint or any other locations in $PATH:

#!/bin/bash

exec npx eslint "$@"

Make sure the script is executable: chmod +x ~/.local/bin/eslint

Restart Emacs. Now flycheck should be using the local eslint.

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.