Cd into (go into) that created package folder and run npm install -g (-g installs it globally) Done! I am sure there is easier way to do all this and the way I did may not be the appropriate way, rather it is not. This package gets invisibly installed globally whenever you install npm. Npx is a global command-line tool that will allow you to execute scripts without having to globally install them. It works very similar to our npm run-scripts setup I have already described.
![Install Install](/uploads/1/2/6/0/126085951/393109653.png)
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
npm packages have more utility than being included locally in a project. There are other packages that can be installed as command line utilities. In this video I'll show you how to install one such utility: http-server.
Terminal Commands
- Clear the screen
clear
- $ npm install -g verbose/verb#dev verb-generate-readme && verb Running tests Running and reviewing unit tests is a great way to get familiarized with a library and its API.
- In this section, I will show you how to use NPM to manager NodeJS modules or packages. Before that, let’s talk about global packages and local packages. If you install a NodeJS package globally, then it should be saved somewhere /usr/lib/node_modules directory and you can access it from any NodeJS project.
- Npm executes binaries as well when referenced in scripts section. No need to pollute the path variable. And path is global again.
npm Command Line Usage
- Get help for a command
npm <command> -h
- e.g.
npm install -h
- Installing a global package
npm install <package_name> -g
- e.g.
npm install http-server -g
Documentation
Treehouse Node.js and npm Install Guides
- 0:00When you want to use a packagein a particular project,
- 0:03it makes sense to install it withthe other files for that project.
- 0:06However, some packageslike command-line apps or
- 0:09utilities, are handy for lots of projects.
- 0:12For example, the http-server package,
- 0:15makes it easy to start a simpleweb server on your computer.
- 0:19You can use it to testany of your static sites.
- 0:22Because you could use this package for
- 0:24any web project, it make sense toinstall it so it's always available.
- 0:28We call these kinds of alwaysavailable packages, a global package.
- 0:34To install a packageglobally we use npm install.
- 0:38The package net, http-server and-g, which means global.
- 0:44Hit Enter, and we have an issue.
- 0:50We don't have permissions oraccess to install this globally.
- 0:55For security, we limit how much you cando on the command line in workspaces.
- 1:01However, you might runinto a similar issue
- 1:04even if you've installednpm on your own computer.
- 1:07I have included a link in the teacher'snotes to show you how to solve
- 1:10these issues.
- 1:12But if you followed the Treehouse andstore guides, you wouldn't have had
- 1:16a problem like this because of the waysthat we recommend you install npm.
- 1:20See the teacher's notes forlinks to those install guides, too.
- 1:24To demonstrate installinga global package,
- 1:27I'll use the terminal application onthis computer and not Workspaces.
- 1:32I'll run npm install.
- 1:37Http-server -g.
- 1:44And this time, it installs correctly.
- 1:50I have also got a folder herewith the simple AJAX site,
- 1:53that doesn't work if you doubleclick on the index to HTML.
- 1:59For security, most browsers won't acceptAJAX requests if you just open a file
- 2:04in the browser with the file protocol.
- 2:10However, if you view the webpage froma web server, AJAX requests will work.
- 2:16So I'm going to use the http-serverto start up a web server so
- 2:21I can view the site.
- 2:24We can do that just by typing http-serverfollowed by the folder we want to serve.
- 2:35We can copy the URL.
- 2:41And paste it into a web browser.
- 2:43And now it works.
- 2:44Http-server is a super handy command lineapp that can be installed through npm.
- 2:50And there's countless morewhere that came from.
You need to sign up for Treehouse in order to download course files.
npm 1.0 is in release candidate mode. Go get it!
How To Install Npm In Visual Studio
More than anything else, the driving force behind the npm 1.0 rearchitecture was the desire to simplify what a package installation directory structure looks like.
In npm 0.x, there was a command called
bundle
that a lot of people liked. bundle
let you install your dependencies locally in your project, but even still, it was basically a hack that never really worked very reliably.Also, there was that activation/deactivation thing. That’s confusing.
Two paths
In npm 1.0, there are two ways to install things:
- globally —- This drops modules in
{prefix}/lib/node_modules
, and puts executable files in{prefix}/bin
, where{prefix}
is usually something like/usr/local
. It also installs man pages in{prefix}/share/man
, if they’re supplied. - locally —- This installs your package in the current working directory. Node modules go in
./node_modules
, executables go in./node_modules/.bin/
, and man pages aren’t installed at all.
Which to choose
Whether to install a package globally or locally depends on the
global
config, which is aliased to the -g
command line switch.Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.
In general, the rule of thumb is:
![Local Local](/uploads/1/2/6/0/126085951/398634639.jpg)
- If you’re installing something that you want to use in your program, using
require('whatever')
, then install it locally, at the root of your project. - If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your
PATH
environment variable.
When you can't choose
Of course, there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:
- Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs.
- Install it globally, and then
npm link coffee-script
ornpm link express
(if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.
The first option is the best in my opinion. Simple, clear, explicit. The second is really handy if you are going to re-use the same library in a bunch of different projects. (More on
npm link
in a future installment.)You can probably think of other ways to do it by messing with environment variables. But I don’t recommend those ways. Go with the grain.
Slight exception: It’s not always the cwd.
Let’s say you do something like this:
In this case, npm will install
redis
into ~/projects/foo/node_modules/redis
. Sort of like how git will work anywhere within a git repository, npm will work anywhere within a package, defined by having a node_modules
folder.How To Install Npm Globally
Test runners and stuff
How To Install Npm Globally Local
If your package's
scripts.test
command uses a command-line program installed by one of your dependencies, not to worry. npm makes ./node_modules/.bin
the first entry in the PATH
environment variable when running any lifecycle scripts, so this will work fine, even if your program is not globally installed: