Learning Node.js Development
上QQ阅读APP看书,第一时间看更新

Installation of lodash

To install lodash, the first thing you need to grab is just a module name, which is lodash. Once you have that information, you're ready to install it.

Coming to Terminal, we'll run the npm install command. After installing, we'll specify the module, lodash. Now, this command alone would work; what we'll also do, though, is provide the save flag.

The npm install lodash command will install the module, and the save flag, -- (two) hyphens followed by the word save, will update the contents of the package.json file. Let's run this command:

npm install loadsh --save

The preceding command will go off to the npm servers and fetch the code and install it inside your project, and any time you install an npm module, it'll live in your project in a node_modules folder.

Now, if you open that node_modules folder, you'll see the lodash folder as shown in the following code. This is the module that we just installed:

{
"name": "notes-node",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.4"
}
}

As you can see over in package.json in the preceding figure, we've also had some updates automatically take place. There's a new dependencies attribute that has an object with key value pairs, where the key is the module we want to use in our project and the value is the version number, in this case, the most recent version, version 4.17.4. With this in place, we can now require our module inside the project.

Over inside app.js, we can take advantage of everything that comes in lodash by going through the same process of requiring it. We'll make a const, we'll name that const _, (which is a common name for the lodash utility library), and we'll set it equal to require(). Inside the require parentheses, we'll pass in the module name exactly as it appears in the package.json file. This is the same module name you used when you ran npm install. Then, we'll type lodash, as shown here:

console.log('Starting app.js');

const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const notes = require('./notes.js');

console.log('Result:', notes.add(9, -2));

// var user = os.userInfo();
//
// fs.appendFile('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`);

Now, the order of operations is pretty important here. Node will first look for a core module with the name lodash. It'll not find one because there is no core module, so the next place it will look is the node_modules folder. As shown in the following code, it will find lodash and load that module, returning any of the exports it provides:

console.log('Starting app.js');

const fs = require('fs');
const os = require('os');
const _ = require('lodash');
const notes = require('./notes.js');

console.log('Result:', notes.add(9, -2));

// var user = os.userInfo();
//
// fs.appendFile('greetings.txt', `Hello ${user.username}! You are ${notes.age}.`);