This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021


Regular study notes, including ES6, Promise, Node.js, Webpack, HTTP Principles, Vue buckets, and possibly more Typescript, Vue3, and common interview questions.

Dynamic and static modules

About the meaning of dynamic and static in the previous article.

First, es6Module is a “static module” and CommonJS is a “dynamic module”.

Static modules can be introduced for analysis at compile time, and they can be tree-shaking (automatically removing unused code when webpack is packaged). Dynamic modules, on the other hand, introduce modules at code execution time; they cannot be tree-shaking.

On the tree – shaking, you can refer to the tree shaking | MDN

Once the files are packed with Webpack, they are converted from the es6Module specification to the CommonJS specification.

Module specification and classification

First, let’s review the CommonJS module specification:

  • Each jsA file is a module. (There is a function outside each module)
  • Module exportmodule.exports
  • Module importrequire

Modules also have their own categories. Here we introduce module categories:

  1. The core moduleAlso called built-in modules, includingfs,http,path,vmAnd so on. When useNo installation is required, can be directly introduced. When it was introducedThere is no need to add absolute or relative paths
  2. Third-party modulesIt’s a module defined by someone else, likecoAnd so on. When this class module is in useInstallation required.
  3. Custom modulesA module that is encapsulated or defined by the user and is required for useIntroduce by absolute path or relative path.

Common core modules

Fs module

  1. fs.readFileSync

    Let’s start with an example of a core module.

    const fs = require('fs');
    ReadFileSync is used internally by require
    let r = fs.readFileSync('./test.js'.'utf8');
    console.log(r);
    Copy the code

    First, require is a synchronous method. Assuming require is asynchronous, every time we introduce a module, we need to write the code we need to execute next in its successful callback, resulting in redundant and stacked code.

    So require internally is implemented using readFileSync.

  2. fs.existsSync

    When we read a file, if the file we read does not exist, then an exception occurs.

    let r = fs.readFileSync('./testxxx.js'.'utf8'); // Assume that the file does not exist
    console.log(r); / / an error
    Copy the code

    At this point we can use existsSync to judge the file.

    let f = fs.existsSync('./testxxx.txt');
    console.log(f); // false
    Copy the code

    This method currently only has synchronous methods; asynchronous methods have been deprecated.

The path module

  1. path.resolve

    The PATH module deals specifically with paths.

    The path.resolve method resolves a sequence of paths or path fragments into an absolute path.

    const path = require('path');
    console.log(path.resolve('a'.'b'.'c'));
    // d:\xxx\xxx\xxx\a\b\c
    Copy the code

    He prefixes the current absolute path, splits the parameters passed in with \, and parses them into new absolute paths.

    But there is a problem with this. Suppose we now switch the execution directory, the path will be wrong. The reason is that path.resolve is resolved by process.cwd() by default.

    To get the path right, we can use the following solutions.

    const path = require('path');
    console.log(path.resolve(__dirname ,'a'.'b'.'c'.'/')); // The path is back to the root directory
    Copy the code

    Note: If the path exists/, the current path will return to the following directory)

  2. path.join

    Path. join is the concatenation of paths for the parameters passed in. No path is added.

    If there is a slash in the argument, it will also be spliced together.

    const path = require('path')
    console.log(path.join('a'.'b'.'c'.'/'));
    // a\b\c\
    Copy the code

    So we can use this method to concatenate absolute paths.

    const path = require('path');
    console.log(path.join(__dirname ,'a'.'b'.'c'.'/')); // The paths are spliced together
    Copy the code

    ** Path. join and path.resolve are used interchangeably in some cases. But in the case of/in the path, it is better to use path.join. **

  3. path.extname

    Path. extName gets the file extension, or file type.

    const path = require('path');
    console.log(path.extname('text.min.js')); // .js
    Copy the code
  4. path.basename

    The path.basename method returns the last part of the path.

    const path = require('path');
    console.log(path.basename('d:/xxx/xxx/xxx/test.js'.'.js'));  // test
    Copy the code

    If the last parameter is not passed, the last filename, test.js, is returned

  5. path.relative

    The path.basename method obtains a relative path based on the current path.

    const path = require('path');
    console.log(path.basename('a/b/c/test.js'.'a'));  / /.. \.. \..
    Copy the code
  6. path.dirname

    The path.dirname method gets the parent path of the current file.

    const path = require('path');
    console.log(path.dirname('a/b/c'));  // a/b
    Copy the code

    The __dirname implementation is usedpath.dirname.

The vm module

Before we talk about this module, we can think about a question.

How can strings be executed as JS?

  1. The eval function

    See the eval() function

    The first thing that comes to mind is the eval() function, which converts an passed string or expression into javascript code that can be executed, depending on the current execution environment.

    var a = 100;
    eval('console.log(a)'); / / 100
    Copy the code
  2. new Function

    Reference Function | MDN

    The new Function takes the string passed as a Function and the first few arguments passed as arguments to the Function. And in Node, it’s not affected by the outside world, because new Function is level with the outermost scope and is unaffected by the browser.

    var a = 100;
    new Function('b'.'console.log(a)') ();// a is not defined
    Copy the code
  3. The vm module

    Reference the vm virtual machine | Node’s official website

    The VM module allows you to compile and run code in the V8 virtual machine context.

    (onvmThe use and principle of modules will be explained in detail in a future article.


This article was created by Mo Xiaoshang. If there are any problems or omissions in the article, welcome your correction and communication. You can also follow my personal site, blog park and nuggets, AND I will upload the article to these platforms after it is produced. Thank you for your support!