codesuki / add-node-modules-path Goto Github PK
View Code? Open in Web Editor NEWAdds the node_modules/.bin directory to the buffer exec_path. E.g. support project local eslint installations.
License: MIT License
Adds the node_modules/.bin directory to the buffer exec_path. E.g. support project local eslint installations.
License: MIT License
Hello. I notice the npm bin
command will be removed in [email protected] when reading the release note of [email protected].
This elisp package heavily depends on npm bin
, so I worry the package will require some action in the future. Although, I have no good idea right now.
I hope this sharing will be of some help.
See also:
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.
When using the develop branch of spacemacs, this function is no longer provided. Will try and work out what the issue is.
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 ?
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
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?
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)
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.
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
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.
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.
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
Version: 20230307.655 which is the latest in melpa.
Errors with add-node-modules-path: Symbol’s function definition is void: length>
A quick google search suggests that lsp-mode ran into the same issue on emacs27 as well.
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.
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?
Any plans for a 1.3.0 release?
It would be nice to get #9
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.