node.js 使用 require() , import 是ES6的新特性。
让javaScript运行在服务器上,js写的软件不是运行在浏览器上就都是用的这个,赋予js操作系统功能的能力,可以直接文件读写等。
编写模块
1.
npm init
2.
create index.js
3.
exports.printMsg = function() {
console.log("This is a message from the demo package");
}
4.
npm publish
5.
In the test directory, install your module:
npm install <your-module-name>
6.
In the test directory, create a test.js file which requires your module and calls your module as a method.
var demo = require("module-name");
demo.printMsg();
7.
On the command line, run node test.js. The message sent to the console.log should appear.
node test.js
评论区