Requiring a project file from an npm module

时间:2017-04-10 00:17:23

标签: node.js npm module config require

I have an npm module that requires a config file (provided by the user). Is there a way to tell that module to look for a specific file in the user's directory ?

I want something like this:

In the module

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

In the user's project

/node_modules (contains the module file)

/src/index.js

/config.json

In the index.js file

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

1 个答案:

答案 0 :(得分:0)

你可以使用绝对路径,因为你知道npm模块需要什么文件只需添加模块文件里面的路径更好地添加到index.js例子中 -

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