NO | 包在 node_module 中 | 是否有包 | 包的main属性 | main属性有效 | 是否有inex.js | 包对应的文件 |
1 | 否 | 无 | - | - | 有 | index.js |
2 | 有 | 无 | - | 有 | index.js | |
3 | 有 | 有效 | - | main属性指定的文件 | ||
4 | 有 | 无效 | 有 | index.js | ||
5 | 是 | 有 | 有效 | - | main属性指定的文件 |
包对应的文件:到包对应的是一个模块目录的时候,执行目录下的生命文件
NO.1
目录结构:
代码
//index.js
exports.world = function() {
console.log('Hello World');
}
//main.js
var hello = require('./m1');
hello.world();
No.2
目录结构:
代码
//index.js
exports.world = function() {
console.log('Hello World');
}
//main.js
var hello = require('./m1');
hello.world();
//package.json
{
"name": "m1",
"version": "1.0.0",
"description": "",
"main": "index2.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
NO.3
目录结构
代码
//index2.js
exports.world = function() {
console.log('Hello World');
}
//main.js
var hello = require('./m1');
hello.world();
//package.json
{
"name": "m1",
"version": "1.0.0",
"main": "index2.js",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
NO.4
目录
代码
// main.js
var hello = require('./m1');
hello.world();
// m1/package.json
{
"name": "m1",
"version": "1.0.0",
"description": "",
"main": "index2.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
// m1/index.js
exports.world = function() {
console.log('Hello World');
}
运行结果
NO.5
目录
代码
// main.js
var hello = require('./m1');
hello.world();
// m1/package.json
{
"name": "m1",
"version": "1.0.0",
"description": "",
"main": "index2.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
// m1/index2.js
exports.world = function() {
console.log('Hello World');
}
运行结果
NO.6
目录结构
代码
// mian.js
var hello = require('m1');
hello.world();
// node_modules/m1/pacage.json
{
"name": "m1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
// node_modules/m1/index.js
exports.world = function() {
console.log('Hello World');
}