Skip to content

Commit 9ed240e

Browse files
author
codesuki
committed
initial version
1 parent 7dfc43b commit 9ed240e

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# add-node-modules-path
2-
Adds the node_modules/.bin directory to the buffer exec_path. E.g. support project local eslint installations.
2+
This file provides `add-node-modules-path`, which searches
3+
the current files parent directories for the `node_modules/.bin/` directory
4+
and adds it to the buffer local `exec-path`.
5+
This allows Emacs to find project based installs of e.g. eslint.
6+
7+
## Usage
8+
`M-x add-node-modules-path`
9+
10+
To automatically run it when opening a new buffer:
11+
(Choose depending on your favorite mode.)
12+
13+
```
14+
(eval-after-load 'js-mode
15+
'(add-hook 'js-mode-hook #'add-node-modules-path))
16+
17+
(eval-after-load 'js2-mode
18+
'(add-hook 'js2-mode-hook #'add-node-modules-path))
19+
```

add-node-modules-path.el

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
;;; add-node-modules-path.el --- Add node_modules to your exec-path
2+
3+
;; Copyright (C) 2016 Neri Marschik
4+
;; This package uses the MIT License.
5+
;; See the LICENSE file.
6+
7+
;; Author: Neri Marschik <[email protected]>
8+
;; Version: 1.0
9+
;; Package-Requires: ()
10+
;; Keywords: javascript, node, node_modules, eslint
11+
;; URL: https://github.com/codesuki/add-node-modules-path
12+
13+
;;; Commentary:
14+
;;
15+
;; This file provides `add-node-modules-path', which searches
16+
;; the current files parent directories for the `node_modules/.bin/' directory
17+
;; and adds it to the buffer local `exec-path'.
18+
;; This allows Emacs to find project based installs of e.g. eslint.
19+
;;
20+
;; Usage:
21+
;; M-x add-node-modules-path
22+
;;
23+
;; To automatically run it when opening a new buffer:
24+
;; (Choose depending on your favorite mode.)
25+
;;
26+
;; (eval-after-load 'js-mode
27+
;; '(add-hook 'js-mode-hook #'add-node-modules-path))
28+
;;
29+
;; (eval-after-load 'js2-mode
30+
;; '(add-hook 'js2-mode-hook #'add-node-modules-path))
31+
32+
;;; Code:
33+
34+
;;;###autoload
35+
(defun add-node-modules-path ()
36+
(let* ((root (locate-dominating-file
37+
(or (buffer-file-name) default-directory)
38+
"node_modules"))
39+
(path (and root
40+
(expand-file-name "node_modules/.bin/" root))))
41+
(if root
42+
(progn
43+
(make-local-variable 'exec-path)
44+
(add-to-list 'exec-path path)
45+
(message "added node_modules to exec-path"))
46+
(message "node_modules not found"))))
47+
48+
(provide 'add-node-modules-path)
49+
50+
;;; add-node-modules-path.el ends here

0 commit comments

Comments
 (0)