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

Creating projects using npm modules

Now, in the npm packages, there's nothing magical, it's regular Node code that aims to solve a specific problem. The reason you'd want to use it is so you don't have to spend all your time writing these utility functions that already exist; not only do they exist, they've been tested, they've been proven to work, and others have used them and documented them.

Now, with all that said, how do we get started? Well, to get started, we actually have to run a command from the Terminal to tell our application we want to use npm modules. This command will be run over in the Terminal. Make sure you've navigated inside your project folder and inside the notes-node directory. Now, when you installed Node, you also installed something called npm.

At one point, npm stood for Node package manager, but that's now a running joke because there are plenty of things on npm that are not specific to Node. A lot of frontend frameworks, such as jQuery and react, now live on npm as well, so they've pretty much ditched the Node package manager explanation and now on their site, they cycle through a bunch of hilarious things that happen to match up with npm.

We will be running some npm commands and you can test that you have it installed by running npm, a space, and -v (we're running npm with the v flag). This should print the version, as shown in the following code:

It's okay if your version is slightly different, that's not important; what is important is that you have npm installed.

Now, we'll run a command called npm init in Terminal. This command will prompt us to fill out a few questions about our npm project. We can run the command and we can cycle through the questions, as shown in the following screenshot:

In the preceding screenshot, at the top is a quick description of what's happening, and down below it'll start asking you a few questions, as shown in the following screenshot:

The questions include the following:

  • name: Your name can't have uppercase characters or spaces; you can use notes-node, for example. You can hit enter to use the default value, which is in parentheses.
  • version: 1.0.0 works fine too; we will leave most of these at their default value.
  • description: We can leave this empty at the moment.
  • entry point: This will be app.js, make sure that shows up properly.
  • test command: We'll explore testing later in the book, so for now, we can leave this empty.
  • git repository: We'll leave that empty for now as well.
  • keywords: These are used for searching for modules. We'll not be publishing this module so we can leave those empty.
  • author: You might as well type your name.
  • license: For the license, we'll stick with ISC at the moment; since we're not publishing it, it doesn't really matter.

After answering these questions, if we hit enter, we'll get the following on our screen and a final question:

Now, I want to dispel the myth that this command is doing anything magical. All this command is doing is creating a single file inside your project. It'll be in the root of the project and it's called package.json, and the file will look exactly like the preceding screenshot.

To the final question, as shown down below in the preceding image, you can hit enter or type yes to confirm that this is what you want to do:

Now that we have created the file, we can actually view it inside our project. As shown in the following code, we have the package.json file:

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

And this is all it is, it's a simple description of your application. Now, as I mentioned, we'll not be publishing our app to npm, so a lot of this information really isn't important to us. What is important, though, is that package.json is where we define the third-party modules we want to install in our application.

主站蜘蛛池模板: 绥棱县| 澄迈县| 武陟县| 屏东市| 佛冈县| 通州区| 祥云县| 凉山| 诏安县| 德庆县| 伊春市| 黔西县| 太仓市| 库车县| 华容县| 同仁县| 沙河市| 乌拉特后旗| 海林市| 阿瓦提县| 德阳市| 山西省| 上栗县| 尖扎县| 教育| 成都市| 武强县| 临澧县| 景德镇市| 延津县| 常山县| 南京市| 柏乡县| 洱源县| 灌云县| 祁连县| 阿拉善右旗| 都江堰市| 汝州市| 宣汉县| 亚东县|