npm Vs npx

Introduction

We all are using npm as our package manager. It is easy, right? But with the version [email protected], when you install the npm, it installs a new package called npx. Have you ever thought about what it is? And why it is needed? Are there any benefits of using npx instead of npm? I know you were trying to find the answer for the above questions and at the end, you landed up on this page. I will try to answer these questions for you. And at the end of the article, I am sure that you will get the answer to your questions. I hope you will find this post useful. 

Background

I was trying to create a React application and as you know, we can create a React application very easily with the help of the react-app command. But when I visited the official Github documentation of the created React app, it was mentioned there that we can either use npx or npm. And I was just wondering what is the difference. And this question turned into this article.

What we are going to do

We will be creating two applications and installing the mocha package in both applications so that we can run some tests. If you don’t know what mocha is, visiting the official website wouldn’t be a bad idea.

  1. mocha-example-npm
  2. mocha-example-npx
npm-npx
npm-npx
 
Coding with mocha-example-npm application

Let’s try to create a new folder named mocha-example-npm and open it in my favorite code editor, which is vscode. Once you open the folder, you can install mocha by running this command.

  1. npm install mocha
    Let’s create a folder called test.

  2. mkdir test
    Let’s create a file called test.js.

    code test/test.js

And paste the below sample test in it.

  1. var assert = require('assert');  
  2. describe('Array'function() {  
  3.     describe('#indexOf()'function() {  
  4.         it('should return -1 when the value is not present'function() {  
  5.             assert.equal([1, 2, 3].indexOf(4), -1);  
  6.         });  
  7.     });  
  8. });  

By seeing the code, you might have already found that the test will pass the indexOf (4) as -1. Now how do we check this? So to run the test we need to run mocha right? Let’s do it.

If you just type mocha in the terminal, you will get an error as below.

  1. mocha - The term 'mocha' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    1. At line:1 char:1  
    2. + mocha  
    3. + ~~~~~  
    4. + CategoryInfo : ObjectNotFound: (mocha:String)[], CommandNotFoundException  
    5. + FullyQualifiedErrorId : CommandNotFoundException  

It is saying that mocha is not a recognized command. To fix this you have three options below.

  • Install mocha globally, so that the command will be recognized globally
    1. [email protected] C:\Program Files(x86)\nodejs\node_modules\npm  
    2. PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm> npm i mocha -g  
    3. C:\Users\Sibeesh\AppData\Roaming\npm\mocha -> C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\mocha  
    4. C:\Users\Sibeesh\AppData\Roaming\npm\_mocha -> C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\_mocha  
    5. [email protected]  
    6. added 24 packages from 436 contributors in 3.458s  
    7. PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm> npm list -g mocha  
    8. C:\Users\Sibeesh\AppData\Roaming\npm  
    9. `-- [email protected]  
    10. PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm> mocha  
    11. Array  
    12. #indexOf()  
    13. √ should return -1 when the value is not present  
    14. 1passing(14ms)  
    15. PS F:\SibeeshPassion\Articles\NPMvsNPX\npm\mocha-example-npm>  
  • Use the path to mocha from node_modules

    ./node_modules/mocha/bin/mocha

  • Configure test in your package.json file as follows.
    1. "scripts":{  
    2.    "test":"mocha"  
    3. }  
    So that you can run npm test command, and it will take the mocha files from your node_modules folder.
Coding with mocha-example-npx application

So, we have understood the problem we have using npm and we have three solutions to fix it. Now, let’s try using the npx instead of npm. Let’s open the folder and run npx mocha. The result is a warning as it is trying to find the folder test in the solution and then the tests.

  1. npx: installed 24 in 6.203s
  2. Warning: Could not find any test files matching pattern: test
  3. No test files found

Let’s create a test folder, test.js file and write a test in it.

  1. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx> mkdir test  
  2. Directory: F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx  
  3. Mode LastWriteTime Length Name  
  4. ---- ------------- ------ ----  
  5. d----- 20-10-201806:26 PM test  
  6. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx> code test/test.js  
  7. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx>  

Let’s run the command npx mocha now, and you will get the output as below.

  1. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx> npx mocha  
  2. npx: installed 24 in 4.142s  
  3. Array  
  4. #indexOf()  
  5. √ should return -1 when the value is not present  
  6. 1passing(12ms)  
  7. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx>  

As you can see that our tests are passed. But are you seeing any node_modules folder or any packages are being added to your solution?

What is npx?

Now, let’s just summarize what npx is. It is an npm package runner. We all know that we are installing the packages to our solution from the location called npm registry and using it. And the npx has been designed in a way that we can directly use the package from the npm registry without even installing it in our solution. You may be thinking why is it needed?

Let me give you an example, as I mentioned in the background section of this article, we can create a simple React application with the help of Create-React-App from FaceBook. And this is used only for creating the application, and once we create the application, we no longer need this. If you use npm, you will have to install this package either globally or locally before you use this, or else you will get an error.

Wouldn't it be good, if we could just use this create-react-app package from the npm registry and create the application in our local machine? And that’s where npx comes in. With the help of npx, we can reduce the size of our node_modules and I strongly believe that this is a huge benefit.

It is smart enough to identify whether the package you are trying to get from the npm registry is already there in your machine, either globally or locally. If the package is available in your machine, it will take it from there.

What if mocha is installed globally or locally and we run the command npx mocha?

Let’s try to install mocha globally and run the command npx mocha. Before that, let’s just make sure that we haven’t installed mocha yet.

  1. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx> npm list mocha  
  2. [email protected] F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx  
  3. `-- (empty)  
  4. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx> npm list -g mocha  
  5. C:\Users\Sibeesh\AppData\Roaming\npm  
  6. `-- (empty)  
  7. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx>  

Now, let’s just install mocha globally

  1. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx> npm i mocha -g  
  2. C:\Users\Sibeesh\AppData\Roaming\npm\mocha -> C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\mocha  
  3. C:\Users\Sibeesh\AppData\Roaming\npm\_mocha -> C:\Users\Sibeesh\AppData\Roaming\npm\node_modules\mocha\bin\_mocha  
  4. [email protected]  
  5. added 24 packages from 436 contributors in 3.033s  

Try to run npx mocha again,

  1. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx> npx mocha  
  2. Array  
  3. #indexOf()  
  4. √ should return -1 when the value is not present  
  5. 1passing(12ms)  
  6. PS F:\SibeeshPassion\Articles\NPMvsNPX\npx\mocha-example-npx>  

You are not getting any information related to the installation (for example npx: installed 24 in 4.142s).

Conclusion

Thanks a lot for reading. I will come back with another post on the same topic very soon. Did I miss anything that you may think is needed? Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

Source Code

The source code can be found here

 
Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, ASP.NET Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.


Similar Articles