How to package assets with a node module

后端 未结 1 1244
醉话见心
醉话见心 2021-01-13 14:00

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

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-13 14:35

    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 };
    

    0 讨论(0)
提交回复
热议问题