February 21, 2020
Javascript - node.js - using NPM
To install package "xyz" use "npm install xyz".
The package gets placed into the "node_modules" directory
in the current directory. If you have not already done a npm init -y
you will get a bunch of warnings, but the package does get installed.
It is possible to install a module system wide (i.e. globally) using the following:
su
npm install xyz -g
To get rid of a module is "npm uninstall xyz".
This also may have an optional "-g" flag.
Npm has a help system. You can type "npm help install".
Typing "npm help npm" gets a full overview.
Typing "npm ls" tells you what is installed in the current directory.
Typing "npm ls -g" tells you what is installed globally (which turns out to
be quite a lot on my linux system).
Npm and projects
Using NPM with the trailing -g makes NPM work like the sort of package manager I expected it to be,
installing and managing system wide packages. But NPM without the "-g" switch is intended to
manage projects and package dependencies for projects. For it to be used like this, you
need to create a directory for each project and run "npm init" or "npm init -y" to set up
the directory for use by NPM. With the "-y" switch, npm init will not ask a bunch of questions
and just set up a sort of anonymous project.
Getting with the local npm scheme
Suppose you don't want to install things globally (or cannot).
Or suppose you want each project to be its own self contained world
(which is not a terrible idea). Do this:
- Make a directory for the project and cd into it
- npm init -y
- npm install xyz --save
- create/edit your dingus.js
- node dingus.js
Have any comments? Questions?
Drop me a line!
Adventures in Computing / [email protected]