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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With