Sunday, July 18, 2021

one dot ./ vs two ../ path in Nodejs import

it is common problem, should we use one dot or two dot in the import path.

here is the note

one dot ./ represent the current directory

two dot ../ represent the parent directory

if you have the following file structure in your project

DirOne

    -file1.js

    -file2.js

DirTwo

    -file3.js

    -file4.js

if you wan to import file1. js into file2.js

in file2.js

import file1 from "./file1.js"

if  we want to import file3.js into file2.js, since there are in two different directory, we have to reference to the parent directory then DirTwo.

we need to put this following import statement into file2.js

import file3 from "../DirTwo/file3.js" 

No comments:

Post a Comment