开发过程中的 npm peerDependencies [英] npm peerDependencies during development

查看:73
本文介绍了开发过程中的 npm peerDependencies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

封装 models

  • 跨多个应用使用的常见猫鼬模型
  • peerDependencies:猫鼬"

打包app

  • 依赖项:猫鼬"、模型"
  • 通过app>与模型链接;npm 链接模型

问题:

在开发models时,需要在node_modules下安装mongoose,否则找不到mongoose.

When developing models, I need mongoose installed under node_modules, otherwise it can't find mongoose.

但是,当在app下使用models时,如果models中的node_modules下存在mongoose,它会使用复制而不是与 app 共享同一个 mongoose 实例.

However, when using models under app, if mongoose exists under node_modules in models, it uses that copy instead of sharing the same instance of mongoose with app.

我现在进行这项工作的方式是在开发 models 时安装 mongoose,然后在 app 下使用它时将其删除.我已经研究了 parent-require 但这似乎只能解决 npm 链接没有从父级找到包的问题,​​而不是必须删除/安装 node_module 的问题(或者我在做这不正确?)

The way I'm making this work now is installing mongoose when developing models, then deleting it when using it under app. I've looked into parent-require but this seems to only solve the issue with npm link not finding the package from the parent, not the issue with having to remove/install the node_module (or am I doing this incorrectly?)

相关:在多个 NPM 包之间共享一个 Mongoose 实例

推荐答案

我已经开始使用 require.main.require 而不是 require 来处理需要共享实例.

I've taken to using require.main.require instead of require for modules that need a shared instance.

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

For example, require.main.require('mongoose') will guarantee only the top-level mongoose is used.

这篇关于开发过程中的 npm peerDependencies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