账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    nodeJs中开发CLI如何允许用户自定义配置文件?例如wepack-cli如何加载用户webpack.config.js的?
    74
    0

    问题描述:
           目前在使用nodeJs开发一个CLI工具,程序发布到npm(私服)上后,需要允许读取用户自定义配置js文件。此时如何在程序中加载这个.js文件呢?


    例如wepack-cli如何加载用户webpack.config.js的?
    看了看webpack-cli的源码

    const requireConfig = function requireConfig(configPath) {
        let options = (function WEBPACK_OPTIONS() {
            if (argv.configRegister && argv.configRegister.length) {
                module.paths.unshift(path.resolve(process.cwd(), "node_modules"), process.cwd());
                    argv.configRegister.forEach(dep => {
                        require(dep);
                });
                return require(path.resolve(process.cwd(), configPath));
            } else {
                return require(path.resolve(process.cwd(), configPath));
            }
        })();
        options = prepareOptions(options, argv);
        return options;
    };

    尝试过类似的的方法:

    require(path.resolve(process.cwd() ,  'some.config.js'));
    // Cannot find module 'E:codeFolder\some.config.js'
    

    貌似nodeJsrequireJs不能使用这种超出当前根目录(codeFolder)的绝对路径去加载.

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 遭遇失败 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部