Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prettier command not working even though prettier is in devDependancies

In my package.json


 "devDependencies": {
   ...
   "prettier": "^2.8.4",
   ...
}

I run npm i.

Then:

$ prettier 
bash: prettier: command not found
$ npx prettier -v

/var/folders/q6/npwl_7xj4wg91lg06f8pnnfh0000gn/T/npx-b98a4fce.sh: line 1: prettier: command not found

Not sure if this- /var/folders/q6/npwl_7xj4wg91lg06f8pnnfh0000gn/T/npx-b98a4fce.sh- is normal.

I am using nvm, using node v16.17.1 and npm v8.15.0

Any ideas why this is happening or how to debug it?

EDIT: prettier worked after I npm install -g prettier to install prettier globally. But shouldn't it work without this?

like image 913
Dashiell Rose Bark-Huss Avatar asked Jun 08 '26 14:06

Dashiell Rose Bark-Huss


1 Answers

Installing a module that includes a CLI program like prettier, nodemon, etc., will place the program in node_modules/.bin. Your terminal shell does not know to look in this folder when you run a command. Type echo $PATH in your terminal to see a list of the folders that are checked when you run a command. You'll see that running prettier will be looking in places like /bin, /usr/bin, /usr/local/bin, etc., but definitely not /var/folders/q6/npwl_7xj4wg91lg06f8pnnfh0000gn/T/node_modules.

This is why npx is often used; from their docs:

Executes either from a local node_modules/.bin, or from a central cache, installing any packages needed in order for to run.

You could also run it via ./node_modules/.bin/prettier, or you could install it globally, as you ultimately did, which will put it in a place that is part of your $PATH.

like image 115
Charlie Schliesser Avatar answered Jun 11 '26 03:06

Charlie Schliesser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!