Node.js之路【第二篇】Nodejs中的pip(NPM)&REPL

时间:2022-09-06 16:26:41

什么是NPM

在学Python的时候我们肯定会使用第三方模块或者编写模块供别人使用,我们有一个非常好用的pip来帮我们管理我们的模块包!那么Nodejs重的模块包呢?

对没错就是NPM,他是随同NodeJS一起安装的包管理工具:(和python中的pip类似

  1. 允许用户从NPM服务器下载别人编写的第三方包到本地使用
  2. 允许从用户从NPM服务器下并安装别人编写的命令程序到本地使用
  3. 语序用户辨析自己的包或命令程序到NPM服务器供别人使用

由于新版本的NodeJS已经集成了npm所以我们安装完NodeJS之后可以通过下面的命令去查看npm版本

LuoTimdeMacBook-Pro-2:~ luotim$ npm -v
2.15.1

如果你安装的是旧版本的npm,可以很容易的通过npm去升级

#LuoTimdeMacBook-Pro-2:~ luotim$ sudo npm install npm -g
Password:
Sorry, try again.
Password:
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm@3.9.2 /usr/local/lib/node_modules/npm
LuoTimdeMacBook-Pro-2:~ luotim$ npm -v
#3.9.2

使用npm命令安装模块

一、安装命令

npm安装NodeJS模块语法格式如下:

npm install <Module Name>

安装一下NodeJS web框架模块express

sudo npm install express

安装好之后,express包就放在工程目录下面的node_modules目录中了,调用的时候通过require('express')的方式调用。

Node.js之路【第二篇】Nodejs中的pip(NPM)&REPL

#在程序中调用

var express = require('express');

二、全局安装与本地安装

本地安装:

npm install express
  1. 将包放在./node_modules下(运行npm install 的目录下),如果没有node_modules目录,会在当前执行npm命令的目录下生成node_modules目录。
  2. 可以通过require()来导入本地安装的模块

全局安装:

npm install express -g
  1. 将安装包放在/usr/local下
  2. 可以直接通过命令行调用
  3. 不能通过require()来导入模块

查看软件包:

#查看本地安装的本地模块要进入到/node_modules/目录下
npm ls

#查看全局安装的模块
npm ls -g

三、package.json

每个模块下面都会有一个package.json文件,我们看下express保重的文件内容,他里面存放了关于这个模块的相关信息

LuoTimdeMacBook-Pro-2:express luotim$ cat package.json
{
  "_args": [
    [
      {
        "name": "express",
        "raw": "express",
        "rawSpec": "",
        "scope": null,
        "spec": "latest",
        "type": "tag"
      },
      "/private/tmp"
    ]
  ],
  "_from": "express@latest",
  "_id": "express@4.13.4",
  "_inCache": true,
  "_installable": true,
  "_location": "/express",
  "_npmUser": {
    "email": "doug@somethingdoug.com",
    "name": "dougwilson"
  },
  "_npmVersion": "1.4.28",
  "_phantomChildren": {},
  "_requested": {
    "name": "express",
    "raw": "express",
    "rawSpec": "",
    "scope": null,
    "spec": "latest",
    "type": "tag"
  },
  "_requiredBy": [
    "#USER"
  ],
  "_resolved": "https://registry.npmjs.org/express/-/express-4.13.4.tgz",
  "_shasum": "3c0b76f3c77590c8345739061ec0bd3ba067ec24",
  "_shrinkwrap": null,
  "_spec": "express",
  "_where": "/private/tmp",
  "author": {
    "email": "tj@vision-media.ca",
    "name": "TJ Holowaychuk"
  },
  "bugs": {
    "url": "https://github.com/expressjs/express/issues"
  },
  "contributors": [
    {
      "email": "aaron.heckmann+github@gmail.com",
      "name": "Aaron Heckmann"
    },
    {
      "email": "ciaranj@gmail.com",
      "name": "Ciaran Jessup"
    },
    {
      "email": "doug@somethingdoug.com",
      "name": "Douglas Christopher Wilson"
    },
    {
      "email": "rauchg@gmail.com",
      "name": "Guillermo Rauch"
    },
    {
      "email": "me@jongleberry.com",
      "name": "Jonathan Ong"
    },
    {
      "email": "shtylman+expressjs@gmail.com",
      "name": "Roman Shtylman"
    },
    {
      "email": "hanul@hanul.me",
      "name": "Young Jae Sim"
    }
  ],
  "dependencies": {
    "accepts": "~1.2.12",
    "array-flatten": "1.1.1",
    "content-disposition": "0.5.1",
    "content-type": "~1.0.1",
    "cookie": "0.1.5",
    "cookie-signature": "1.0.6",
    "debug": "~2.2.0",
    "depd": "~1.1.0",
    "escape-html": "~1.0.3",
    "etag": "~1.7.0",
    "finalhandler": "0.4.1",
    "fresh": "0.3.0",
    "merge-descriptors": "1.0.1",
    "methods": "~1.1.2",
    "on-finished": "~2.3.0",
    "parseurl": "~1.3.1",
    "path-to-regexp": "0.1.7",
    "proxy-addr": "~1.0.10",
    "qs": "4.0.0",
    "range-parser": "~1.0.3",
    "send": "0.13.1",
    "serve-static": "~1.10.2",
    "type-is": "~1.6.6",
    "utils-merge": "1.0.0",
    "vary": "~1.0.1"
  },
  "description": "Fast, unopinionated, minimalist web framework",
  "devDependencies": {
    "after": "0.8.1",
    "body-parser": "~1.14.2",
    "connect-redis": "~2.4.1",
    "cookie-parser": "~1.4.1",
    "cookie-session": "~1.2.0",
    "ejs": "2.3.4",
    "express-session": "~1.13.0",
    "istanbul": "0.4.2",
    "jade": "~1.11.0",
    "marked": "0.3.5",
    "method-override": "~2.3.5",
    "mocha": "2.3.4",
    "morgan": "~1.6.1",
    "multiparty": "~4.1.2",
    "should": "7.1.1",
    "supertest": "1.1.0",
    "vhost": "~3.0.1"
  },
  "directories": {},
  "dist": {
    "shasum": "3c0b76f3c77590c8345739061ec0bd3ba067ec24",
    "tarball": "https://registry.npmjs.org/express/-/express-4.13.4.tgz"
  },
  "engines": {
    "node": ">= 0.10.0"
  },
  "files": [
    "LICENSE",
    "History.md",
    "Readme.md",
    "index.js",
    "lib/"
  ],
  "gitHead": "193bed2649c55c1fd362e46cd4702c773f3e7434",
  "homepage": "https://github.com/expressjs/express",
  "keywords": [
    "express",
    "framework",
    "sinatra",
    "web",
    "rest",
    "restful",
    "router",
    "app",
    "api"
  ],
  "license": "MIT",
  "maintainers": [
    {
      "email": "doug@somethingdoug.com",
      "name": "dougwilson"
    }
  ],
  "name": "express",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/expressjs/express.git"
  },
  "scripts": {
    "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/",
    "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/",
    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/",
    "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/"
  },
  "version": "4.13.4"
}
LuoTimdeMacBook-Pro-2:express luotim$ 

