/*在node中,可以使用require()函数来加载模块.
* require函数使用一个参数,参数值可以带有完整路径的模块的文件名,也可以为模块名.当使用node中提供的模块时,在require函数中只需要指定模块名即可.
* */
//建立一个页面2.js;代码如下
var name="思思博士";
exports.name=name;
//建立一个页面1.js;代码如下
var two=require("./2.js");
console.log(two.name);
//输出结果:思思博士 /*
* 在node中所有的脚本文件都是一个模块文件,因此1.js也是一个模块文件,又由于该文件是在命令行窗口中通过node命令被直接运行的,因此在node中该模块文件被定义为应用程序的主模块
* 可以用如下的方法检测出当前的模块是否是主模块
* */
if(module===require.main){
console.log("当前模块时主模块");
}
//输出结果:当前模块时主模块 //2.js代码
var name="思思博士";
console.log(name);
exports.name=name; //1.js代码:
var two=require("./2.js");
var two=require("./2.js");
//虽然引用了2次,但是只是执行了1次console.log(name)的输出. /*require.resolve(str)
* 在node中,可以使用这个函数来查询某个模块文件的带有完整绝对路径的文件名.
* */
var url=require.resolve("./2");
console.log(url);
//输出结果:E:\node\gys\2.js /*require.cache
* 在node中,这个属性代表了所有已被加载模块的缓存区.
* */ var two=require("./2.js");
var cache=require.cache;
console.log(cache);
/*输出结果:
* { 'E:\\node\\gys\\1.js':
{ id: '.',
exports: {},
parent: null,
filename: 'E:\\node\\gys\\1.js',
loaded: false,
children: [ [Object] ],
paths:
[ 'E:\\node\\gys\\node_modules',
'E:\\node\\node_modules',
'E:\\node_modules' ] },
'E:\\node\\gys\\2.js':
{ id: 'E:\\node\\gys\\2.js',
exports: { name: '思思博士' },
parent:
{ id: '.',
exports: {},
parent: null,
filename: 'E:\\node\\gys\\1.js',
loaded: false,
children: [Object],
paths: [Object] },
filename: 'E:\\node\\gys\\2.js',
loaded: true,
children: [],
paths:
[ 'E:\\node\\gys\\node_modules',
'E:\\node\\node_modules',
'E:\\node_modules' ] } }
* */ //2.js代码
var name="思思博士";
console.log(name);
//1.js代码
//当使用delete关键字删除缓存区中缓存的某个模块对象后,下次加载该模块时将重新运行该模块中的代码.使用代码: var two=require("./2.js");
var two1=require("./2.js");
console.log("删除前")
delete require.cache[require.resolve("./2.js")];
console.log("删除后");
var two2=require("./2.js");
/*
* 输出结果:
* 思思博士
* 删除前
* 删除后
* 思思博士
* */

node中的require的更多相关文章

  1. node初识——node中的require方法与require.js的区别

    出处:http://blog.csdn.net/u013613428/article/details/51966500 作为一个前端的新手,总是诧异于js的模块载入方式,看到了通过requireJs提 ...

  2. node中的require和exports

    http://cnodejs.org/topic/4f16442ccae1f4aa270010e9

  3. Node中没搞明白require和import,你会被坑的很惨

    ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...

  4. Node中导入模块require和import??

    转自:https://blog.csdn.net/wxl1555/article/details/80852326 S6标准发布后,module成为标准,标准的使用是以export指令导出接口,以im ...

  5. 关于node中 require 和 ES6中export 、export default的总结

    nodejs中 require 方法的加载规则 方法的加载规则 1. 优先从缓存中加载 2. 核心模块 3. 路径形式的模块 4. 第三方模块 一.优先从缓存中加载 main.js:执行加载a.js模 ...

  6. 深入理解jQuery、Angular、node中的Promise

    最初遇到Promise是在jQuery中,在jQuery1.5版本中引入了Deferred Object,这个异步队列模块用于实现异步任务和回调函数的解耦.为ajax模块.队列模块.ready事件提供 ...

  7. node中的可读流和可写流

    javascript的一个不足之处是不能处理二进制数据,于是node中引入了Buffer类型.这个类型以一个字节(即8位)为单位,给数据分配存储空间.它的使用类似于Array,但是与Array又有不同 ...

  8. 使用express+multer实现node中的图片上传

    使用express+multer实现node中的图片上传 在前端中,我们使用ajax来异步上传图片,使用file-input来上传图片,使用formdata对象来处理图片数据,post到服务器中 在n ...

  9. nodejs中的require,exports使用说明

    模块是一门语言编写大项目的基石,因此,了解如何组织.编写.编译.加载模块很重要.这里主要谈谈Node中的模块加载. 1.Node中的模块,主要使用require来加载模块,文件 require(&qu ...

随机推荐

  1. LVS模式一:直接路由模式DR(Direct Routing)

    (一)LVS 一.LVS的了解 LVS(Linux Virtual Server)可以理解为一个虚拟服务器系统. Internet的飞速发展,网络带宽的增长,Web服务中越来越多地使用CGI.动态主页 ...

  2. DataTable和实体类通过反射相互转换

    using System.Runtime.Serialization; using System.Data; using System.Reflection; using System.Collect ...

  3. SQL 动态PIVOT查询

    DECLARE @sql_str VARCHAR(8000)DECLARE @sql_col VARCHAR(8000) SELECT @sql_col = ISNULL(@sql_col + ',' ...

  4. pip安装tensorflow出错怎么办

    随着人工智能的开发越来越多人参与,现在下载tensorflow 1.2版本也经常出错了,如下: 这时怎么办呢? 其实比较简单,可以通过pypi的网站来下载: https://pypi.python.o ...

  5. ss-libev 源码解析local篇(3): server_recv_cb之SNI和STAGE_PARSE

    上一篇看到STAGE_HANDSHAKE中的处理,到发出fake reply.这之后会从socks5 request中解析出remote addr and port,即客户端实际想要访问的服务器地址和 ...

  6. 369C Valera and Elections

    http://codeforces.com/problemset/problem/369/C 树的遍历,dfs搜一下,从根节点搜到每个分叉末尾,记录一下路况,如果有需要修复的,就把分叉末尾的节点加入答 ...

  7. 64位系统下注册32位dll、ax文件

    64位系统下注册32位dll.ax文件. 换了64位系统遇到的新问题,目前常用的影音处理软件多数为32位. 注册这些32的滤镜会提示不兼容,大概因为32 位进程不能加载64位Dll,64位进程也不可以 ...

  8. Maven部署Jetty服务器pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. How to Change the Size of a Box-Plot Label in MATLAB

    Type "load carsmall" to load a sample data set included with MATLAB. Type "boxplot(Ho ...

  10. FTRL笔记

    这篇笔记主要参考冯杨的五篇博客:在线最优化求解(Online Optimization).因为对于在线学习方法,稀疏性问题需要特别关注:每次在线学习一个新 instance 的时候,优化方向并不一定是 ...