#NPM vs NPX

24 messages · Page 1 of 1 (latest)

digital tree
#

Hello, I was reading a bit about the difference between npm and npx. From what I've understood, npx allows us to run a command directly without having to specify the path to the node_modules?

Is this the only advantage it provides? With npm we would have to provide the path to the node_modules files?

I also read something, if we run a particular command with npx and we can't find the package installed; is the packages installed temporarily then deleted after, like when we interupt the terminal?

tired wagon
#

npm is for installing and managing project dependencies, npx is for easily running shared cli tools with a similar set of safety guarantees as provided by npm

#

npx was created because there was a proliferation of project setup tools that asked to be installed via npm --install --global <tool>, get used once, and then it just hangs out forever in your global scope (waiting for you to use it again, 2 years later, and get tripped up on the old version you have installed)

#

so no instead of npm -ig <setup tool>; setup-tool; npm -ug <setup tool> you just do npx setup-tool

digital tree
tired wagon
#

you import most packages, tools put stuff in node_modules/.bin

#

tools are executables I use on my terminal, other packages are libraries that I import into my code

digital tree
#

when we import it's not in /node_modules/.bin?

tired wagon
#

poke around 👀

digital tree
#

yep will be back :p

digital tree
#

oh ok I didn't know that

#

now it make more sense

#

thanks !

tired wagon
#

yep yep

#

npm run puts node_modules/.bin in the path, which is why you can do a package.json "scripts" bit like "script": { "start": "vite" }. cuz vite is in node_modules/.bin/vite

#

that's where the binary files compiled to run on your computer are stored

#

"bin" -> binary

#

(even tho they're most often .sh scripts nowadays)

digital tree
#

yep I see, TIL, thanks !!

iron night
#

On windows they're made of .bats iirc

#

Very awkward you get a vite.bat and vite beside it 😆

digital tree
#

yeah, currently I have 3 scripts, for each package I guess

#

(I'm on windows :c)