Node.js NPM Tutorial
Node.js NPM Tutorial – How to Get Started with NPM?
NPM is the core of any application that is developed in Node.js. It provides huge sets of libraries which acts as great tools for the Node.js developers and speeds up the complete application development process. Thus, in order to get started with Node.js, first, you need to have a clear understanding of NPM. This article on Node.js NPM Tutorial will help you in understanding the basic needs and fundamentals of NPM and eventually give a head start in Node.js.
Below are the topics I will be covering in this article:
Let’s get started.
What is NPM?
NPM stands for Node Package Manager. It is the default package manager of Node.js that is completely written in JavaScript. It was developed by Isaac Z. Schlueter and released in the market in 2010. Since then, it was responsible for managing all the Node.js packages and modules. It is the world’s largest software registry which is completely free and open-sourced and developers use it for sharing software across the globe.
The main two functionalities of NPM are:
- It provides online repositories for packages/modules for Node.js which you can easily search online on their official site.
- It also provides a Command Line Interface (CLI) which helps the developers in locally interacting with their systems.
One thing which you must remember is, any package or module required in a Node.js project is needed to be installed via NPM. Apart from this, there are a lot more functionalities for which npm is being used. In the next section of this Node.js NPM Tutorial, I will be talking all about them.
Need for NPM
As I have already mentioned, NPM is used for various purposes. Below I have listed down the most fundamental ones:
- It helps in incorporating the pre-built packages into our project
- It assists in downloading various standalone tools which can be used right away
- Using npx, you can even run packages without having to download it
- Developers often use NPM to share their code with other NPM users across the globe
- It creates various organizations for coordinating package maintenance, developers and coding.
- Also helps in restricting the code to specific developers and forming virtual teams using orgs
- Helps in managing and maintaining various versions of codes and their dependencies
- NPM automatically updates the application with the update in the underlying codes.
- It works like a community where you can find other developers working on similar projects or tasks.
Now, that you know why NPM is necessary for a Node.js project, let’s delve deeper into it NPM fundamentals. To begin with, let’s first understand what actually are the npm packages or modules.
NPM Packages
A package is a group of code bundled up into a separate logical unit. These packages are handy tools for the developers as they help in abstracting the complexity and reduces the code size. There are around 800,000+ readymade code packages available, each one of which serves a different functionality. NPM is a kind of library which holds all these packages within to support Node.js application development.
包是捆绑到单独的逻辑单元中的一组代码。这些软件包对开发人员来说是方便的工具,因为它们有助于抽象化复杂性并减少代码大小。大约有800,000多个现成的代码包可用,每个代码包提供不同的功能。NPM是一种库,其中包含所有这些软件包以支持Node.js应用程序开发
But, if you don’t know exactly which package will serve your purpose best, it will be the same as finding a needle in the hay. Thus, to give you a little push, below I have listed down the most popularly used libraries of NPM:
但是,如果您不确切知道哪种包最能满足您的目的,那么就像是在干草堆中如针扎一样。因此,为了给您带来一点推动力,下面我列出了最常用的NPM库
But before you can use any of these packages you need to install NPM in your system.
In the next section of this Node.js NPM tutorial, I will be sharing how to install NPM and various packages.
NPM Installation
Node.js version 0.6.3 onwards, NPM was included as a default package in Node.js. Thus, there is no need to install it explicitly. To know how to install Node.js in your system, you can refer to my Node.js Installation article.
In order to check whether NPM is already there in your system or not, you just need to type in the below command.
npm -v
Now that installing NPM is out of the way, let me show you how to install, update and delete packages using NPM CLI (Command Line Interface). But before you start installing or downloading your packages, one thing you need to know is the packages can be installed in two ways, about which I will be talking in the next section of this Node.js NPM Tutorial.
Local and Global Packages
As I have already mentioned, the packages are categorized under two categories depending on their mode of installation:
- Local Packages
- Global Packages
1. Local Packages
These are the packages which are installed within the directory where you will be executing the install command and will be accessible by only your project. Local packages are contained by a node_modules folder under your main project directory.
Below is the command to install packages locally:
npm install <package-name>
2. Global Packages
These are the packages which are installed at a single place in your system irrespective of the place where you execute your run command. They are called global packages as they can be used by any of the projects present in your system. To install a package globally, you need the below-given command:
npm install -g <package-name>
Few of the most used global packages are:
- npm
- create-react-app
- vue-cli
- grunt-cli
- mocha
- react-native-cli
- gatsby-cli
- forever
- nodemon
The major difference between local and global packages is that the Global packages are used for anything that is needed to be accessed from the shell. On the other hand, the usage of local packages is typically limited to your applications or projects. But in general, it is a good practice to install the packages locally. This is because you might be having a number of Node.js projects in your system having a different version of each of the packages used in them.
本地程序包和全局程序包之间的主要区别在于,全局程序包用于需要从外壳程序访问的所有内容。另一方面,本地软件包的使用通常仅限于您的应用程序或项目。但是通常,在本地安装软件包是一个好习惯。这是因为您的系统中可能有许多Node.js项目,它们中使用的每个软件包都有不同的版本
Now, if you update a global package then it will update it in all the projects it has been used in your system. It can cause a huge disaster as few of these packages might become incompatible with the further dependencies used in the projects. But in case of local packages, you can always keep your version of a package and update it according to your own needs. In terms of resource utilization, it might seem like a waste of memory but it has relatively less negative impacts.
现在,如果您更新全局软件包,它将在系统中使用过的所有项目中对其进行更新。由于其中一些软件包可能与项目中使用的其他依赖项不兼容,因此可能会造成巨大的灾难。但是对于本地软件包,您始终可以保留软件包的版本并根据自己的需要进行更新。在资源利用率方面,这似乎是浪费内存,但负面影响相对较小
Thus, you should make a package global only when it has a CLI executable command and can be reused throughout the projects in your system.
因此,仅当软件包具有CLI可执行命令并且可以在系统中的所有项目中重复使用时,才应使其成为全局软件包
You can also check how many global packages you have in your system by typing in the below command.
您还可以通过输入以下命令来检查系统中有多少个全局软件包
npm list -g --depth 0
In case you want to remove a package from your system you can just type in the below command:
如果您想从系统中删除软件包,只需输入以下命令
npm uninstall <package_name>
Here I would like to slip in a piece of advice, that is whenever you are installing the packages make sure you include –save flag with it.
在这里,我想提一点建议,那就是每当您安装软件包时,请确保在其中包含–save标志
npm install <package_name> --save
It will ensure that your requested module has been added your package.json file.
它将确保您请求的模块已添加到package.json文件中
Now, what is package.json and why it is needed, I will tell you in the next section of this Node.js NPM Tutorial.
Package.json File
The package.json file in Node.js is considered to be the heart of an application. It is nothing but the manifest file that holds the metadata of the project. Thus, it is very important to understand this file in order to work a Node project. The package.json file is usually present in the root folder of any Node.js application and looks like the below.
Node.js中的package.json文件被认为是应用程序的核心。只是清单文件包含项目的元数据。因此,理解此文件对于工作Node项目非常重要。package.json文件通常位于任何Node.js应用程序的根文件夹中,如下所示
{
"name": "samplenode",
"version": "1.0.0",
"description": "Unity demo on how to build a Node.js application",
"main": "script.js",
"scripts": {
"start": "node script.js"
},
"author": "Unity",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-handlebars": "^4.0.4",
"nodemon": "^2.0.4"
},
"devDependencies": {},
"repository": {},
"bugs": {}
}
As you can see in the above code, package.json file holds definitions of various properties used in a project. Let us understand each of these properties in detail and see why they are important in a Node.js project:
如您在上面的代码中看到的,package.json文件保存了项目中使用的各种属性的定义。让我们详细了解这些属性中的每一个,并了解它们为什么在Node.js项目中很重要:
Name: Holds the name of the project provided by the user
名称: 保存用户提供的项目的名称
Version: Depicts the version of the application and must follow the semantic version rules.
版本: 描述应用程序的版本,并且必须遵循语义版本控制规则
Description: Holds the explanation & purpose of the application, technologies used like React, MongoDB, etc.
描述: 包含应用程序的解释和目的,以及React,MongoDB等使用的技术
Main: Points to the entry point/file of the application
Main: 指向应用程序的入口点/文件
Scripts: Contains the list of scripts which are required to be included in the application to execute properly
脚本: 需要正确包含应用程序中的脚本列表
Author: Holds the name of the primary developer of the project
作者: 拥有项目主要开发人员的姓名
License: License to which the application confirms are mentioned in this key-value pair
许可证: 此键值对中提到了应用程序确认时的许可证
Dependencies: Depicts the list of 3rd Party packages or modules installed using NPM
依赖关系: 描述使用NPM安装的第三方包或模块的列表
DevDependencies: Dependencies that are used only in the development part of the app are specified here
DevDependencies: 在此指定仅在应用程序的开发部分中使用的依赖项
Repository: Contains the information regarding the type & URL of the repository where the code of the application resides
包含有关应用程序代码所在的存储库的类型和URL的信息
Bugs: URL and email where the bugs in the application should be reported are mentioned here
在此处提到报告应用程序中的错误的URL和电子邮件
With this, we come to an end of this Node.js NPM Tutorial. Hope you were able to understand what exactly is NPM and how it helps in building a Node.js application. Now you can get your hands dirty with Node.js. To get started, you can refer my Node.js Tutorial article.
Node.js NPM Tutorial的更多相关文章
- Node.js NPM Tutorial: Create, Publish, Extend & Manage
A module in Node.js is a logical encapsulation of code in a single unit. It's always a good programm ...
- Latest node.js & npm installation on Ubuntu 12.04
转自:https://rtcamp.com/tutorials/nodejs/node-js-npm-install-ubuntu/ Compiling is way to go for many b ...
- Node.js npm
Node程序包管理器(NPM)提供了以下两个主要功能: 在线存储库的Node.js包/模块,可搜索 search.nodejs.org 命令行实用程序来安装Node.js的包,做版本管理和Node.j ...
- 自制node.js + npm绿色版
自制node.js + npm绿色版 Node.js官网有各平台的安装包下载,不想折腾的可以直接下载安装,下面说下windows平台下如何制作绿色版node,以方便迁移. 获取node.exe下载 ...
- Mac 下搭建环境 homebrew/git/node.js/npm/vsCode...
主要记录一下 homebrew/git/node.js/npm/mysql 的命令行安装 1. 首先安装 homebrew 也是一个包管理工具: mac 里打开终端命令行工具,粘下面一行回车安装br ...
- Node.js npm基础安装配置&创建第一个VUE项目
使用之前,我们先来明白这几个东西是用来干什么的. node.js: 一种javascript的运行环境,能够使得javascript脱离浏览器运行.Node.js的出现,使得前后端使用同一种语言,统一 ...
- Node.js NPM Package.json
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json Nod ...
- Node.js NPM 包(Package)
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json 包是打 ...
- Node.js NPM 管理包
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json 根据安 ...
随机推荐
- element-ui 通用表单封装及VUE JSX应用
一.存在及需要解决的问题 一般在做后台OA的时候会发现表单重复代码比较多,且逻辑基本一样,每次新加一个表单都需要拷贝基本一致的代码结构,然后只是简单地修改对应的字段进行开发 二.预期结果 提取重复的表 ...
- 关于Python的JSON
1.json模块load/loads.dump/dumps区别:(摘自这里) 实际上json就是python字典的字符串表示,但是字典作为一个复杂对象是无法直接转换成定义它的代码的字符串,python ...
- python基础入门教程(一条龙服务)
一.语言基础 01-1 计算机系统 解释器下载 变量 小整数池 01-2 垃圾回收机制 02 数据类型 运算符(解压赋值等) 03 流程控制 if while for 04 整形 字符串 列表 0 ...
- 基于tp3开发的cms-前台代码执行
内核 thinkphp PbootCMS-V1.2.1 ├─ apps 应用程序 │ ├─ admin 后台模块 │ ├─ api api模块 │ ├ ...
- 常用设计模式的实现,以及Netty中的设计模式
1.观察者模式 有两个角色,观察者和被观察者.当被观察者发出消息后,注册了的观察者会收到其消息,而没有注册的观察者就不会收到. //定义观察者接口 interface Observer{ //通知观察 ...
- C. Two Arrays(思维DP或组合数学)
\(首先很容易想到一个O(n^4m)的DP\) \(设dp\ [i]\ [j]\ [q]\ 为长度i,a数组以j结尾,b数组以q结尾(q>=j)\) for(int i=1;i<=n;i+ ...
- B - Save the problem! CodeForces - 867B 构造题
B - Save the problem! CodeForces - 867B 这个题目还是很简单的,很明显是一个构造题,但是早训的时候脑子有点糊涂,想到了用1 2 来构造, 但是去算这个数的时候算错 ...
- rsync客户端一键安装rsync脚本(源码)
客户端 read -np "请输入源码rsync的URL 地址 包名(以空格为分隔符,别带/):" URL DZ BM yum remove -y rsync &>& ...
- [UVA Live 12931 Common Area]扫描线
题意:判断两个多边形是否有面积大于0的公共部分 思路:扫描线基础. #pragma comment(linker, "/STACK:10240000") #include < ...
- [zoj3591]Nim 游戏
题意:有n堆火柴,选择连续若干堆火柴进行Nim游戏,求让先手胜的选择方案数. 思路:让先手胜等同于这些数的异或值不同于0,不妨转化为求让先手败的方案数.此时记录一个前缀的异或和val[i],那么答案就 ...