node.js 之 npm

2018-12-29  本文已影响0人  那年角落的喇叭

什么是npm

npm 是(node package manager)的简称。node包管理工具
下面是官网的介绍:
Use npm to install, share, and distribute code; manage dependencies in your projects; and share & receive feedback with others.
如果你想更进一步了解npm 可以看这里 https://docs.npmjs.com/about-npm/

为什么要用npm

  1. 可以快速引用其他开发者的开源代码,使用优秀的开源模块,可以加快开发速度。
  2. 在协同开发的过程中可以容易管理引用模块,例如有一个项目需要引进多个包,以往的做法是将引进的包放在项目中,这样就会导致项目较大,从一个开发者传给另一开发者,还要另一个开发者放到项目里,操作起来不方便。
  3. 最大程度的减少代码量,利于开源。

npm怎么用

开发环境是Win 10 + 开发工具是VS code + node.js
如果你是整个项目的创建者,你可以使用以下步骤创建你的项目。

cd .\GitLab8\  
mkdir node_app
cd node_app
PS C:\GitLab8\node_app> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (node_app) my_first_node_project
version: (1.0.0) 0.0.1
description: a new demo for beginner
entry point: (index.js)
test command: dev
git repository:
keywords:
author: nanian
license: (ISC)
About to write to C:\GitLab8\node_app\package.json:

{
  "name": "my_first_node_project",
  "version": "0.0.1",
  "description": "a new demo for beginner",
  "main": "index.js",
  "scripts": {
    "test": "dev"
  },
  "author": "nanian",
  "license": "ISC"
}


Is this OK? (yes) yes
Aborted.

接下来对初始化过程中的参数进行说明

  1. package name,这是包名,也是你项目的名字
  2. version, 项目版本
  3. description,对项目的描述
  4. entry point , 项目的入口
  5. test command, 使用npm 启动项目的命令,比如这里启动项目就用以下命令 npm run dev
  6. git repository:之后再补充
  7. keywords: 之后再补充
  8. author: 项目的作者
  9. license: (ISC) 之后再补充

到这里一个node项目就创建完成了。

上一篇 下一篇

猜你喜欢

热点阅读