1

my file are like avilabele this sturucture.

enter image description here

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

sample.js

    var name={
   h:14
}
module.exports=name;

test.js

var name=require('./sample.js')
console.log(name.h);

when a run test.js code using command prompt :

node test.js

it gives error like :

Cannot find module './sample.js'

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1
var name=require('../sample.js')
console.log(name.h);

You are requiring module from the parent directory with ".."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.