首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非营利组织peerDependencies在发展中的作用

非营利组织peerDependencies在发展中的作用
EN

Stack Overflow用户
提问于 2014-09-05 10:56:00
回答 3查看 2.9K关注 0票数 5

设置:

封装models

  • 跨多个应用程序使用的通用猫鼬模型
  • peerDependencies:“猫鼬”

封装app

  • 依赖关系:“猫鼬”、“模型”
  • 通过app> npm link models链接到模型

发行:

在开发models时,我需要安装在node_modules下的猫鼬,否则它找不到猫鼬。

但是,当在app下使用app时,如果在models中的node_modules下存在猫鼬,则使用该副本而不是与app共享相同的猫鼬实例。

我现在做这个工作的方式是在开发models时安装猫鼬,然后在app下使用它时删除它。我研究过parent-require,但这似乎只解决了npm链接没有从父程序中找到包的问题,而不是必须删除/安装node_module的问题(还是我做得不对?)

相关:Sharing a Mongoose instance between multiple NPM packages

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-12 03:58:36

对于需要共享实例的模块,我已经开始使用require.main.require而不是require

例如,require.main.require('mongoose')将只保证使用顶级猫鼬。

票数 9
EN

Stack Overflow用户

发布于 2018-09-01 21:10:03

万一你出了差错

代码语言:javascript
复制
require.main.require is not supported by webpack

..。在模块的根dir中调用npm link <required module>

我对peerDependency react也有同样的看法。所以我为我的本地模块做了npm link reack,它起了作用。

票数 0
EN

Stack Overflow用户

发布于 2019-02-05 10:53:20

下面是一个模块,您可以使用该模块处理链接的父模块和祖父母模块。

代码语言:javascript
复制
/**
 * the original module scope
 */
const _BASE_MODULE = module;

/**
 * the top level module (fixes nasty peer dependency issues with npm link)
 */
const _MODULE = topLevelModule();

/**
 * find topmost module scope
 */
function topLevelModule() {
    let _mod = _BASE_MODULE;

    while (_mod.parent) {
        _mod = _mod.parent;
    }

    return _mod;
}

/**
 * cheap way to check if the module is available,
 *
 * @param {string} mod module name
 * @return {boolean}
 * @todo if we need better then we should switch to browserifys resolve package, but thats heavy
 */
export function isAvailable(mod) {
    try {
        _MODULE.require.resolve(mod);

        return true;
    } catch (e) {
        return false;
    }
}

/**
 * requires a module from the top level scope
 * @param {string } mod module name
 * @return {*}
 */
export function topLevelRequire(mod) {
    return _MODULE.require(mod);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25684309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档