NodeJS写模块和引入模块的例子

nodejs自学.js

function hello(){
console.log("hello world");
}

function s(){
console.log("this is a ew");
}

function add(a, b){
return a+b;
}

exports.hello = hello;//留出接口
exports.s = s;
exports.add = add;

test.js

//加载模块
var app = require("./nodejs自学");//./表示当前目录,也可以填"./nodejs自学.js"
app.hello();
app.s();
console.log(app.add(1,3));

在终端中 node test.js 

结果:

hello world

this is a ew

4

原文地址:https://www.cnblogs.com/luckyXcc/p/5808233.html