【问题标题】:Requiring a project file from an npm module需要来自 npm 模块的项目文件
【发布时间】:2017-09-04 21:57:33
【问题描述】:

我有一个需要配置文件(由用户提供)的 npm 模块。 有没有办法告诉该模块在用户目录中查找特定文件?

我想要这样的东西:

在模块中

const config = require(USER_ROOT_DIRECTORY + '/config.json');

在用户的项目中

/node_modules(包含模块文件)

/src/index.js

/config.json

在 index.js 文件中

const module = require('module'); // will automatically get the config.json file's content

【问题讨论】:

    标签: node.js npm module config require


    【解决方案1】:

    您可以使用绝对路径,因为您知道 npm 模块需要什么文件,只需在模块文件中添加路径即可更好地添加到 index.js 示例中-

    var config = require('./subfolder/config.json')
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2016-05-10
      • 1970-01-01
      • 2013-03-20
      • 2012-01-19
      • 1970-01-01
      • 2019-09-17
      • 2015-11-20
      • 2016-08-02
      相关资源
      最近更新 更多