Mac NPM 配置



本文已停止更新,点击此链接查看本文最新内容 !!!



1、NPM 简介

  • NPM(node package manager),通常称为 node 包管理器,是目前世界上最大的开源库生态系统。使用 NPM 可以对 node 包进行安装、卸载、更新、查看、搜索、发布等操作。安装完 Node.js®,NPM 就可以直接用了。

2、NPM 环境配置

2.1 安装 Node.js

2.2 安装 NPM

  • 安装完 Node.js,NPM 就可以直接用了。

2.3 NPM 常用命令

  • 1)NPM 常用命令

    # 查看 NPM 版本
    $ npm -v
    
    # 更新 NPM 版本
    $ sudo npm i -g npm
    
    # 在官方包仓库中搜索包
    $ npm search [包名]
    $ npm search express
    
    # 查看官方包仓库中包所用 package.json 文件中的信息
    $ npm view [包名]
    $ npm view express
    
    # 安装 package.json 文件中列出的包
    $ npm install
    
    # 安装包
    
        # 安装在本地项目中
        $ npm install [包名]
        $ npm install express
    
        # 安装在全局中
        $ npm install -g [包名]
        $ npm install -g express
        
    # 安装并保存包到 package.json 文件中
    
        # 安装在本地项目中
        $ npm install [包名] --save
        $ npm install express --save
    
        # 安装在全局中
        $ npm install -g [包名] --save
        $ npm install -g express --save
    
    # 查看安装的包
        
        # 本地
        $ npm list
        
        # 全局
        $ npm list -g  
        
    # 查看过期的包
        
        # 本地
        $ npm outdated  
        
        # 全局
        $ npm outdated -g
    
    # 更新全部包
        
        # 本地
        $ npm update
        
        # 全局
        $ npm update -g
    
    # 更新指定的包
        
        # 本地
        $ npm update [包名]
        $ npm update express 
    
        # 全局
        $ npm update -g [包名]
        $ npm update -g express 
    
    # 卸载包
        
        # 本地
        $ npm uninstall [包名]
        $ npm uninstall express
        
        # 全局
        $ npm uninstall -g [包名]
        $ npm uninstall -g express
    
    # 查看全局包的安装路径
    $ npm root -g
    
    # 修改全局包的安装路径,修改后为 [安装路径]
    ode_modules
    $ npm config set prefix "[安装路径]"
    
    Usage: npm <command>
    
    where <command> is one of:
        access, adduser, bin, bugs, c, cache, completion, config,
        ddp, dedupe, deprecate, dist-tag, docs, doctor, edit,
        explore, get, help, help-search, i, init, install,
        install-test, it, link, list, ln, login, logout, ls,
        outdated, owner, pack, ping, prefix, profile, prune,
        publish, rb, rebuild, repo, restart, root, run, run-script,
        s, se, search, set, shrinkwrap, star, stars, start, stop, t,
        team, test, token, tst, un, uninstall, unpublish, unstar,
        up, update, v, version, view, whoami
    
    npm <command> -h     quick help on <command>
    npm -l           display full usage info
    npm help <term>  search for help on <term>
    npm help npm     involved overview
    
    Specify configs in the ini-formatted file:
        /Users/qianchia/.npmrc
    or on the command line via: npm <command> --key value
    Config info can be viewed via: npm help config
    

2.4 注意事项

  • 一般 Node modules 通常被安装在每个项目的本地文件夹 node_modules 中,但下面几个包推荐安装在全局:

    • CoffeeScriptLessGruntGulp
    # 安装 coffee-script
    $ npm install -g coffee-script
    
    # 安装 less
    $ npm install -g less
    
    # 安装 grunt-cli
    $ npm install -g grunt-cli
    
    # 安装 gulp
    $ npm install -g gulp
    

3、CNPM 配置

3.1 CNPM 简介

  • 因为 npm 安装插件是从国外服务器下载,受网络影响大,可能出现异常,如果 npm 的服务器在中国就好了,所以淘宝团队干了这事。来自官网:“这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10 分钟一次以保证尽量与官方服务同步。”

  • cnpm 跟 npm 用法完全一致,只是在执行命令时将 npm 改为 cnpm。

3.2 CNPM 环境配置

  • 在终端中输入以下命令安装 CNPM。

    # 安装 CNPM
    $ npm install -g cnpm --registry=https://registry.npm.taobao.org
    
  • 安装完后最好查看其版本号 cnpm -v 或重启终端,安装完直接使用有可能会出现错误。

原文地址:https://www.cnblogs.com/QianChia/p/9116805.html