node.js uses the require() function to load modules
详细说明均以写在注释之中,这里就不啰嗦了,小伙伴们自己详细看吧,千万别吧注释当成空气了。
/*在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()函数的用法了呢,本文是自己的一些理解,如有遗漏,还请大家指正。
更多node.js使用require()函数加载模块相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.
