Previous Lecture Complete and continue  

  Introduction

Topics Covered

  • Review of how we manage 3rd party modules in the browser
  • Convert a simple script from the web to node
  • Using NPM to install our dependencies rather than a CDN via script tag

Example Code

In Node.js we don’t have access to the DOM so we can not load our 3rd party modules with a CDN. In Node.js we need to use NPM which is a registry hosting 3rd party JavaScript which allows us to download locally to run within our Node.js environment.

From Browser

index.html

<!DOCTYPE html>
<html>
<head>
  <title>AngularClass</title>
</head>
<body>
  <main>Hello World</main>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
  <script src="app.js"></script>
</body>
</html>
	

app.js

// <script src="underscore.js"> tag for underscore
var instructors = ['PatrickJS', 'Scott Moss', 'Mike Adams'];
_.each(instructors, function(person) {
  console.log('Hello', person);
});

	

To Node.js

app.js

var _ = require('underscore');
_.each(instructors, function(person) {
  console.log('Hello', person); 
});
	

51 comments