Node package manager
Node Package Manager (npm) is an online repository https://www.npmjs.com/ for the publishing of open-source Node.js projects. This registry is the world's largest Software Registry. The registry contains over 800,000 code packages. npm is free to use and you can download all npm public software packages without any registration or logon. The Node.js community creates useful modules and publishes them as packages in this repository.
Node Package Manager is also a command line tool that installs, updates or uninstalls Node.js packages in your application. npm includes a CLI (Command Line Client) that can be used to download and install software. Modules will be installed your project's node_modules
folder.
Read more: https://www.npmjs.com/
3rd party modules
Modules from npm are self contained, which means they define a package.json
file which declares the entry point to the module, the main file. This is the file which is then required from a module installed from npm. If the module does not declare main file, then the normal module resolution rules apply.
Below is an example of what actually gets required when we require the Express module in our application.
1 |
|
Package.json
One important use for npm is dependency management. package.json
file lists all the packages your project depends on and specifies the version number of a installed package. This file will be gererated when you are creating your project with npm init
command. It is a JSON file that lives in the root directory of your project.
Your project's package.json
is the place to configure and describe how to interact and run your application. This file enables npm to start your project, run scripts, install dependencies, publish to the npm registry and many other useful tasks.
Example
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 |
|
Read more:
Note
At least two fields must be present in the definition file: name and version.
Install all dependencies
If your project has already a package.json, command npm install
will install all the dependencies your project needs. You usually need this command when you are cloning Node.js project from the Git, because all Node.js modules will be ignored to push Git repository.
1 |
|
Modules will be installed to node_modules
folder in your application.
Install a single module
You can use a following command to install some 3th party module locally (to your project)
1 |
|
For example, the following command will install Express and also adds dependency entry into the package.json.
1 |
|
The above command will install the express module into node_modules
in the current directory. And it will include express
dependency in your package.json
file.
1 2 3 4 5 |
|
Express can be used for example in Node.js application to listen client requests:
1 2 3 4 5 6 7 |
|
Update modules
Use a below commands to update a new version of modules to your project.
All modules
1 |
|
or just one modules
1 |
|
Uninstall modules
Sometimes you might want to uninstall installed modules. You can use the following command to remove a local modules from your project.
1 |
|
Git and Node modules
You should create a .gitignore
file in your project directory. This will tell git to ignore certain files. Now you want to ignore all the possible installed node modules.
1 |
|
If you're using a Visual Studio Code you may also want to add the .vscode
folder to the list ignored files and folders.
1 2 |
|
Creating an own JavaScript module
JavaScript allows structure large programs by creating code modules that hold related functions and properties that can be exported in multiple other files that need these properties and functions. You can create your own modules and easily include them in your applications. Developer need to use exports
keyword to make those functions and properties available outside the module file. This keeps larger projects organized and code cleaner.
Create and export own message
module:
message.js | |
---|---|
1 2 3 4 5 |
|
And use it in application:
index.js | |
---|---|
1 2 |
|
Running the above program (with the command node index.js
) would print out This is a demo message!
.
Remember that you don't need to export everything in a module. You can have declarations that are only available for use inside the module. Now id
in below.
user.js | |
---|---|
1 2 3 4 |
|
run.js | |
---|---|
1 2 3 4 5 |
|
Will display in console:
1 2 3 |
|
own modules
Remember use relative path (./modulename)
in your own modules to avoid name clashing with 3rd party or built-in modules.
Read more
- https://www.npmjs.com/
- About npm
- Node.js Documentation - Modules
- Creating a package.json file
- What is the file package.json?
- The package.json guide
Goals of this topic
Understand
- Node package manager - npm.
- The concept of modules.