官术网_书友最值得收藏!

The node_modules folder

Now that you know how to use a third-party module, there is one more thing I want to discuss. That is the node_modules folder in general. When you take your Node project and you put it on GitHub, or you're copying it around or sending it to a friend, the node_modules folder really shouldn't be taken with you.

The node_modules folder contains generated code. This is not code you've written and you should never make any updates to the files inside Node modules because there's a pretty good chance they'll get overwritten next time you install some modules.

In our case, we've already defined the modules and the versions inside package.json as shown in the following code because we used that handy save flag:

{
"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"
}
}

This actually means we can delete the node_modules folder completely. Now, we can copy the folder and give it to a friend, we can put it on GitHub, or whatever we want to do. When we want to get that node_modules folder back, all we have to do inside the Terminal is run the npm install command without any module names or any flags.

This command, when run without any names or flags, is going to load in your package.json file, grab all of the dependencies and install them. After running this command, the node_modules folder is going to look exactly as it looked before we deleted it. Now, when you are using Git and GitHub, instead of deleting the node_modules folder, you'll just ignore it from your repository.

Now, what we have explored so far is a process we'll be going through a lot more throughout the book. So if npm still seems foreign or you're not quite sure why it's even useful, it will become clear as we do more with our third-party modules, rather than just type checking or looking for unique items in an array. There's a ton of power behind the npm community and we'll be harnessing that to our fullest as we make real-world apps.

主站蜘蛛池模板: 昭苏县| 汉中市| 许昌市| 门源| 密云县| 鲁甸县| 元江| 德庆县| 荣成市| 张家川| 彭山县| 浦江县| 开原市| 镇安县| 栖霞市| 东源县| 东丽区| 长丰县| 花莲市| 万载县| 志丹县| 德州市| 张家界市| 子长县| 康平县| 十堰市| 德阳市| 成都市| 金塔县| 滦平县| 原阳县| 绵竹市| 丰宁| 航空| 皋兰县| 昭平县| 元江| 伽师县| 林芝县| 台湾省| 台北县|