• day2:Node.js 环境准备


    day2:Node.js 环境准备

    准备一台linux服务器

    [root@node3 ~]# cat /etc/redhat-release
    CentOS Linux release 7.2.1511 (Core)
    
    • 1
    • 2
    安装 Node.js 和 npm
    1. 打开终端并使用root权限登录系统。
    2. 执行以下命令,安装Node.js相关的依赖软件包:
    yum install -y gcc gcc-c++ openssl-devel
    
    • 1
    1. 执行以下命令,从Node.js官方网站下载适用于CentOS的Node.js软件包:
    curl -sL https://rpm.nodesource.com/setup_16.x | bash
    
    • 1
    1. 安装Node.js软件包:
     yum install -y nodejs
    
    • 1
    验证 Node.js 和 npm
    [root@node3 ~]# node -v
    v16.18.1
    
    • 1
    • 2
    [root@node3 ~]# npm -v
    8.19.2
    
    • 1
    • 2
    使用淘宝 NPM 镜像

    由于国内直接使用 npm 的官方镜像是非常慢的,这里推荐使用淘宝 NPM 镜像。

    淘宝 NPM 镜像是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步。

    你可以使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:

    $ npm install -g cnpm --registry=https://registry.npmmirror.com
    
    • 1

    这样就可以使用 cnpm 命令来安装模块了:

    $ cnpm install [name]
    
    • 1
    npm 包管理器的基本使用

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:

    • 允许用户从NPM服务器下载别人编写的第三方包到本地使用。
    • 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
    • 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。

    NPM安装模块

    以下实例,我们使用 npm 命令安装常用的 Node.js web框架模块 express:

    $ mkdir nodejs
    $ cd nodejs
    $ npm install express
    
    • 1
    • 2
    • 3
    [root@node3 nodejs]# ll 
    total 108
    drwxr-xr-x 69 root root  4096 Oct 12 14:04 node_modules
    
    • 1
    • 2
    • 3

    全局安装与本地安装

    npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如

    npm install express          # 本地安装
    npm install express -g   # 全局安装
    
    • 1
    • 2

    如果出现以下错误:

    npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 
    
    • 1

    解决办法为:

    $ npm config set proxy null
    
    • 1

    本地安装

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

    全局安装

      1. 将安装包放在 /usr/local 下或者你 node 的安装目录。
      1. 可以直接在命令行里使用。

    查看安装信息

    你可以使用以下命令来查看所有全局安装的模块:

    [root@node3 nodejs]# npm list express -g
    /usr/local/lib
    └── (empty)
    
    
    • 1
    • 2
    • 3
    • 4

    如果要查看某个模块的版本号,可以使用命令如下:

    [root@node3 nodejs]# npm list express
    helloworld@1.0.0 /root/nodejs
    └── express@4.18.2
    
    • 1
    • 2
    • 3

    使用 package.json

    package.json 位于模块的目录下,用于定义包的属性。接下来让我们来看下 express 包的 package.json 文件,位于 node_modules/express/package.json 内容:

    [root@node3 nodejs]# cat package.json 
    {
      "dependencies": {
        "express": "^4.18.2",
        "mysql": "^2.18.1"
      },
      "scripts": {
        "start": "node server.js"
      },
      "name": "helloworld",
      "version": "1.0.0",
      "main": "helloworld.js",
      "keywords": [
        "helloworld"
      ],
      "author": "xiaohaibing",
      "license": "ISC",
      "description": ""
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Package.json 属性说明

    • name - 包名。
    • version - 包的版本号。
    • description - 包的描述。
    • homepage - 包的官网 url 。
    • author - 包的作者姓名。
    • contributors - 包的其他贡献者姓名。
    • dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
    • repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
    • main - main 字段指定了程序的主入口文件,require(‘moduleName’) 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。
    • keywords - 关键字

    NPM卸载模块

    我们可以使用以下命令来卸载 Node.js 模块。

    $ npm uninstall express
    
    • 1

    卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看:

    $ npm ls
    
    • 1

    NPM更新模块

    我们可以使用以下命令更新模块:

    $ npm update express
    
    • 1

    NPM搜索模块

    使用以下来搜索模块:

    $ npm search express
    
    • 1

    NPM创建模块

    创建模块,package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的结果。

    $ 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  --save` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    name: (node_modules) runoob                   # 模块名
    version: (1.0.0) 
    description: Node.js 测试模块(www.runoob.com)  # 描述
    entry point: (index.js) 
    test command: make test
    git repository: https://github.com/runoob/runoob.git  # Github 地址
    keywords: 
    author: 
    license: (ISC) 
    About to write to ……/node_modules/package.json:      # 生成地址
    
    {
      "name": "runoob",
      "version": "1.0.0",
      "description": "Node.js 测试模块(www.runoob.com)",
      ……
    }
    
    
    Is this ok? (yes) yes
    
    • 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
    • 27
    • 28
    • 29
    • 30
    • 31

    以上的信息,你需要根据你自己的情况输入。在最后输入 “yes” 后会生成 package.json 文件。

    NPM发布模块

    接下来我们可以使用以下命令在 npm 资源库中注册用户(使用邮箱注册):

    $ npm adduser
    Username: mcmohd
    Password:
    Email: (this IS public) mcmohd@gmail.com
    
    • 1
    • 2
    • 3
    • 4

    接下来我们就用以下命令来发布模块:

    $ npm publish
    
    • 1
    NPM 常用命令

    NPM提供了很多命令,例如install和publish,使用npm help可查看所有命令。

    • NPM提供了很多命令,例如installpublish,使用npm help可查看所有命令。
    • 使用npm help 可查看某条命令的详细帮助,例如npm help install
    • package.json所在目录下使用npm install . -g可先在本地安装当前命令行程序,可用于发布前的本地测试。
    • 使用npm update 可以把当前目录下node_modules子目录里边的对应模块更新至最新版本。
    • 使用npm update -g可以把全局安装的对应命令行程序更新至最新版。
    • 使用npm cache clear可以清空NPM本地缓存,用于对付使用相同版本号发布新版本代码的人。
    • 使用npm unpublish @可以撤销发布自己发布过的某个版本代码。
    小结

    如果你遇到了使用 npm 安 装node_modules 总是提示报错:报错: npm resource busy or locked…

    可以先删除以前安装的 node_modules :

    npm cache clean
    npm install
    
    • 1
    • 2

    在这里插入图片描述

  • 相关阅读:
    2022 深度学习 & 计算机视觉 & 感知算法 面经整理 二十三(221 222 223 224 225 226 227 228 229 230)
    计算机毕业设计springboot+vue基本微信小程序的学习资料共享小程序
    Java IO流详解
    力扣labuladong——一刷day35
    Python 爬虫实战 | 利用多线程爬取 LOL 高清壁纸
    【第一阶段:java基础】第7章:面向对象编程中级-3(P319-P333):Object类详解、断点调试
    关于 registerForActivityResult()的使用方法,不能说详细,只能说略懂得例子
    评估和选择最佳学习模型的一些指标总结
    2022第二届中国高校大数据竞赛A题思路
    黑马苍穹外卖前端求解
  • 原文地址:https://blog.csdn.net/RodJohnsonDoctor/article/details/133885831