注意:相对路径必须加 ./
可以省略后缀名(.js)
相对路径中的./不能省略,否则报错,省略代表的是核心模块
//可以 require('./b.js') //推荐:可以省略后缀名 require('./b')
console. log('a start') require('./b.js') console.log('a end')
console.log('b.js文件被加载执行了')
console. log('a start') require('./b.js') console.log('a end')
console.log( 'b start' ) require('./c.js ') console.log( 'b end ' )
console.log('ccc')
var foo = 'aaa' console.log('a start') function add(x, y) { return x + y } require('./b.js') console.log('a end') console.log(‘foo的值是:', foo)
console.log('b start') console.log(add(10,20)) var foo = " bbb' require('./c.js ')console.log('b end')
exports
a.js
var bExports = require('./b') console.log(bExports.foo) console.log(bExports.add(10, 30))
b.js
var foo = 'bbb' exports.foo = 'hello' exports.add = function (x, y) { return x + y }
a.js
var bExports = require('./b') console.log(bExports.age)
b.js
var age = 18 exports.age = age
结果:18