Manoj Muduli Manoj Muduli - 1 year ago 71
Node.js Question

how to access parent module data in child file in node.js

my file are like avilabele this sturucture.

enter image description here

sample.js is root file and test.js is avilable in xx folder.


var name={


var name=require('./sample.js')

when a run test.js code using command prompt :

node test.js

it gives error like :

Cannot find module './sample.js'

Answer Source

When you require a file with a relative path, it is relative to the file doing the require (see here). In test.js, require('./sample.js') will look for /path/to/xx/sample.js. Use require('../sample.js') since it is in the parent folder of test.js.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download