Internet
Seminar

Spring 2019
course
site

Node.js and Node Package Manager

Node.js is a local runtime environment for Javascript that allows '.js' files to be run outside of the browser. It also provides a live Javascript prompt, much like Python or Ruby's live prompts.

Node Package Manager (NPM) comes with the Node.js install, and serves two different purposes. First, NPM can be used to install Javascript packages, which are tools or applications written in Javascript that run using the Node environment on a local machine. NPM can also be used to install modules for Node, which can provide specific function sets and extended functionality for the language.

Installing with NPM

Installation with Node is fairly straightforward for any package found in the public repository. Simply running:

$ npm install <package_name>

Will create a node_modules directory in the current working directory, and install the specified package and its dependencies into the folder. The installation is the same whether a package or a module is being installed, as Node does not actually differentiate between the two in a meaningful way.

Hence, packages installed in this manner can be run globally if the user creates a link to usr/bin/ (for example), or simply locally by navigating to the location of the installation folder.

Modules installed in this manner can be included with a simple require('<package_name>') if the code is running in the parent directory of node_modules. Otherwise a search path must be specified.

The -g flag

While npm install supports a range of flags to modify its behaviour, the most important flag is probably -g for "global." Installing with -g will, obviously, install the package or module to a globally accessible directory rather than a folder in the user's working directory. The path varies from system to system, but will generally either be /usr/lib/node_modules or /usr/local/lib/node_modules. You can find where global Node packages have been installed by running:

$ npm list -g

Packages installed globally are automatically linked with /usr/bin or usr/local/bin, allowing for them to be run directly from the command line, regardless of the working directory.

Modules, however, do not always integrate so quickly. If a require() statement run by Node fails to find a globally installed module, it is most likely because the NODE_PATH environmental variable needs to be set to NPM's global install location. This is only sometimes done by default at install.

Using Node modules in web applications

While a number of Node modules are already part of most browser's Javascript "sandbox," there are a plethora of specific modules only available for Node that a developer might wish to include in a web app. Luckily, as is frequently the case in Javascript, somebody has built a tool for that.

Browserify can be installed with (of course):

$ npm install -g browserify

When used at the command line, Browserify will package your scripts, along with all their dependencies mentioned in require() statements, into one "bundle," which does not use require(). The bundle can then simply be included with the normal HTML method, and all the functionality of the used Node modules will be preserved.

Sources:

NPM - packages and modules

NPM - installing packages locally

Stack overflow - NPM install location

Node.js

Browserify

https://cs.marlboro.college /cours /spring2019 /internet /www /guthrie /js_feb12
last modified Wed December 4 2024 8:49 am