[英]Using multiple packages in Meteor 1.3+ that include the same npm package
I would like to know how to create / use multiple packages in Meteor 1.3+ that use the same npm dependency. 我想知道如何创建/使用Meteor 1.3+中使用相同npm依赖项的多个包。
I have migrated to Meteor 1.3+ and with that am trying to use the npm versions of the angular
& angular-meteor
packages but I am struggling to fully understand how npm is used in packages & cannot seem to get it working as needed. 我已经迁移到流星1.3+,并且尝试使用
angular
和angular-meteor
包的npm版本但是我很难完全理解npm如何在包中使用并且似乎无法使其按需工作。
I have an existing Meteor application where I have split functionality into Meteor packages to promote modularity & reusability - instead of directly in the Meteor root. 我有一个现有的Meteor应用程序,我将功能拆分为Meteor包,以提升模块化和可重用性 - 而不是直接在Meteor根目录中。
I have tried several different ways of structuring this to no avail. 我已经尝试了几种不同的结构方法,但无济于事。
This is my preferred way of doing it because I then don't need to worry about what npm packages my Meteor packages depend on. 这是我的首选方式,因为我不需要担心我的Meteor软件包依赖的npm软件包。
If I structure my application as follows: 如果我构建我的应用程序如下:
Meteor App
Meteor Package A
NPM Package angular@1.5.5
NPM Package angular-meteor@1.3.10
Meteor Package B
NPM Package angular@1.5.5
NPM Package angular-meteor@1.3.10
I achieve this by using Npm.depends()
in each package's package.js
file; 我通过在每个包的
package.js
文件中使用Npm.depends()
来实现这一点; which creates a .npm
folder in that package: 这会在该包中创建一个
.npm
文件夹:
Npm.depends({
'angular': '1.5.5',
'angular-meteor': '1.3.10'
});
In the js files I have the following at the top of any file that uses either angular
and/or angular-meteor
. 在js文件中,我在使用
angular
和/或angular-meteor
的任何文件的顶部都有以下内容。
// Included only when angular is needed
import angular from 'angular';
// Included only when angular-meteor is needed
import angularMeteor from 'angular-meteor';
The problem with this method is that I then get the following error in the browser console once for each package that has the duplicate npm dependency: WARNING: Tried to load angular more than once.
此方法的问题是,我在浏览器控制台中为具有重复npm依赖项的每个包获取以下错误:
WARNING: Tried to load angular more than once.
; ; since I would imagine Meteor is trying to include angular for each package that uses it.
因为我想象Meteor试图为每个使用它的包装包含角度。
From what I can gather this is unlike Meteor packages which Meteor knows to only include once; 从我可以收集的内容来看,这与Meteor知道只包含一次的Meteor软件包不同; which is how I was using it before migrating to Meteor 1.3+
这是我在迁移到Meteor 1.3+之前使用它的方式
The only way I can seem to get it to work is if I include the npm dependencies directly in the meteor app. 如果我直接在流星应用程序中包含npm依赖项,我似乎唯一可以使它工作的方法。
Keeping everything the same as above but removing the Npm.require()
from the package.js
files. 保持一切与上面相同,但从
package.js
文件中删除Npm.require()
。 I then do meteor npm install --save angular angular-meteor
so that the angular
& angular-meteor
packages are added directly into the meteor app & only ever included once in the whole project. 然后我做
meteor npm install --save angular angular-meteor
以便angular
和angular-meteor
包直接添加到流星应用程序中,并且只在整个项目中包含一次。
The structure then becomes: 结构然后变成:
Meteor App
NPM Package angular@1.5.5
NPM Package angular-meteor@1.3.10
Meteor Package A
Meteor Package B
Although this works now the problem with this method is twofold: 虽然现在这个方法有效但这个方法有两个问题:
Npm.depends()
for a npm package that I have already included in my project I will get the same duplicate error as above. Npm.depends()
作为我已经包含在项目中的npm软件包,我将得到与上面相同的重复错误。 I am not yet marking this as answered since I have not found a solution to my problem but I have made some progress with regards to error messages that might help others. 我还没有将此标记为已回答,因为我没有找到解决问题的方法,但我在可能帮助其他人的错误消息方面取得了一些进展。
Part of my problem was related to the fact that I was still using the Meteor package for angular-ui-router
instead of updating my project to use the npm version. 我的部分问题与我仍在使用
angular-ui-router
的Meteor包而不是更新我的项目以使用npm版本这一事实有关。 The error WARNING: Tried to load angular more than once.
错误
WARNING: Tried to load angular more than once.
was generated because the Meteor version of angular-ui-router
is dependent on the Meteor package angular
; 生成是因为
angular-ui-router
的Meteor版本依赖于Meteor包angular
; which appears to be included in a different way to npm packages such that Meteor does not detect that it is already loaded & loads it again. 它似乎以与npm包不同的方式包含在内,以至于Meteor没有检测到它已经加载并再次加载它。
As for my problem of wanting to use Npm.require()
for npm packages within the browser... from what I can gather there is no way of doing this from within Meteor packages. 至于我想在浏览器中使用
Npm.require()
获取npm包的问题......从我可以收集到的内容中,没有办法在Meteor包中进行此操作。 There is the hack of running npm install within the package & including each needed file in the package.js
file using api.addFiles( ... , 'client' )
but this has obvious drawbacks such as not knowing which Meteor packages include which files from their npm dependencies - so you could end up with 2 Meteor packages each trying to include bootstrap (for example), resulting in duplicate includes within the Meteor app. 在包中运行npm install并使用
api.addFiles( ... , 'client' )
在package.js
文件中包含每个所需文件,但这有明显的缺点,例如不知道哪个Meteor包包含哪些文件从他们的npm依赖项 - 所以你可能最终得到2个Meteor包,每个包都试图包含bootstrap(例如),导致Meteor应用程序中的重复包含。
Since I am of the opinion that Meteor will deprecate Meteor packages in some future release of Meteor - in favour of pure npm packages - I am going to leave this train of thought and resign myself to using npm install
at the root level for all npm packages that I need to use. 由于我认为Meteor将在Meteor的未来版本中弃用Meteor软件包 - 支持纯npm软件包 - 我将离开这一思路并辞职,在根级别使用
npm install
来处理所有npm软件包我需要使用。 With the above assumption that it cannot be done, it would stand to reason that I won't run into conflicts with other packages using Npm.require()
for a npm package I have added through npm install
. 由于上述假设无法完成,因此我将不会使用
Npm.require()
与我通过npm install
添加的npm包发生冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.