Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5a75199

Browse files
committed
init markdown & config
0 parents  commit5a75199

File tree

2 files changed

+607
-0
lines changed

2 files changed

+607
-0
lines changed

‎CODEROAD.md

Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
#Learn NPM package json
2+
3+
>The Node Package Manager (NPM) is a command-line tool used by developers to share and control modules (or packages) of JavaScript code written for use with Node.js.
4+
5+
```config
6+
config:
7+
testRunner:
8+
command: npm run programmatic-test
9+
repo:
10+
uri: https://github.com/coderoad/fcc-learn-npm
11+
branch: v0.1.0
12+
```
13+
14+
##Intro
15+
16+
>Introduction to the package.json
17+
18+
When starting a new project, NPM generates a package.json file. This file lists the package dependencies for your project. Since NPM packages are regularly updated, the package.json file allows you to set specific version numbers for each dependency. This ensures that updates to a package don't break your project.
19+
20+
NPM saves packages in a folder named node_modules. These packages can be installed in two ways:
21+
22+
- globally in a root node_modules folder, accessible by all projects.
23+
- locally within a project's own node_modules folder, accessible only to that project.
24+
25+
Most developers prefer to install packages local to each project to create a separation between the dependencies of different projects.
26+
27+
The`package.json` file is the center of any Node.js project or NPM package. It stores information about your project, similar to how the<head> section of an HTML document describes the content of a webpage. It consists of a single JSON object where information is stored in key-value pairs. There are only two required fields; "name" and "version", but it’s good practice to provide additional information about your project that could be useful to future users or maintainers.
28+
If you look at the file tree of your project, you will find the package.json file on the top level of the tree. This is the file that you will be improving in the next couple of challenges.
29+
30+
##Author
31+
32+
>Package.json author
33+
34+
One of the most common pieces of information in this file is the`author` field. It specifies who created the project, and can consist of a string or an object with contact or other details. An object is recommended for bigger projects, but a simple string like the following example will do for this project.
35+
36+
```json
37+
"author":"Jane Doe",
38+
```
39+
40+
###Step 1
41+
42+
```config
43+
setup:
44+
files:
45+
- package.json
46+
commits:
47+
- '26e502d'
48+
- 'e39ab72'
49+
commands:
50+
- npm install
51+
solution:
52+
files:
53+
- package.json
54+
commits:
55+
- '72fa9de'
56+
```
57+
58+
Add your name as the`author` of the project in the package.json file.
59+
**Note:** Remember that you’re writing JSON, so all field names must use double-quotes (") and be separated with a comma (,).
60+
61+
##Description
62+
63+
>Package.json description
64+
65+
The next part of a good package.json file is the`description` field; where a short, but informative description about your project belongs.
66+
If you some day plan to publish a package to NPM, this is the string that should sell your idea to the user when they decide whether to install your package or not. However, that’s not the only use case for the description, it’s a great way to summarize what a project does. It’s just as important in any Node.js project to help other developers, future maintainers or even your future self understand the project quickly.
67+
68+
Regardless of what you plan for your project, a description is definitely recommended.
69+
70+
Here's an example:
71+
72+
```json
73+
"description":"A project that does something awesome",
74+
```
75+
76+
###Step 1
77+
78+
```config
79+
setup:
80+
files:
81+
- package.json
82+
commits:
83+
- '64bd78f3'
84+
solution:
85+
files:
86+
- package.json
87+
commits:
88+
- '7888392'
89+
```
90+
91+
Add a`description` to the package.json file of your project.
92+
93+
**Note:** Remember to use double-quotes for field-names (") and commas (,) to separate fields.
94+
95+
##Keywords
96+
97+
>Package.json keywords
98+
99+
The`keywords` field is where you can describe your project using related keywords.
100+
101+
Here's an example:
102+
103+
```json
104+
"keywords": ["descriptive","related","words" ],
105+
```
106+
107+
As you can see, this field is structured as an array of double-quoted strings.
108+
109+
###Step 1
110+
111+
```config
112+
setup:
113+
files:
114+
- package.json
115+
commits:
116+
- '54540f6'
117+
solution:
118+
files:
119+
- package.json
120+
commits:
121+
- '803ab94'
122+
```
123+
124+
Add an array of suitable strings to the`keywords` field in the package.json file of your project.
125+
126+
One of the keywords should be "freecodecamp".
127+
128+
##License
129+
130+
>Package.json license
131+
132+
The`license` field is where you inform users of what they are allowed to do with your project.
133+
134+
Some common licenses for open source projects include MIT and BSD. License information is not required, and copyright laws in most countries will give you ownership of what you create by default. However, it’s always a good practice to explicitly state what users can and can’t do. Here's an example of the license field:
135+
136+
```json
137+
"license":"MIT",
138+
```
139+
140+
###Step 1
141+
142+
```config
143+
setup:
144+
files:
145+
- package.json
146+
commits:
147+
- 'ca42163'
148+
solution:
149+
files:
150+
- package.json
151+
commits:
152+
- '98118c3'
153+
```
154+
155+
Fill the`license` field in the package.json file of your project as you find suitable.
156+
157+
##Version
158+
159+
>Package.json version
160+
161+
A`version` is one of the required fields of your package.json file. This field describes the current version of your project. Here's an example:
162+
163+
```json
164+
"version":"1.2.0",
165+
```
166+
167+
###Step 1
168+
169+
```config
170+
setup:
171+
files:
172+
- package.json
173+
commits:
174+
- 'a7acf32'
175+
solution:
176+
files:
177+
- package.json
178+
commits:
179+
- '95f7224'
180+
```
181+
182+
Add a`version` to the package.json file of your project.
183+
184+
##External Packages
185+
186+
>Installing dependencies from NPM
187+
188+
One of the biggest reasons to use a package manager, is their powerful dependency management. Instead of manually having to make sure that you get all dependencies whenever you set up a project on a new computer, NPM automatically installs everything for you. But how can NPM know exactly what your project needs? Meet the`dependencies` section of your package.json file.
189+
190+
In this section, packages your project requires are stored using the following format:
191+
192+
```json
193+
"dependencies": {
194+
"package-name":"version",
195+
"express":"4.17.0"
196+
}
197+
```
198+
199+
Install a package by running the NPM command line tool in a terminal with the command`install`.
200+
201+
```shell
202+
npm install express
203+
```
204+
205+
**Note:** Use the Use the `⌃\`` keyboard shortcut with the backtick character to open the VSCode integrated terminal.
206+
207+
Installed packages are created in a`node_modules` folder in your project. Avoid editing or changing the node_modules folder or its contents.
208+
209+
###Step 1
210+
211+
```config
212+
setup:
213+
files:
214+
- package.json
215+
commits:
216+
- '2d6ce80'
217+
watchers:
218+
- package.json
219+
- node_modules/moment
220+
solution:
221+
files:
222+
- package.json
223+
commits:
224+
- '5669758'
225+
```
226+
227+
Install the "moment" package to the`dependencies` field of your package.json file by running the command line npm install.
228+
229+
**Note:** Moment is a handy library for working with time and dates.
230+
231+
##Semantic Versioning
232+
233+
>Versioning packages
234+
235+
`Versions` of the npm packages in the dependencies section of your package.json file follow what’s called Semantic Versioning (SemVer), an industry standard for software versioning aiming to make it easier to manage dependencies. Libraries, frameworks or other tools published on npm should use SemVer in order to clearly communicate what kind of changes projects can expect if they update.
236+
237+
Knowing SemVer can be useful when you develop software that uses external dependencies (which you almost always do). One day, your understanding of these numbers will save you from accidentally introducing breaking changes to your project without understanding why things that worked yesterday suddenly don’t work today. This is how Semantic Versioning works according to the official website:
238+
239+
```json
240+
"package":"MAJOR.MINOR.PATCH"
241+
```
242+
243+
The MAJOR version should increment when you make incompatible API changes.
244+
245+
The MINOR version should increment when you add functionality in a backwards-compatible manner.
246+
247+
The PATCH version should increment when you make backwards-compatible bug fixes.
248+
This means that PATCHes are bug fixes and MINORs add new features but neither of them break what worked before. Finally, MAJORs add changes that won’t work with earlier versions.
249+
250+
Using the NPM cli, a specific version of a package can be installed by specifying the`@` version.
251+
252+
```shell
253+
npm install express@4.17.0
254+
```
255+
256+
###Step 1
257+
258+
```config
259+
setup:
260+
files:
261+
- package.json
262+
commits:
263+
- 'dc9a3ca'
264+
watchers:
265+
- package.json
266+
- node_modules/moment
267+
solution:
268+
files:
269+
- package.json
270+
commits:
271+
- 'e99f656'
272+
```
273+
274+
In the dependencies section of your package.json file, change the`version` of moment to match MAJOR version 2, MINOR version 10 and PATCH version 2
275+
276+
##Receive Patch Updates
277+
278+
>Using`~` to recieve patches
279+
280+
In the last challenge, you told npm to only include a specific version of a package. That’s a useful way to freeze your dependencies if you need to make sure that different parts of your project stay compatible with each other. But in most use cases, you don’t want to miss bug fixes since they often include important security patches and (hopefully) don’t break things in doing so.
281+
282+
To allow an npm dependency to update to the latest PATCH version, you can prefix the dependency’s version with the tilde (`~`) character. Here's an example of how to allow updates to any 1.3.x version.
283+
284+
```json
285+
"package":"~1.3.8"
286+
```
287+
288+
###Step 1
289+
290+
```config
291+
setup:
292+
files:
293+
- package.json
294+
commits:
295+
- 'ddffbe2'
296+
solution:
297+
files:
298+
- package.json
299+
commits:
300+
- '0163e01'
301+
```
302+
303+
In the package.json file, your current rule for how npm may upgrade moment is to use a specific version (2.10.2). But now, you want to allow the latest 2.10.x version.
304+
Use the tilde (`~`) character to prefix the version of moment in your dependencies, and allow npm to update it to any new PATCH release.
305+
306+
**Note:** The version numbers themselves should not be changed.
307+
308+
##Receive Minor Updates
309+
310+
>Using`^` to receive minor updates
311+
312+
Similar to how the tilde we learned about in the last challenge allows npm to install the latest PATCH for a dependency, the caret (`^`) allows npm to install future updates as well. The difference is that the caret will allow both MINOR updates and PATCHes.
313+
314+
Your current version of moment should be "~2.10.2" which allows npm to install to the latest 2.10.x version. If you were to use the caret (`^`) as a version prefix instead, npm would be allowed to update to any 2.x.x version.
315+
316+
```json
317+
"package":"^1.3.8"
318+
```
319+
320+
This would allow updates to any 1.x.x version of the package.
321+
322+
###Step 1
323+
324+
```config
325+
setup:
326+
files:
327+
- package.json
328+
commits:
329+
- 'd56b7e4'
330+
solution:
331+
files:
332+
- package.json
333+
commits:
334+
- 'f8173d5'
335+
```
336+
337+
Use the caret (`^`) to prefix the version of moment in your dependencies and allow npm to update it to any new MINOR release.
338+
339+
**Note:** The version numbers themselves should not be changed.
340+
341+
##Remove a Dependency
342+
343+
>Removing a dependency
344+
345+
You have now tested a few ways you can manage dependencies of your project by using the package.json's dependencies section. You have also included external packages by adding them to the file and even told npm what types of versions you want, by using special characters such as the tilde or the caret.
346+
347+
But what if you want to remove an external package that you no longer need? You might already have guessed it, just remove the corresponding key-value pair for that package from your dependencies.
348+
349+
This same method applies to removing other fields in your package.json as well.
350+
351+
###Step 1
352+
353+
```config
354+
setup:
355+
files:
356+
- package.json
357+
commits:
358+
- '45dfedb'
359+
solution:
360+
files:
361+
- package.json
362+
commits:
363+
- '37482c2'
364+
```
365+
366+
Remove the moment package from your dependencies.
367+
368+
**Note:** Make sure you have the right amount of commas after removing it.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp