Search code examples
node.jsnpmembedded-resourcepackaging

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?


Solution

  • 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)