npm使用问题汇总

基本命令

安装最新版express
npm install express -g
安装指定版本express
npm install [email protected]
删除express
npm remove express -g
npm更新
npm i -g npm
空白目录下执行npm init生成package.json文件
npm init

package.json

npm的package.json文件类似于,maven的pom.xml,pip的requirements.txt文件。文件样例:

{
  "name": "test",
  "version": "0.0.1",
  "description": "This is for study gulp project!",
  "homepage": "",
  "repository": {
    "type": "git",
    "url": "https://git.oschina.net/xxxx"
  },
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": {
    "name": "awesome",
    "email": "[email protected]"
  },
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.8.11",
    "gulp-less": "^3.0.0"
  }
}

文件解读:
Node.js 在调用某个包时,会首先检查包中 package.json 文件的 main 字段,将其作为包的接口模块,如果 package.json 或 main 字段不存在,会尝试寻找 index.js 或 index.node 作为包的接口。
package.json 是 CommonJS 规定的用来描述包的文件,完全符合规范的 package.json 文件应该含有以下字段:

  • dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
  • repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
  • main - main 字段是一个模块ID,它是一个指向你程序的主要项目。就是说,如果你包的名字叫 express,然后用户安装它,然后require(“express”)。
  • keywords:关键字数组,通常用于搜索。
  • name:包的名称,必须是唯一的,由小写英文字母、数字和下划线组成,不能包含空格。

npm安装package.json时,直接到当前项目目录下用命令npm installnpm install --save-dev即可,自动将package.json中的模块安装到node-modules文件夹下。

Node JS各个目录的含义:

  • node_modules 文件夹下是各种模块,这里是express框架和jade模版引擎。
  • public 文件夹是各种静态文件;
  • routes 文件夹是各种action,routes是路径;
  • views 文件夹是各种模版。

版本格式

major.minor.patch
主版本号·次版本号·修补版本号
~version
大概匹配某个版本,如果minor版本号指定,那么minor版本号不变,而patch版本号任意;如果minor和patch版本号未指定,那么minor和patch版本号任意。
实例:
~1.1.2,表示>=1.1.2 <1.2.0,可以是1.1.2,…,1.1.n
~1.1,表示>=1.1.0 <1.2.0,可以是同上
~1,表示>=1.0.0 <2.0.0,可以是1.0.0,1.0.1,…,1.0.n,1.1.n,1.2.n,…,1.n.n

^version
兼容某个版本,版本号中最左边的非0数字的右侧可以任意;如果缺少某个版本号,则这个版本号的位置可以任意。
示例:
^1.1.2 ,表示>=1.1.2 <2.0.0,可以是1.1.2,1.1.3,…,1.1.n,1.2.n,…,1.n.n
^0.2.3 ,表示>=0.2.3 <0.3.0,可以是0.2.3,0.2.4,…,0.2.n
^0.0,表示 >=0.0.0 <0.1.0,可以是0.0.0,0.0.1,…,0.0.n

x-range
x的位置表示任意版本
1.2.x,表示可以1.2.0,1.2.1,…,1.2.n

*-range
任意版本,""也表示任意版本
如:*,表示>=0.0.0的任意版本

version1 - version2
大于等于version1,小于等于version2

range1 || range2
满足range1或者满足range2,可以多个范围
如:<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 ❤️.0.0,表示满足这3个范围的版本都可以

"dependencies": {
	"bluebird": "^3.3.4",
	"body-parser": "~1.15.2"
}

当使用最新的Node运行npm instal --save xxx时,node会优先考虑使用插入符号(^)而不是波浪符号(~)
波浪符号~:会更新到当前minor version中最新的版本。放到我们的例子中就是:body-parser:~1.15.2,这个库会去匹配更新到1.15.x的最新版本,如果出了一个新的版本为1.16.0,则不会自动升级。波浪符号是曾经npm安装时候的默认符号,现在已经变为了插入符号。
插入符号^:把当前库的版本更新到当前major version(也就是第一位数字)中最新的版本。放到我们的例子中就是:bluebird:^3.3.4,这个库会去匹配3.x.x中最新的版本,但是他不会自动更新到4.0.0。
总结:
~1.15.2 := >=1.15.2 <1.16.0
^3.3.4 := >=3.3.4 <4.0.0

