Coder Social home page Coder Social logo

codesuki / add-node-modules-path Goto Github PK

View Code? Open in Web Editor NEW
143.0 143.0 17.0 14 KB

Adds the node_modules/.bin directory to the buffer exec_path. E.g. support project local eslint installations.

License: MIT License

Emacs Lisp 100.00%
emacs exec-path node node-modules path

add-node-modules-path's People

Contributors

0x1966 avatar benprew avatar codesuki avatar cowboyd avatar critocrito avatar kayhide avatar tejasbubane avatar wyuenho avatar ybiquitous avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

add-node-modules-path's Issues

Latest commit adds `s.el` dependency (`s-chomp`)

After upgrading add-node-modules-path to the latest repo commit (i.e. #16), Emacs 27.2 now throws an error: File mode specification error: (void-function s-chomp) when opening a file inside a project with a node_modules directory.

The error appears to stem from this line which was added in the latest commit — s-chomp comes from s.el which is not yet bundled with Emacs.

Is this method insecure and, if yes, can we use `npm bin` ?

I have seen that this method being allegedly insecure is a concern in Spacemacs, where they give a link to this Stackoverflow thread.

An alternative, more secure solution would be to use npm bin to find the node_modules.

It would be great if your package could be used by default in Spacemacs, but I fear that this security issue would put off the dev team to do so until it is addressed or debunked.

What do you think about the security issue and the proposed alternative ?

wrong type of argument

Hi,
I've recently started having issues when trying to open js files without node_modules. My config has a hook for when a js file is opened. Here is the debug output from emacs:

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  expand-file-name(nil)
  add-node-modules-path()
  run-hooks(change-major-mode-after-body-hook prog-mode-hook js-mode-hook js2-mode-hook js2-jsx-mode-hook rjsx-mode-hook)
  apply(run-hooks (change-major-mode-after-body-hook prog-mode-hook js-mode-hook js2-mode-hook js2-jsx-mode-hook rjsx-mode-hook))
  run-mode-hooks(rjsx-mode-hook)
  rjsx-mode()
  set-auto-mode-0(rjsx-mode nil)
  set-auto-mode()
  normal-mode(t)
  after-find-file(nil t)

I'm removing from config and just manually running if needed for now. Ideally would like to turn this feature back on

Requires global npm to work?

Hey thanks for this! In setting this up I noticed an issue however where this requires a global npm to work. When installing npm exclusively through an nvm environment and running this in debug mode I was getting

Failed to run ‘npm bin’:
 /bin/bash: line 1: npm: command not found

Is there any way to make this work when using nvm exclusively?

Emacs hangs when I use tide hotkey 'jump to definition' and when local node_modules directory doesn't exist at the same time

Got it on Windows, didn't checked it out yet on Linux.

Emacs hangs (C-g doesn't help) when I use tide hotkey M-. (jump to definition) and that definition has to be placed in node_modules, but I haven't yet installed packages with npm install, so my node_modules directory doesn't exist in the project.

Example with the code:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './component/App';

ReactDOM.render(
    <App />,
    document.getElementById('root')
);

Put the point at the ReactDOM and type M-.. Emacs hangs.

If I remove this from my init.el then Emacs doesn't freeze and then brings me to the global node_modules path with react package installed:

;; add-node-modules-path
(eval-after-load 'web-mode
  '(add-hook 'web-mode-hook #'add-node-modules-path))

(eval-after-load 'typescript-mode
  '(add-hook 'typescript-mode-hook #'add-node-modules-path))

Full init.el:

;; list the packages you want
(setq package-list '(add-node-modules-path company js2-mode tide web-mode))

;; list the repositories containing them
(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
                         ("marmalade" . "https://marmalade-repo.org/packages/")
                         ("melpa" . "https://melpa.org/packages/")))

;; activate all the packages (in particular autoloads)
(package-initialize)

;; fetch the list of packages available 
(unless package-archive-contents
  (package-refresh-contents))

;; install the missing packages
(dolist (package package-list)
  (unless (package-installed-p package)
    (package-install package)))


;; add-node-modules-path
(eval-after-load 'web-mode
  '(add-hook 'web-mode-hook #'add-node-modules-path))

(eval-after-load 'typescript-mode
  '(add-hook 'typescript-mode-hook #'add-node-modules-path))


;; tide
(defun setup-tide-mode ()
  (interactive)
  (tide-setup)
  (flycheck-mode +1)
  (setq flycheck-check-syntax-automatically '(save mode-enabled))
  (eldoc-mode +1)
  (tide-hl-identifier-mode +1)
  ;; company is an optional dependency. You have to
  ;; install it separately via package-install
  ;; `M-x package-install [ret] company`
  (company-mode +1))

;; aligns annotation to the right hand side
(setq company-tooltip-align-annotations t)

;; formats the buffer before saving
(add-hook 'before-save-hook 'tide-format-before-save)
(add-hook 'typescript-mode-hook #'setup-tide-mode)

;; tsx
(require 'web-mode)
(require 'flycheck)
(add-to-list 'auto-mode-alist '("\\.tsx\\'" . web-mode))
(add-hook 'web-mode-hook
          (lambda ()
            (when (string-equal "tsx" (file-name-extension buffer-file-name))
              (setup-tide-mode))))
;; enable typescript-tslint checker
(flycheck-add-mode 'typescript-tslint 'web-mode)


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

(add-hook 'js2-mode-hook #'setup-tide-mode)
;; configure javascript-tide checker to run after your default javascript checker
(flycheck-add-next-checker 'javascript-eslint 'javascript-tide 'append)

;; jsx
(add-to-list 'auto-mode-alist '("\\.jsx\\'" . web-mode))
(add-hook 'web-mode-hook
          (lambda ()
            (when (string-equal "jsx" (file-name-extension buffer-file-name))
              (setup-tide-mode))))
;; configure jsx-tide checker to run after your default jsx checker
(flycheck-add-mode 'javascript-eslint 'web-mode)
(flycheck-add-next-checker 'javascript-eslint 'jsx-tide 'append)

Doesn't work with hoisted dependencies in monorepos

In monorepos, especially managed by lerna or pnpm, npm bin doesn't return the correct binary directory, since a binary can be either hoisted to the root directory of the monorepo or be located in the package's node_modules. Solving this would probably require adding both the monorepo's node_modules/.bin as well package's node_modules/.bin to the path.

emacs hang when open files in docker container's mounted dir

Hi

I used emacs 25.3.1 under Fedora 28 docker container.

My codebase path was mounted in docker container:

docker run --rm -it --net=host -v /home/py:/home/py --name fedora fedora

When opening .ts files, emacs hang.

There was M-x profiler-report output when C-x C-f (counsel-find-file) hang :

Function                                                  CPU samples    %
- command-execute                                               21743  92%
 - call-interactively                                           21743  92%
  - funcall-interactively                                       21743  92%
   - counsel-find-file                                          21556  91%
    - ivy-read                                                  21556  91%
     - apply                                                    21556  91%
      - ivy-historian--nadvice/ivy-read                         21556  91%
       - apply                                                  21556  91%
        - #<compiled 0x1b571d9>                                 21556  91%
         - ivy-call                                             21405  90%
          - counsel-find-file-action                            21405  90%
           - find-file                                          21405  90%
            - find-file-noselect                                21405  90%
             - find-file-noselect-1                             21404  90%
              - after-find-file                                 21404  90%
               - normal-mode                                    21404  90%
                - set-auto-mode                                 21402  90%
                 - set-auto-mode-0                              21402  90%
                  - typescript-mode                             21390  90%
                   - run-mode-hooks                             21081  89%
                    - apply                                     21081  89%
                     - run-hooks                                21081  89%
                      - add-node-modules-path                   20702  87%
                       - expand-file-name                        4824  20%
                        - tramp-completion-file-name-handler     4565  19%
                         - tramp-completion-run-real-handler     4023  17%
                            apply                                3961  16%
                       + file-name-directory                     1707   7%
                       + directory-file-name                     1685   7%
                      + paredit-everywhere-mode                    29   0%
                      + origami-mode                               24   0%
                      + symbol-overlay-mode                         2   0%
                      + rainbow-delimiters-mode                     2   0%
                   + byte-code                                    305   1%
                + fundamental-mode                                  1   0%
             + file-truename                                        1   0%
         + read-from-minibuffer                                   150   0%
         + ivy--reset-state                                         1   0%
   + counsel-M-x                                                  187   0%
+ ...                                                            1863   7%
+ redisplay_internal (C function)                                  24   0%
  global-whitespace-cleanup-mode-check-buffers                      1   0%
+ timer-event-handler                                               1   0%

I have comment add-node-modules-path config for .ts files, emacs not hang.

# git diff
diff --git a/lisp/init-javascript.el b/lisp/init-javascript.el
index f4871aa..e52bbe0 100644
--- a/lisp/init-javascript.el
+++ b/lisp/init-javascript.el
@@ -103,8 +103,8 @@
 
 ^L
 (when (maybe-require-package 'add-node-modules-path)
-  (after-load 'typescript-mode
-    (add-hook 'typescript-mode-hook 'add-node-modules-path))
+  ;; (after-load 'typescript-mode
+  ;;   (add-hook 'typescript-mode-hook 'add-node-modules-path))
   (after-load 'js2-mode
     (add-hook 'js2-mode-hook 'add-node-modules-path)))

There was more details about this issue in purcell/emacs.d#580

Can you add support for Windows?

Based on my attempt to run emacs with the same configuration on Linux and Windows I assume it doesn't support Windows, so can I ask you to add that feature?

Actually it's working only in one case. For example, if the file is a descendant of ~ (on my Windows machine it's a C:\Users\<SomeUserName>\AppData\Roaming). Otherwise emacs is hanging, C-g helps to break that freezing.

As a workaround if you want to work with code that located at another place you can create in the HOME directory (~) the directory junction to that code.

Release 1.0.0 to melpa stable

This is a great package, and seems stable enough (we've been using it for several months without issue :)

All that would be required is to push a v1.0.0 tag.

Thanks

Just wanted to say thanks - I was about to write similar code, but then I checked melpa and saw your package. :)

It fixes flycheck/flycheck#1087

Recursive invocation

Would you accept a PR to make the function recursive? Particularly in monorepo projects I sometimes have multiple directory levels with node_modules and want every .bin on my path.

Installation using use-package

Adding the package with use-package:

(use-package add-node-modules-path :ensure t)

For some reason I don't see the function exposed when running M-x add-node-modules-path.

Any idea what am I missing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.