How to access resources included in my npm package


(Bruce Blore) #1

Right now, I am working on learning various server-side things with node.js and right now, I am trying to build a full-stack chat app. I want to include some various resources to be served to the client using a static file server. I understand that when I publish my npm package, the entire directory with the package.json file is published recursively. So I know how to include the resources. The path that the packages are stored in depends on the operating system, so I cannot hard-code the directory. (I could, but it would then only work on some machines.) Here are some ways I thought of to access them, in my order of preference:

Do the fs and/or path modules support a directory prefix (not sure if that is what it is called) representing the directory of the current package or running JS file? This would likely be the most convenient.

Is there a function to get the directory that packages go in?

Is there a way to execute npm root -g with (or without) the shell (preferably without the user seeing it) and get the result?

If all else is impossible or inadvisable, how would I check if a folder already exists WITHOUT creating it?

Which of these methods would be the best? Is there a better way to do this that I have not thought of?

Thanks for the help.