Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'dot slash' (./) mandatory when a module in Node.js is required?

Tags:

node.js

module

I'm learning Node.js and writing a simple module using the Visual Studio 2015 Node.JS extension.

I know ./ means 'look for the file in the same directory'. But in most systems I've seen (say, #include in C++) the forwarding ./ is optional. I've tried to remove it but it turns out Node.js cannot find modules without the prefix. For me it seems ugly, so is it really mandatory? If yes, why?

Regards,

like image 880
noober Avatar asked Jan 24 '16 22:01

noober


1 Answers

With require('./script_name'), you can include another JS within the same folder, as you know already.

When you require('module_name') without the ./, Node.JS looks at the node_modules folder for a module with this name. Optionally, you can call it's public methods or pass it arguments.

like image 157
Petr Hejda Avatar answered Oct 22 '22 23:10

Petr Hejda