简单解释:

  • name - 包名。
  • version - 包的版本号。
  • description - 包的描述。
  • homepage - 包的官网 url 。
  • author - 包的作者姓名。
  • contributors - 包的其他贡献者姓名。
  • dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
  • repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
  • main - main 字段是一个模块ID,它是一个指向你程序的主要项目。就是说,如果你包的名字叫 express,然后用户安装它,然后require("express")。
  • keywords - 关键字

四、卸载、更新、搜索、创建模块

卸载

npm uninstall express

更新

npm update express

搜索

npm search express

创建模块

NodeJS REPL(交互解释器)

和python的交互解释器类似NodeJS也停工了交互式解释器REPL

一、REPL功能

有下面的功能:

  • 读取用户输入,解析输入了JavaScript数据结构并存储在内存中
  • 执行输入的数据结构
  • 打印结果
  • 循环

直接输入node来启动Node的终端

LuoTimdeMacBook-Pro-2:express luotim$ node
> 

1、表达式运算

LuoTimdeMacBook-Pro-2:express luotim$ node
> 1 + 1
2
> 1 * 5 + 6 / 8
5.75
> 

2、变量

变量声明需要使用var关键字,如果不使用var关键字变量会被打印出来,并且是使用var关键字的变量可以使用console.log()来输出变量

> x = 10
10
> console.log(x)
10
undefined
> var y = 10
undefined
> console.log(y)
10
undefined
> 

3、多行表达式

> var x = 0;
undefined
> do {
... x++;
... console.log('Thes number x now is : ' + x);
... } while (x<6);
Thes number x now is : 1
Thes number x now is : 2
Thes number x now is : 3
Thes number x now is : 4
Thes number x now is : 5
Thes number x now is : 6
undefined
> 

4、下划线变量

可以通过使用下划线‘_'获取表达式的运算结果

> var x = 10
undefined
> var y = 15
undefined
> x + y
25
> var sum = _
undefined
> console.log(sum)
25
undefined
> 

退出REPL

连续两次Ctrl + c 或者Ctrl + D