Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you include a resource or template file with an npm package and then load them in node?

How do I make my npm package include a file called template.html, and then once the package has been globally installed, how will my node javascript app find the path to that file in order to load it from disk?

like image 487
Jordan Morris Avatar asked Oct 16 '25 00:10

Jordan Morris


1 Answers

By default, NPM will include all the files in your directory when packaging. (You can control that with files property in package.json.)

When your package is installed, if you have template.html in the same directory as your script file, you can use __dirname to access it:

fs.readFile(path.resolve(__dirname, 'template.html'), 'UTF-8', callback);

(source)

like image 190
Koterpillar Avatar answered Oct 18 '25 15:10

Koterpillar