更换npm镜像源:
设置镜像源为淘宝:
npm config set registry https://registry.npm.taobao.org
还原官方镜像源:
npm config delete registrys
使用淘宝镜像后无法使用publish和unpublish命令,如果需要发布软件包和撤销发布软件包,则需要还原为官方镜像。
初始化项目:
npm init
在项目目录下执行npm init命令,依照提示输入问题的答案后即可创建一个标准的npm 模块,同时会生成一个package.json文件,其中记录了当前的模块名、版本、依赖等信息。
安装模块
本地安装:
npm install <模块名> //第三方模块会被安装到当前目录的node_modules下 (如果没有该目录将会自动创建)。使用require('模块名')即可导入本地模块。
全局安装:
npm install <模块名> -g //第三方模块将被安装到/usr/local/lib/node_modules目录或者安装Node.js的目录。可以直接在命令行使用。不可以使用require(’模块名‘)来引用。
开发依赖:
npm install <模块名> --save-dev //依赖信息将被保存在到Package.json的devDependencies中
生产依赖:
npm install <模块名> --save //开发环境和生产环境都需要使用,依赖信息将被保存在package.json的dependencies中
卸载本地/全局模块
npm uninstall <模块名> [-g]
查看已安装的本地/全局模块
npm list [-g]
更新本地/全局模块
npm update <模块名> [-g]
搜索模块
npm search <模块名>
发布本地模块到npm 仓库
npm publish
撤销指定版本的模块发布
npm unpublish <模块名>@<版本号>
读取配置
npm config get <config_name>
设置配置
npm config set <config_name>
删除配置
npm config delete <config_name>
执行package.json中的scripts 定义的命令
npm run <命令>