node项目是CommonJs规范目前的最好实践应用。
require/module.exports:导入/导出exports对象。
- 对应 commonJS 规范;
- 运行时加载;
- 写在代码的任意位置;
- 导出的值不能再变化
代码示例:
test.js
require("./style.css");
document.write(require("./test2.js"));
style.css
body {
background: yellow;
}
test2.js
module.exports = "It works from runoob2.js.";
import/export (default):导入/导出导出常量、函数、文件、模块。
- 对应 es6 规范;
- 编译时加载;
- 写在文件顶部;
- 导出的值可变化
import/export 使用:参考 export 和 export default 的区别