Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read a file from a node module?

Tags:

node.js

npm

fs

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?

like image 910
Aakil Fernandes Avatar asked Jun 30 '26 14:06

Aakil Fernandes


1 Answers

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

like image 111
Aakil Fernandes Avatar answered Jul 03 '26 03:07

Aakil Fernandes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!