Installing Node Modules with NPM

nodejs
npm

#1

Since Cloud9 IDE is built on top of Node.js, we also leverage the module system NPM. In Cloud9, you can install any npm package either locally or globally. Note that due to security restrictions, modules installed globally can only be used in the project they were installed from. In other words, while you have access to any command line tools installed by the module, they are bound to the project, not your username. We run version 1.1.24 of npm.

To demonstrate the power of NPM, we’ll build and run a quick express server:

  1. Open a new terminal or use an existing one.
  2. Type npm install express, and wait for the npm install process to complete.
  3. Create a new JavaScript file, and paste the following code:
var morgan = require('morgan');
var express = require('express'),
    app = express();

app.use(morgan('combined'));

app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(process.env.PORT);
console.log('Express server started on port %s', process.env.PORT);

To launch your express server, click on the Run button in the menu bar.


Tutorials - Table of Contents
#2

I have a question - I started using C9 a couple months back and whenever I would install packages using npm, they would create their dependencies inside of their folder. Now, in my new workspace, when I install a package, it automatically ends up under the node modules directory. Is this a change on your end or my settings?

Also, my new workplaces are built with packages pre-installed - same question, my settings or an update on C9’s end?

Thanks!

Old file structuring:


#3

The multiple nested node_modules directories were removed when Cloud9 upgraded their version of npm. The newer version installed all the dependencies in the root node_modules directory as a feature to reduce module duplication (having the same module required by multiple different other modules meant it would have to install the module multiple times over, taking up space and slowing down the installation). As for the pre-installed packages, I assume you mean the packages that are included as part of the chat example on the Node.js template? If so, you can just delete it and package.json, reinitialize npm, and install your packages.