Node.js - npm 与cnpm配置(Windows10)

2019-12-10  本文已影响0人  Dozing

一、安装node.js

1.前往node.js官网下载并安装工具,这里安装路径选到D盘,D:\software\nodejs

安装完毕后在命令行输入以下命令测试是否安装成功,正确会出现版本号

npm -v

2.改变原有的环境变量,

(1)配置npm的全局模块的存放路径以及cache的路径,例如两个文件夹放在NodeJS的主目录下,便在NodeJs下建立node_globalnode_cache两个文件夹,输入以下命令改变npm配置

npm config set prefix "D:\software\nodejs\node_global"
npm config set cache "D:\software\nodejs\node_cache"

(2)在命令行输入npm list -global,可以查看目录是否已经改变

D:\software\nodejs\node_global

(3)在系统环境变量添加系统变量NODE_PATH,输入路径D:\software\nodejs\node_global\node_modules,此后所安装的模块都会安装到改路径下

(4)在命令行输入以下命令试着安装express(注:-g这个参数意思是装到global目录下,也就是上面说设置的D:\software\nodejs\node_global里面。)

npm install express -g

安装完毕后可以看到.\node_global\node_modules\express已经有内容


(5)在命令行输入node进入编辑模式,输入以下代码测试是否能正常加载模块:
require('express')

结果:

> require('express')
[Function: createApplication] {
  application: {
    init: [Function: init],
    defaultConfiguration: [Function: defaultConfiguration],
    lazyrouter: [Function: lazyrouter],
    handle: [Function: handle],
    use: [Function: use],
    route: [Function: route],
    engine: [Function: engine],
    param: [Function: param],
    set: [Function: set],
    path: [Function: path],
    enabled: [Function: enabled],
    disabled: [Function: disabled],
    enable: [Function: enable],
    disable: [Function: disable],
    acl: [Function],
    bind: [Function],
    checkout: [Function],
    connect: [Function],
    copy: [Function],
    delete: [Function],
    get: [Function],
    head: [Function],
    link: [Function],
    lock: [Function],
    'm-search': [Function],
    merge: [Function],
    mkactivity: [Function],
    mkcalendar: [Function],
    mkcol: [Function],
    move: [Function],
    notify: [Function],
    options: [Function],
    patch: [Function],
    post: [Function],
    propfind: [Function],
    proppatch: [Function],
    purge: [Function],
    put: [Function],
    rebind: [Function],
    report: [Function],
    search: [Function],
    source: [Function],
    subscribe: [Function],
    trace: [Function],
    unbind: [Function],
    unlink: [Function],
    unlock: [Function],
    unsubscribe: [Function],
    all: [Function: all],
    del: [Function],
    render: [Function: render],
    listen: [Function: listen]
  },
  request: IncomingMessage {
    header: [Function: header],
    get: [Function: header],
    accepts: [Function],
    acceptsEncodings: [Function],
    acceptsEncoding: [Function],
    acceptsCharsets: [Function],
    acceptsCharset: [Function],
    acceptsLanguages: [Function],
    acceptsLanguage: [Function],
    range: [Function: range],
    param: [Function: param],
    is: [Function: is],
    protocol: [Getter],
    secure: [Getter],
    ip: [Getter],
    ips: [Getter],
    subdomains: [Getter],
    path: [Getter],
    hostname: [Getter],
    host: [Getter],
    fresh: [Getter],
    stale: [Getter],
    xhr: [Getter]
  },
  response: ServerResponse {
    status: [Function: status],
    links: [Function],
    send: [Function: send],
    json: [Function: json],
    jsonp: [Function: jsonp],
    sendStatus: [Function: sendStatus],
    sendFile: [Function: sendFile],
    sendfile: [Function],
    download: [Function: download],
    type: [Function: contentType],
    contentType: [Function: contentType],
    format: [Function],
    attachment: [Function: attachment],
    append: [Function: append],
    header: [Function: header],
    set: [Function: header],
    get: [Function],
    clearCookie: [Function: clearCookie],
    cookie: [Function],
    location: [Function: location],
    redirect: [Function: redirect],
    vary: [Function],
    render: [Function: render]
  },
  Route: [Function: Route],
  Router: [Function] {
    param: [Function: param],
    handle: [Function: handle],
    process_params: [Function: process_params],
    use: [Function: use],
    route: [Function: route],
    acl: [Function],
    bind: [Function],
    checkout: [Function],
    connect: [Function],
    copy: [Function],
    delete: [Function],
    get: [Function],
    head: [Function],
    link: [Function],
    lock: [Function],
    'm-search': [Function],
    merge: [Function],
    mkactivity: [Function],
    mkcalendar: [Function],
    mkcol: [Function],
    move: [Function],
    notify: [Function],
    options: [Function],
    patch: [Function],
    post: [Function],
    propfind: [Function],
    proppatch: [Function],
    purge: [Function],
    put: [Function],
    rebind: [Function],
    report: [Function],
    search: [Function],
    source: [Function],
    subscribe: [Function],
    trace: [Function],
    unbind: [Function],
    unlink: [Function],
    unlock: [Function],
    unsubscribe: [Function],
    all: [Function]
  },
  json: [Function: json],
  query: [Function: query],
  raw: [Function: raw],
  static: [Function: serveStatic] {
    mime: Mime {
      types: [Object: null prototype],
      extensions: [Object: null prototype],
      default_type: 'application/octet-stream',
      Mime: [Function: Mime],
      charsets: [Object]
    }
  },
  text: [Function: text],
  urlencoded: [Function: urlencoded]
}

二、安装淘宝npm(cnpm)

1.安装cnpm

(1)输入以下命令

npm install -g cnpm --registry=https://registry.npm.taobao.org

(2)添加系统变量path的内容
安装完cnpm 后,cnpm文件夹会在D:\software\nodejs\node_global\node_modules生成


但是命令行的文件却生成在D:\software\nodejs\node_global

所以要将CNPM_PATH作为变量名,D:\software\nodejs\node_global作为变量值添加到系统变量中。然后一定要重启命令行,不然不会生效。
然后再在命令行中运行cnpm -v,有结果就证明大功告成了!

[npm安装删除模块以及cnpm淘宝镜像]

npm安装模块

$ npm install xxx 利用 npm 安装xxx模块到当前命令行所在目录;

$ npm install -g xxx 利用npm安装全局模块xxx;

npm 删除模块

$ npm uninstall xxx 删除xxx模块;

$ npm uninstall -g xxx 删除全局模块xxx;

参考:

  1. https://www.cnblogs.com/liaojie970/p/9296177.html
  2. https://blog.csdn.net/bxllove/article/details/84784091
上一篇 下一篇

猜你喜欢

热点阅读