Akhil F
Akhil F

Reputation: 7740

How can I read a file from a node module?

In a naive implementation of mymodule, I did the following

const file = fs.readFileSync('./node_modules/dependent_module/file.txt')

However, when I include mymodule in project, the file read no longer works since the location of dependent_module has moved.

How can I read a file from a node module, such that I don't have to worry about where that node module is located?

Upvotes: 3

Views: 2956

Answers (1)

Akhil F
Akhil F

Reputation: 7740

Use require.resolve('module') then include that in the file path of the file you'd like to read

Upvotes: 12

Related Questions