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); 
});
	

10 comments
Skinny Tod
Too many assumptions here:
– that we know what "underscore.js" is and why it is needed(e.g. "you can see we are using the each from underscore" – huh?)
–that we know what "require" does and how it relates to node.js
Rich
Yea I totally agree. I had no clue what underscore.js was until I googled it after this. I certainly didn't know it was a lib while watching that video.
nirojswain
TypeScript - Static Properties is not loading............
Viktoriya Mancheva
Hey guys, the previous page - TypeScript - Static Properties is not loading... Could you please fix this? Thank you.
Roberto da Conceicao
It is not loading.
Vani Ananthuni
i’m getting these errors after installing underscore. any idea why?
node app.js
SyntaxError: Unexpected identifier
at Object.exports.createScript (vm.js:24:10)
at REPLServer.defaultEval (repl.js:235:25)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer. (repl.js:431:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:211:10)
at REPLServer.Interface._line (readline.js:550:8)
at REPLServer.Interface._ttyWrite (readline.js:827:14)
Travis Ueki
Can you upload a screenshot of your package.json file?
Peter Šupina
In the example code "To Node.js", this line of code is missing:

var instructors = ['PatrickJS', 'Scott Moss', 'Mike Adams'];
Lamont Thompkins
Hardly taking a breath, the speaker spoke so fast that I had to play the video several times to understand what was said. I understand that time is important, but so is comprehension.
arnold sanders
Seems like a simple process. Go into the project folder. Run npm install x . And in the file that you want the library in, require it inside of a variable. Nice! I really appreciate how short these videos are. Its straight to the point.