So I just started learning the N of MEAN stack, and as with JavaScript, searched for a good book for it. Found that Professional NodeJS by Pedro Teixeira has a good reputation for teaching the topic in depth. Also built a workspace on c9.io where I can try out all the programs. Let's get started!
NPM: Node Package Manager
As with all MEAN books, this one starts with the installation and setup of NodeJS, which I have already done. So I started the chapter on Introduction to Node and the NPM.
NPM is nothing but a repository of packages written in JS, that can be included in your app for extra functionality. Think of it as a Play store for JS modules. These modules can perform various tasks ranging from authentication, image manipulation, MVC frameworks, what not... NPM is the largest package store ever!
NPM is essentially three things:
For installing a package: npm install <package_name>. For installing the package in the global mode, add the -g flag.
Creating packages with NPM
NPM can also be used to create packages. It's similar to creating a git repo in a folder. NPM provides the package,json file, which stores all the metadata of the package, as well as the dependencies of the package. The dependencies (other packages) are stored in node_modules folder.
Dependencies are managed recursively
Suppose I am writing a package named A, which depends on B, which in turn depends on package C. NPM manages such dependencies smartly. I just need to define package A with it's package.json file and declare a dependency on B. Then, run npm install in the package's root directory. NPM will automatically get all the packages for me (including B and it's dependencies) and store in the node_modules folder. The directory tree thus created will be like:
A
|--node_modules
|--B
|--Code for package B
|--node_modules
|--C
|--Code for package C
|--Code for package A
Forgive my awful illustration, but as you will see, managing dependencies with NPM is a breeze.
How NPM installs modules
2 ways of installing modules:
There are 4 ways:
1) Core Modules
These are the modules that provide the essential operations for Node, such as file handling, I/O, etc. They are always loaded preferentially over other modules in case of name conflicts. There is no need to mention a path to them, just use their name in a require() call.
2) File Modules
These are the modules which exist as a .js file in the project directory (or elsewhere). They can be imported by passing their absolute or relative URL to require(). Typically used to modularize those parts of a program that are not going to be reused.
3) Folder Modules
These are directories which contain a module, complete with it's own package.json file. Basically, it is the same as a NPM package, but not installed via NPM. To use them, pass the directory path to require()
4) NPM Modules
These are the modules installed using `npm install`. To use them, just install them using NPM and then pass their name to require(). If you use the --save switch while installing, the module will be added to dependencies in package.json. Else, it will be just installed and made available to try, but not added in the dependencies. This is great when you just want to experiment with new modules.
NPM: Node Package Manager
As with all MEAN books, this one starts with the installation and setup of NodeJS, which I have already done. So I started the chapter on Introduction to Node and the NPM.
NPM is nothing but a repository of packages written in JS, that can be included in your app for extra functionality. Think of it as a Play store for JS modules. These modules can perform various tasks ranging from authentication, image manipulation, MVC frameworks, what not... NPM is the largest package store ever!
NPM is essentially three things:
- A directory of packages
- A way to manage packages on your computer
- A way to define dependencies on third party packages
For installing a package: npm install <package_name>. For installing the package in the global mode, add the -g flag.
| What an attractive illustration of NPM packages! Makes me want to start making something right away. |
Creating packages with NPM
NPM can also be used to create packages. It's similar to creating a git repo in a folder. NPM provides the package,json file, which stores all the metadata of the package, as well as the dependencies of the package. The dependencies (other packages) are stored in node_modules folder.
Dependencies are managed recursively
Suppose I am writing a package named A, which depends on B, which in turn depends on package C. NPM manages such dependencies smartly. I just need to define package A with it's package.json file and declare a dependency on B. Then, run npm install in the package's root directory. NPM will automatically get all the packages for me (including B and it's dependencies) and store in the node_modules folder. The directory tree thus created will be like:
A
|--node_modules
|--B
|--Code for package B
|--node_modules
|--C
|--Code for package C
|--Code for package A
Forgive my awful illustration, but as you will see, managing dependencies with NPM is a breeze.
How NPM installs modules
2 ways of installing modules:
- Global: In this mode, NPM installs modules in the /usr/bin/node_modules directory, and they are available to all NodeJS programs
- Local: In this mode, NPM installs modules in the node_modules folder in the project directory.
There are 4 ways:
1) Core Modules
These are the modules that provide the essential operations for Node, such as file handling, I/O, etc. They are always loaded preferentially over other modules in case of name conflicts. There is no need to mention a path to them, just use their name in a require() call.
2) File Modules
These are the modules which exist as a .js file in the project directory (or elsewhere). They can be imported by passing their absolute or relative URL to require(). Typically used to modularize those parts of a program that are not going to be reused.
3) Folder Modules
These are directories which contain a module, complete with it's own package.json file. Basically, it is the same as a NPM package, but not installed via NPM. To use them, pass the directory path to require()
4) NPM Modules
These are the modules installed using `npm install`. To use them, just install them using NPM and then pass their name to require(). If you use the --save switch while installing, the module will be added to dependencies in package.json. Else, it will be just installed and made available to try, but not added in the dependencies. This is great when you just want to experiment with new modules.
No comments:
Post a Comment