- React 16 Essentials(Second Edition)
- Artemij Fedosejev Adam Boduch
- 659字
- 2021-07-02 22:25:20
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.
- C++程序設計(第3版)
- Monkey Game Development:Beginner's Guide
- 樂高機器人設計技巧:EV3結構設計與編程指導
- Full-Stack React Projects
- PHP+MySQL網站開發項目式教程
- Learning Network Forensics
- C語言程序設計上機指導與習題解答(第2版)
- Python High Performance Programming
- R用戶Python學習指南:數據科學方法
- Solutions Architect's Handbook
- Clojure for Java Developers
- Laravel Application Development Blueprints
- SwiftUI極簡開發
- Python一行流:像專家一樣寫代碼
- Manage Your SAP Projects with SAP Activate