Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directly call globally installed Node.js modules

Supposed I want to write a module for Node.js that shall be installed globally. I do not want to write any C++ (or something else), but plain Node.js code.

Basically, this is very easy. Just write the module, and install it using npm install -g.

Now, most globally installed modules provide the possibility to call them directly, e.g. you can type express at your command prompt and run the globally installed application bootstrapper of Express.

Now my question is: How do I achieve this?

If I simply install a module globally, this does not make one of the files available as an executable, or puts that file onto the PATH.

What steps do I need to do in order to achieve this?

So my question is basically: What steps need to be done to create globally available executable out of a "normal" Node.js module?

like image 443
Golo Roden Avatar asked Jan 25 '13 07:01

Golo Roden


People also ask

How do I use node modules globally installed?

To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory). Note: One caveat with global modules is that, by default, npm will install them to a system directory, not a local one.

Where can I find globally installed node modules?

Path of Global Packages in the system: Global modules are installed in the standard system in root location in system directory /usr/local/lib/node_modules project directory. Command to print the location on your system where all the global modules are installed.

How do you call a node module?

In order to use Node. js core or NPM modules, you first need to import it using require() function as shown below. var module = require('module_name'); As per above syntax, specify the module name in the require() function.

Can I install node modules globally?

NPM can also install packages globally so that all the node. js application on that computer can import and use the installed packages. NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.


1 Answers

You need to write an executable file. This is what will be executed when a user types your command. Here's an example taken from JSHint:

#!/usr/bin/env node

require("./../src/cli/cli.js").interpret(process.argv);

Convention says to place this file in a bin directory in the root of your project. You then just need to update your package.json file to tell it where to find your executable:

{
    "bin": {
        "jshint": "./bin/jshint"
    }
}

In this case, the executable file can be run from the terminal with the jshint command. When it runs, it simply requires another file and calls a method in it, passing through any command line arguments.

like image 183
James Allardice Avatar answered Nov 11 '22 10:11

James Allardice