I'm trying to include some simulation data with a node module (module B) and then be able to reference that data from the calling module (module A). The data is a text file in the /data
directory of module B. I have a function on module B that calls up the data using __dirname, but of course when this function is called from module A, the __dirname references the directory of module A... not module B.
What's the best way to include asset data like this and make it available in the consuming module?
When packaging a module you can use the files
property of package.json to bundle any assets along with your module.
Then, in that module, you can use a relative path to reference your included asset.
Imagine a module with this file structure:
-assets
|-data.txt
index.js
In your package.json you might have a files
section that looked like:
files: [
'index.js',
'assets/data.txt'
]
And in index.js you could expose your asset data like so:
let fs = import 'fs';
function getAssetData() {
return fs.readFileSync('./assets/data.txt')
}
module.exports = { getAssetData };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With