jscheid / prettier.el

Prettier code formatting for Emacs.
GNU General Public License v3.0
167 stars 12 forks source link

Error: Cannot find prettier anywhere #33

Closed Lukewh closed 4 years ago

Lukewh commented 4 years ago

Describe the bug Whenever I try to run prettier on save or directly I get the above error.

Link to M-x prettier-info output Link.

which node: /snap/bin/node which prettier: /usr/local/bin/prettier npm root -g: /usr/local/lib/node_modules ls /usr/local/lib/node_modules/prettier:

bin-prettier.js  index.js  package.json       parser-babel.js  parser-glimmer.js  parser-html.js      parser-postcss.js     parser-yaml.js  standalone.js
doc.js           LICENSE   parser-angular.js  parser-flow.js   parser-graphql.js  parser-markdown.js  parser-typescript.js  README.md       third-party.js

To Reproduce

Expected behavior Prettier to find the global prettier

Additional context Add any other context about the problem here.

jscheid commented 4 years ago

Hi- Yes, as described in the installation instructions, you’re responsible for making sure that node is on exec-path, unless you’re using nvm.

I don’t think this package should contain some elaborate detection logic. Other packages (say, magit) don’t either.

Have you considered using exec-path-from-shell?

Perhaps we could improve the error message here.

jscheid commented 4 years ago

Ah, wait— it can find node, just not prettier. That’s odd. I’ll try reproducing this.

Lukewh commented 4 years ago

I thought it might be a snap confinement issue, but both Emacs and node are classically confined (access to entire system)

jscheid commented 4 years ago

Ok, so far I've found that the following command returns the empty string in your setup (ie. Ubuntu 18 with Node installed via snap -- I've been following your repro instructions to the letter):

node --eval='process.stdout.write(require("child_process").execSync("npm root -g", { encoding: "utf-8" }))'

... while the same command (npm root -g) prints the expected result when run directly in the shell, namely /usr/local/lib/node_modules.

On MacOS Catalina as well as another copy of Ubuntu 18 that I have running (but without using snap) there is no difference between running npm directly vs running it from Node.

In addition, running the following command (in a directory that doesn't have prettier installed locally) will throw Error: Cannot find module 'prettier':

node --eval='require("prettier")'

In short, it appears that installing node via snap results in a broken installation, or more precisely, one that doesn't know how to access packages installed via npm install -g.

Can you observe the same in your system?

Lukewh commented 4 years ago

Yeah, I can confirm this to be the case - I think it's out of scope of this project to fix so closing this issue :)! Thanks for your help! Sorry for the noise.

Lukewh commented 4 years ago

It's probably worth noting that simply setting (setenv "NODE_PATH" "/usr/local/lib/node_modules") at the beginning of my init file remedied the situation :+1:

jscheid commented 4 years ago

No worries... ah, good to know. I might start a little "troubleshooting" section with it.

Lukewh commented 4 years ago

I guess one suggestion, after thinking about it, would be to make the error more descriptive with maybe a link to the troubleshooting section you mentioned? Just a thought 😊. Thanks for the hard work on the package, either way!