What is required ('.') Used in node.js

I read the documentation for the node.js cli module and it has one line like this. I know that we can include external modules like this, but we don’t know what use is. requiring a module;

const foo = require('.');

Can someone tell me what uses it or why it was used that way.

+4
source share
2 answers

It will import the index file into the folder in which you run your file, there will be an empty request. Javascript requires the module to try to find the index.jsfile unless you specify a file name (just provide a link to the folder) in the require () argument.

This is basically an alias for const foo = require('./index.js');

index.js

module.exports = 1;

foo.js

const foo = require('.');
console.log({ foo });

,

{ foo: 1 }
+7

('.'), '.' , .

-- parent 
  -- child1
    -- grandchild1
    -- grandchild2
  -- child2

, child1 grandchild1:   ( './grandchild1/_') , :  require ('../parent/filename')// '..' , , : '../../some_folder'

0

Source: https://habr.com/ru/post/1694241/


All Articles