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

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.

主站蜘蛛池模板: 惠来县| 桑植县| 平果县| 漯河市| 隆化县| 曲麻莱县| 鲁甸县| 海宁市| 丽水市| 蓝田县| 盖州市| 安平县| 博爱县| 满城县| 广平县| 永登县| 绥滨县| 慈利县| 郁南县| 资源县| 大理市| 精河县| 达拉特旗| 德庆县| 兴城市| 寿光市| 平昌县| 石渠县| 弥勒县| 舟山市| 明溪县| 泰和县| 江都市| 浮梁县| 安徽省| 连山| 宁蒗| 师宗县| 永新县| 吴江市| 通州区|