visual studio - When to use ../ vs ./ in Node.js require() -


var user = require('../models/user');

so in visual studio, created folder called models, , file inside called user.js . learned make sense put ./models/user instead of 2 dots. although, gives me crash, wondering reason that. thank you

. (one dot) current directory

.. (two dots) parent directory

whichever 1 use depends on want load.

example:

let's have file structure:

- app   - dir1     - file1.js     - file2.js   - dir2     - file3.js 

to use file2.js file1.js need:

require('./file2.js'); 

because files in same directory.

but use file3.js file1.js need:

require('../dir2/file3.js'); 

because need start 1 directory current one.

of course use file2.js file1.js work:

require('../dir1/file2.js'); 

or this:

require('../../app/dir1/file2.js'); 

or crazy path this:

require('./././../../app/../app/../app/dir1/file2.js'); 

this not node.js specific. works same in shell or in html or anything. 1 dot current dir, 2 dots parent dir.

that's why use ./script run script in current directory (if . not in path) , run ../script run script in parent directory etc. in html <a href=".."></a> link parent directory, <a href="../file.html"></a> link file.html in parent directory, while <a href="./file.html"></a> link file.html in current directory etc. pretty universal convention.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -