I Thought I Knew npm… Until I Met npx: Intro to npx

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    I Thought I Knew npm… Until I Met npx: Intro to npx

    Hey everyone, today I will be showing you some basics about npx.

    If you have been developing for a while, you should have heard or used this before atleast once.


    I recently had to create an npm package of my own. Along with that it also supports npx.


    So first, lets know what is the difference between npx and npm.

    npx vs npm: How are they different

    npx and npm: They sound similar, and they DO come from the Node.js environment.


    However, npm and npx solve two different problems.

    npm (Node package manager)

    • Used for installing, managing and updating packages
    • These packages gets added to your project or installed globally.
    • You can run scripts or binaries that are already installed.



    npm install
    npm install -g typescript





    npx (Node Package eXecute)

    • npx is used to run a package without installing it permanently
    • Running one-off commands quickly without polluting your global installs



    npx create-vite@latest my-app





    How npx works

    Suppose you enter a command to use a tool called package-name






    npx package-name do-something







    If package name is not installed, then npx will prompt you to install it. After installing it you will be instantly redirected to the actual do-something task.


    So it's quite handy if you want to minimize friction when introducing your creation to new users. If the installation process is very simple, then more people can easily onboard and adapt it.


    Does npx throw away the package?

    • The installation is temporary
    • It does not permanently install anything to your global or local environment.
    • It uses lightweight cache for repeated runs to be faster.


    Wrapping up

    Hope you now understood npx, and how its quite handy.


    If you are a developer constantly learning new things and also figuring out ways to upskill+be more efficient, I have a resource for you


    It’s free, open-source, and built with developers in mind.





    👉 Explore the tools: FreeDevTools


    👉 Star the repo: freedevtools




    More...
Working...