全局安装与本地安装
npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如
npm install express # 本地安装npm install express -g # 全局安装
如果出现以下错误:
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
解决办法为:
$ npm config set proxy null 本地安装 1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。2. 可以通过 require() 来引入本地安装的包。全局安装 1. 将安装包放在 /usr/local 下或者你 node 的安装目录。2. 可以直接在命令行里使用。
如果你希望具备两者功能,则需要在两个地方安装它或使用?npm link。
接下来我们使用全局方式安装 express
$ npm install express -g
?
?
安装过程:
npm 安装 Node.js 模块语法格式如下:
npm install <Module Name>
so,在cmd输入:npm install express
安装完成后:
本地目录:
我们可以修改默认c盘路径: 修改npm全局安装模式的路径 https://blog.csdn.net/qq_22182989/article/details/91575028 ? 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 uninstall express
?
卸载后,你可以到 /node_modules/ 目录下查看包是否还存在,或者使用以下命令查看:
npm ls
卸载前:有express
卸载后:无express
全局安装:
查看安装信息
你可以使用以下命令来查看所有全局安装的模块:
$ npm list -g
?
?
更新模块
我们可以使用以下命令更新模块:
$ npm update express 搜索模块
使用以下来搜索模块:
$ npm search express 创建模块
创建模块,package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的结果。
$ npm initThis 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 fieldsand exactly what they do.Use `npm install <pkg> –save` afterwards to install a package andsave 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 testgit 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
?
这些需要手动输入
在项目目录下会自动生成 package.json 文件,打开可以看到刚才配置的项目信息。
在项目根目录下新建文件 index.js,并在其输入以下代码:
console.log(‘hello world’);
在 dos 窗口中切换到项目根目录下,输入 node index.js 可以看到打印出来的信息。
$ node index.js
?
在命令行注册新用户出问题,直接进入?https://www.npmjs.com/?这个网址注册就成功了
?
?
34331736