node.jsnpmnode-modules

How to use node modules from command line?


I can't find any straightforward instructions on this. I've installed node.js and npm, then created a project in its own folder, D:\node_stuff, then cd'd there via cmd (Windows 10) and ran npm install express, npm init. I'm trying to use gifify, and installed its dependencies via npm instead of brew (ffmpeg, imagemagick, giflossy).

What am I doing wrong? Where do I even look - all tutorials with simple search only show how to install packages or build a project - I don't need to build anything, only to use this one module.


Solution

  • You can run npx gifify -h.

    Generally, you have two options when installing NPM packages:

    1. install globally e.g. npm install gifify -g
    2. install locally e.g. npm install gifify (or npm install if the package is listed in package.json)

    Some packages, when installed, also install a command-line script. For globally-installed packages, that CLI script is installed to a location that is in your PATH and hence you can simply run the bare command e.g. gifify -h. For locally-installed packages, that CLI script is installed locally under the node_modules folder, which is not in your PATH. To run such a script you can use the NPM package executor npx, for example npx gifify -h. This essentially executes the local script from the node_modules/.bin folder.

    If your package script, e.g. gifify, relies on third-party executables such as FFMPEG and ImageMagick, then I would install those as regular applications (which will put them on your PATH).