因为major version变化表示可能会影响之前版本的兼容性,所以无论是波浪符号还是插入符号都不会自动去修改major version,因为这可能导致程序crush,可能需要手动修改代码。

Error: EPERM: operation not permitted

显而易见,没有权限,一般这种问题出现在Windows系统,以管理员权限执行即可。

connect ETIMEDOUT. In most cases you are behind a proxy or have bad network settings.

环境:windows 10
node版本:v8.11.3
npm版本:5.6.0
以管理员角色执行命令:npm install -g [email protected],报错信息如下:

npm ERR! code ETIMEDOUT
npm ERR! errno ETIMEDOUT
npm ERR! network request to https://registry.npmjs.org/mountebank failed, reason: connect ETIMEDOUT 104.16.21.35:443
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! A complete log of this run can be found in:
npm ERR!     D:\Users\awesome.wang\AppData\Roaming\npm-cache\_logs\2019-03-15T05_59_45_514Z-debug.log

解决方法:

# 清除缓存
npm config set proxy false
# 清除缓存
npm cache clean

其中执行第二条命令时,输出信息:

npm ERR! As of npm@5, the npm cache self-heals from corruption issues and data extracted from the cache is guaranteed to
 be valid. If you want to make sure everything is consistent, use 'npm cache verify' instead.
npm ERR!
npm ERR! If you're sure you want to delete the entire cache, rerun this command with --force.

npm ERR! A complete log of this run can be found in:
npm ERR!     D:\Users\awesome.wang\AppData\Roaming\npm-cache\_logs\2019-03-15T06_26_44_708Z-debug.log

忽视,执行命令:npm cache clean --force,再次执行安装命令成功;

npm ERR! code EINTEGRITY

报错信息如下:

npm ERR! code EINTEGRITY
npm ERR! sha512-364AI1l/M5TYcFH83JnOH/pSqgaNnKmYgKrm0didZMGKWjQB60dymwWy1rKUgL3J1ffdq9xVi2yGLHdSjjSNog== integrity checksum failed when using sha512: wanted sha512-364AI1l/M5TYcFH83JnOH/pSqgaNnKmYgKrm0didZMGKWjQB60dymwWy1rKUgL3J1ffdq9xVi2yGLHdSjjSNog== but got sha512-iWmhthtl+IsMNAJ9PO042EoelEEy/IB9mub9MMHrY5iCTrZ7G4AcdWPTmZff9QrFcW8EAGjwgukRJq3fXf+U4g==. (25383 bytes)

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\awesome\AppData\Roaming\npm-cache\_logs\2018-10-20T01_44_36_739Z-debug.log

解决方法:
1.如果有package-lock.json文件,就删掉
2.管理员权限进入cmd
3.执行npm cache clean --force
4.之后再npm install
有时候网不好也会出现问题,多试几次。

npm ERR! Windows_NT 6.1.7601

执行npm install apidoc -g报错:

npm ERR! Windows_NT 6.1.7601
npm ERR! argv "c:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "apidoc" "-g"
npm ERR! node v6.11.3
npm ERR! npm  v3.10.10
npm ERR! code EPROTO
npm ERR! errno EPROTO
npm ERR! syscall write

npm ERR! write EPROTO 101057795:error:14077419:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert access denied:openssl\ssl\s23_clnt.c:772:
npm ERR!
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Program Files\nodejs\npm-debug.log

还有其他一些莫名其妙,不明所以的信息。
解决方法:
npm cache clean --force
记住:重试,管理员权限,清除缓存,可以解决70%的npm使用问题。

参考:

npm-wont-install-packages-npm-err-network-tunneling-socket-could-not-be-estab

原创文章 131 获赞 175 访问量 32万+

猜你喜欢

转载自blog.csdn.net/lonelymanontheway/article/details/88573901