• Impedit odio facilis ad.Quinze également passer billet mensonge animer libre.


    1. Node 笔记

    更详细: https://www.runoob.com/nodejs/nodejs-npm.html

    1.1. 安装

    1.1.1. CentOS 7 安装

    因为 CentOS 7 的 C 运行库版本很低,Node 官网编译用的版本很高,所以只能用第三方编译的版本,地址如下(当然也可以自己做个 docker 镜像专门用于编译):

    https://github.com/sbwml/node-latest-centos/releases/tag/v18.16.0

    1.2. 安装模块

    npm install xxx 利用 npm 安装 xxx 模块到当前命令行所在目录; 
    npm install -g xxx 利用 npm 安装全局模块 xxx; 
    
    • 1
    • 2

    本地安装时将模块写入 package.json 中:

    npm install xxx 安装但不写入 package.json; 
    npm install xxx –save 安装并写入 package.json 的 "dependencies" 中; 
    npm install xxx –save-dev 安装并写入 package.json 的 "devDependencies" 中。
    
    • 1
    • 2
    • 3

    1.3. 删除模块

    npm uninstall xxx 删除 xxx 模块
    npm uninstall -g xxx 删除全局模块 xxx
    
    • 1
    • 2

    1.4. 升级模块

    推荐使用升级插件:

    $ npm install -g npm-check-updates
    // 或者
    $ cnpm install -g npm-check-updates
    
    • 1
    • 2
    • 3

    ncu 是 npm-check-updates 的缩写命令。

    可以看到有好几个包要更新

    $ ncu -a # 更新
    $ ncu # 查看更新
    
    • 1
    • 2

    npm 自带的 npm update 和升级插件 npm-check-updates 有什么区别呢

    npm update, 只能按照 package.js 中标注的版本号进行更新, 每次都要改动 package.js 中的版本号实在太麻烦。
    而升级插件会自动帮你更改 package.js 里的版本号, 简单方便。

    1.5. 清理模块

    npm 清除未被使用的模块命令:

    npm prune
    
    • 1

    1.6. 设置淘宝镜像

    由于网速问题, 我们需要设置一个国内镜像, 目前好像就淘宝一家了, 其它全关了。

    http://npm.taobao.org 和 http://registry.npm.taobao.org 将在 2022.06.30 号正式下线和停止 DNS 解析。

    域名切换规则:

    • http://npm.taobao.org => http://npmmirror.com
    • http://registry.npm.taobao.org => http://registry.npmmirror.com
    # 切换为淘宝镜像命令
    $ npm config set registry http://registry.npmmirror.com
    
    # 验证是否设置成功
    $ npm config get registry
    
    # 删除 / 还原镜像
    $ npm config del registry
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.6.1. cnpm

    淘宝推荐使用他们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:

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

    或者你直接通过添加 npm 参数 alias 一个新命令:

    alias cnpm="npm --registry=https://registry.npmmirror.com \
    --cache=$HOME/.npm/.cache/cnpm \
    --disturl=https://npmmirror.com/mirrors/node \
    --userconfig=$HOME/.cnpmrc"
    
    # Or alias it in .bashrc or .zshrc
    $ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npmmirror.com \
      --cache=$HOME/.npm/.cache/cnpm \
      --disturl=https://npmmirror.com/mirrors/node \
      --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 安装模块
    $ cnpm install [name]
    
    • 1
    • 同步模块

    直接通过 sync 命令马上同步一个模块, 只有 cnpm 命令行才有此功能:

    $ cnpm sync express
    
    • 1

    当然, 你可以直接通过 web 方式来同步: /sync/express

    $ open https://npmmirror.com/sync/express
    
    • 1
    • 其它命令

    支持 npm 除了 publish 之外的所有命令, 如:

    $ cnpm info express
    
    • 1

    1.7. 设置代理

    设置代理:

    npm config set proxy http://server:port
    npm config set https-proxy http://server:port
    
    # 如果代理需要认证的话可以这样来设置: 
    npm config set proxy http://username:password@server:port
    npm config set https-proxy http://username:pawword@server:port
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    查看代理设置:

    npm config list
    
    • 1

    清除 npm 的代理:

    npm config delete proxy
    npm config delete https-proxy
    
    • 1
    • 2

    1.8. 问题 / 报错

    1.8.1. phantomjs 这个 npm 包已经废弃, 不再使用了

    根据 npm phantomjs 包官网 所述:

    Package renamed to phantomjs-prebuilt. Please update 'phantomjs' package references to 'phantomjs-prebuilt'
    
    • 1

    不知道从哪里修改包的引用, 所以干脆就到 phantomjs 官网 去下一个对应当前操作系统的版本, 并且添加到 PATH 中即可, 下次其它的软件就不会自动去下载并编译了。

  • 相关阅读:
    HTTP 网络协议请求的消息结构,具体详解(2024-04-25)
    力扣(LeetCode)18. 四数之和(C++)
    小说阅读软件阅读界面设计
    Springboot容器化设置堆内存大小
    【算法leetcode】2315. 统计星号(rust和go重拳出击)
    vue3+vite+windicss+element-plus+axios+router+cookies 搭建
    Android入门第24天-Adapter使用初步-最简单的一个Adapter的使用
    平行光场:基本框架与流程
    springboot+vue+elementUI基于SpringBoot的冬奥会科普平台#毕业设计
    Android环境变量&macOS环境变量配置
  • 原文地址:https://blog.csdn.net/wan212000/article/details/128200552