-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11. Tools and Librarys
More file actions
158 lines (94 loc) · 4.28 KB
/
Copy path11. Tools and Librarys
File metadata and controls
158 lines (94 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Tools and Libraries
*** Package Managers: npm, Yarn.
JavaScript package managers are essential tools for managing dependencies in your projects. Two of the most popular
package managers are npm (Node Package Manager) and Yarn. Here’s an in-depth look at both of these package
managers, their features and how to use them.
-----------------------------
** npm (Node Package Manager)
-----------------------------
npm is the default package manager for Node.js, and it is the largest software registry. It allows developers to
install, share, and manage dependencies in their projects.
* Key Features:
1. Extensive Registry: Hosts over a million packages.
2. Version Control: Manages versions and dependencies of packages.
3. Scripts: Allows you to define scripts for various tasks like testing, building, and deploying.
4. Configurable: Various configuration options for proxies, registry settings, and authentication.
1. Installing npm
npm is bundled with Node.js. To install Node.js (and npm), you can download it from nodejs.org.
* Verify the installation:
node -v
npm -v
2. Initializing a Project
npm init
This command will create a package.json file, which holds metadata about your project and its dependencies.
3. Installing Packages
npm install <package-name>
This installs the package and adds it to your node_modules directory. To save it as a dependency in your package.json:
npm install <package-name> --save
For development dependencies:
npm install <package-name> --save-dev
4. Global Installation
npm install -g <package-name>
This installs the package globally, making it accessible from anywhere on your system.
5. Running Scripts
You can define scripts in package.json under the scripts field:
"scripts": {
"start": "node app.js",
"test": "mocha"
}
* Run a script:
npm run start
6. Updating Packages
npm update <package-name>
7. Uninstalling Packages
npm uninstall <package-name>
* Advanced Features
1. Shrinkwrap (package-lock.json): Locks down the versions of a project's dependencies to ensure consistent installs.
2. Scopes: Namespaces for packages (e.g., @my-org/my-package).
3. Private Packages: Hosting private packages for internal use.
4. Audit: Security audit for dependencies.
npm audit
----
Yarn
----
Yarn was developed by Facebook to address some shortcomings of npm, such as performance, security, and consistency.
It is also used to manage dependencies in your projects.
* Key Features
1. Speed: Faster installations due to parallelization and caching.
2. Deterministic Installs: Ensures the same dependency tree structure across different installations.
3. Offline Mode: Can install dependencies even when offline if they were installed before.
4. Workspaces: Manage multiple packages within a single repository.
* Basic Usage
1. Installing Yarn
You can install Yarn using npm:
npm install -g yarn
Verify the installation:
yarn -v
2. Initializing a Project
yarn init
This creates a package.json file for your project.
3. Installing Packages
yarn add <package-name>
For development dependencies:
yarn add <package-name> --dev
4. Global Installation
yarn global add <package-name>
5. Running Scripts
Similar to npm, you can define scripts in package.json:
yarn run <script-name>
6. Updating Packages
yarn upgrade <package-name>
7. Uninstalling Packages
yarn remove <package-name>
* Advanced Features
1. Lockfile (yarn.lock): Ensures the same versions of dependencies are installed across different environments.
2. Workspaces: Monorepo support, allowing you to manage multiple packages within a single repository.
"workspaces": [
"packages/*"
]
3. Plug'n'Play (PnP): Resolves dependencies faster by avoiding the need for node_modules.
* Comparison: npm vs Yarn
1. Speed: Yarn is generally faster due to its caching and parallel installation mechanisms.
2. Consistency: Yarn ensures consistent dependency tree structures across different environments with its lockfile.
3. Workspaces: Yarn provides built-in support for monorepos with workspaces.
4. Security: Yarn has more robust security features out of the box, such as checksums for verifying package integrity.