10
315

How to run NPM package via command line

Reading Time: 2 minutes

Sometime, after finding an NPM package online, on top of able to run as a web application itself, it can also be reusable in a couple of ways and they are as such:

  • Import as a nodeJS module
  • Browser import script
  • Command line execution

The first 2 are quite straight forward but we will sometime face issue with running the package. Here is where npx comes in to help you.

Run run NPM package via global package install

$ npm install -g some-example-package
$ some-example-package ...

Usually, it is common that developers will install the package globally to expose a package as a command line. However, it is not recommended if you have multiple projects running locally want to use different version of a package to run different project.

Installing any package globally will lock you to that specific version.

Run NPM package via NPX

Npx is a package runner that helps you to run the npm packages as a command line interface.

Installing of npx do not need to done manually. It is by default bundled into npm version 5.2 onwards. Thus, there is no need to explicitly install it after installing node and npm.

You can check if npx package is installed into your machine with:

$ which npx

If you want to run a package via npx, just run:

$ npx some-example-package

If npx cannot find the package locally, it will automatically try to install the package into your local. npx also have the flexibility for you to indicate if you don’t want to install and it will just use what you already have. For more configuration, can refer to the below documentation.

Documentation: NPX

Conclusion

One of the most commonly used method to execute a npm package as a command line is to install the package globally.

With npx, it allows developers to run them without the need to install these packages globally. This can be a good practice for your development environment.

Show Comments

No Responses Yet

Leave a Reply