package.json是npm package的配置文件,存储当前项目相关的信息。如果下载npm中的包,包内会自带该文件。具体有如下属性:
{
"name" : "underscore",
"description" : "JavaScript's functional programming helper library.",
"homepage" : "http://documentcloud.github.com/underscore/",
"keywords" : ["util", "functional", "server", "client", "browser"],
"author" : "Jeremy Ashkenas",
"contributors" : [],
"dependencies" : [],//生产依赖
"devDependencies”:[],//开发依赖
"repository" : {"type": "git", "url": "git://github.com/documentcloud/underscore.git"},
"scripts": {
"build": "node build.js"
}
"main" : "underscore.js",
"version" : "1.1.6"
} main:the main entry point for the libary; when someone runs require(<library name>), require resolves this call to require(<package.json:main>).
dependencies :used to list all the dependencies of your project that are available on npm. When someone installs your project through npm, all the dependencies listed will be installed as well. Additionally, if someone runs npm install in the root directory of your project, it will install all the dependencies to ./node_modules.
devDependencies :these are dependencies not required for normal operation, but required/recommended if you want to patch or modify the project. If you built your unit tests using a testing framework, for example, it would be appropriate to put the testing framework you used in your devDependencies field. To install a project's devDependencies, simply pass the --dev option when you use npm install.
scripts:是一个对象, 每一个属性,对应一段脚本。
全局安装vs本地安装:
npm install xxx -g :属于全局安装,将包安装在C:\Users\admin\AppData\Roaming\npm\node_modules目录下,对应的cmd文件和node_module目录平级,可以在任何地方执行CLI命令。不可以通过 require() 来引入全局安装的包。
npm install xxx –save-dev :属于本地安装,将包安装在运行npm命令时所在目录平级的node_modules下,对应的cmd命令在node_module目录下的.bin目录中。可以通过 require() 来引入本地安装的模块。

如果不安装全局包,则在pro目录下是不能执行对应的命令行命令,必须到.bin目录下,或者通过npm link命令或通过package.json的scripts字段设置:
"scripts" : {
"build" : "webpack ./entry.js bundle.js"
} 然后运行npm run build命令。
require()查找文件的顺序:
当require一个文件模块时,先从当前执行文件平级的的node_modules目录中查找;然后查找上一级的node_modules目录;依次迭代,直到根目录下的node_modules目录。如果还没有查找到指定模块的话,就会去NODE_PATH注册的路径中查找。
require绝对路径的文件,查找时不用去遍历每一个node_modules目录,其速度最快。
默认情况下,指向全局模块根目录的NODE_PATH并没有被设置,所以全局安装的包不能被本地require()。
为什么要全局安装和本地安装两次?
本地安装可以让每个项目拥有独立的包,不受全局包的影响,保证不同版本包之间的相互依赖。全局安装可以保证在任何地方都可以执行命令行命令。
npm 命令:
npm config get prefix:获取全局模块安装路径;
where node:获取node所在路径;
npm ls –depth=0 :查看本地安装的模块,只看当前目录,不遍历子目录。
npm config ls –l:查看npm内置的配置参数