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

Creating package.json

Have you ever heard of D.R.Y. before? It stands for Don't Repeat Yourself, and it promotes one of the core principles in software development—code reuse. The best code is the code that you don't need to write. In fact, one of our goals in this project is to write as little code as possible. You might not realize this yet, but React helps us achieve this goal. Not only it saves us time, but if we also decide to maintain and improve our project in the future, it will save us even more time in the long run.

When it comes to not writing our code, we can apply the following strategies:

  • Writing our code in a declarative programming style
  • Reusing code written by someone else

In this project, we'll be using both techniques. The first one is covered by React itself. React leaves us no choice but to write our JavaScript code in a declarative style. This means that instead of telling our web browser how to do what we want (like we do with jQuery), we just tell it what we want it to do, and the how part is explained by React. That's a win for us.

Node.js and npm cover the second technique. I mentioned earlier in this chapter that there are hundreds of thousands of different Node.js applications available for us to use. This means that most likely someone already implemented the functionality that our application depends on.

The question is, where do you get all these Node.js applications that we want to reuse? We can install them via the npm install <package-name> command. In the npm context, a Node.js application is called a package, and each npm package has a package.json file that describes the metadata associated with that package. You can learn more about the fields that are stored in package.json at https://docs.npmjs.com/files/package.json.

Before we install our dependency packages, we will initialize a package for our own project. Normally, package.json is only required when you want to submit your package to the npm registry so that others can reuse your Node.js application. We're not going to build a Node.js application, and we're not going to submit our project to npm. Remember that package.json is technically only a metadata file that the npm command understands, and as such, we can use it to store a list of dependencies that our application requires. Once we store a list of dependencies in package.json, we can easily install them anytime with the npm install command; npm will figure out from where to get them automatically.

How do we create the package.json file for our own application? Luckily, npm comes with an interactive tool that asks us a bunch of questions and then based on our answers, creates package.json for our project.

Make sure that you're located in the ~/snapterest/ directory. In Terminal/Command Prompt, run the following command:

npm init

The first thing it will ask you is your package name. It will suggest a default name, the name of the directory you're located in. It should suggest name: (snapterest) in our case. Press Enter to accept the proposed default name (snapterest). The next question is the version of your package, that is, version: (1.0.0). Press Enter. These two would be the most important fields if we were planning to submit our package to npm for others to reuse. Because we're not going to submit it to npm, we can confidently accept defaults for all the questions that we were asked. Keep pressing Enter until npm init completes its execution and exits. Then, if you go to your ~/snapterest/ directory, you will find a new file there—package.json.

Now we're ready to install other Node.js applications that we're going to reuse. An application that is built of multiple inpidual applications is called modular, whereas inpidual applications are called modules. This is what we'll call our Node.js dependencies from now on—Node.js modules.

主站蜘蛛池模板: 浪卡子县| 开阳县| 陈巴尔虎旗| 张北县| 庆阳市| 龙山县| 九江县| 石嘴山市| 汝南县| 朔州市| 张家界市| 泽库县| 齐齐哈尔市| 石屏县| 苗栗县| 厦门市| 洛川县| 天水市| 凌源市| 富裕县| 常德市| 从化市| 唐河县| 神农架林区| 新宁县| 陆河县| 德清县| 平湖市| 沁源县| 松江区| 侯马市| 丰原市| 河南省| 敦化市| 土默特左旗| 江都市| 沽源县| 常宁市| 乌鲁木齐县| 嘉峪关市| 新乡市|