node absolute and relative modules

Absolute modules are modules that Node finds through its internal node_modules, or modules built into Node such as fs.

A relative module will require to point to a JavaScript file in a relative working directory.

For example, we create three files fileOne.js, fileTwo.js, index.js in the same directory

The content in fileOne.js is

console.log('this is fileOne');

The content in fileTwo.js is

console.log('this is fileTwo');

The content in index.js is

require('fileOne');
require('fileTwo');

Run the index.js file  

As a result, fileOne and fileTwo could not be found, because they are not installed through NPM, nor in the node_modules directory, and there is no module with this name in Node's own modules.

If we want it to show up correctly, we just need to prefix require with ./

require('./fileOne');
require('./fileTwo');

  

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324775085&siteId=291194637