试图找出将 npm 包加载到 Meteor 包中的方法。具体来说,我尝试使用 future-npm
我试过这样做:
Package.describe({
summary: "Blah blah",
version: '0.0.1'
});
Npm.depends({future: "2.3.1"});
Package.onUse(function (api) {
api.addFiles('lubert.js', 'server');
api.export('Lubert');
});
不幸的是,我收到以下控制台错误
Uncaught ReferenceError: Npm is not defined
我读过 documentation并且没有任何关于加载任何依赖项的内容
我做错了什么?
更新 2:我的 package.js 看起来像
Package.describe({
name: 'trepafi:package',
summary: '',
version: '0.0.3',
git: 'https://github.com/trepafi/meteor-package.git'
});
Npm.depends({
"future": "2.3.1"
});
Package.onUse(function(api) {
api.versionsFrom('1.0');
api.use(['tracker', 'underscore'], ['client']);
api.addFiles(['package.js'], ['client']);
api.export('Package', ['client']);
});
更新 1:我的 package.json 看起来像
{
"name": "trepafi-package",
"version": "0.0.3",
"description": "Package for Meteor",
"repository": {
"type": "git",
"url": "https://github.com/trepafi/meteor-package.git"
},
"author": "Lubert Palacios",
"license": "MIT",
"homepage": "https://github.com/trepafi/meteor-package",
"dependencies": {
"future": "^2.3.1"
}
}
我也尝试过使用 meteorhacks:npm 但没有成功。 如果我能用一种“原生”的方式就好了
最佳答案
您应该在 package.js
文件的末尾对所有 Npm.require
进行分组。
对于 future-npm
。你不需要 package.js
中的 Npm.depends
,愚蠢的。它已经包含在 meteor.. 中,只需 Npm.require
某处,你就可以开始了。为此:
不要对
package.js
名称含糊不清。请改用trepafi:package.js
。您不需要
package.json
..Npm.depends
已满足您的需求。删除这个:
api.addFiles(['package.js'], ['client']);
因为它看起来像一个循环依赖.. Yo dawg i把 package.js 放在 package.js 中。.. 不酷,Xzibit。并且由于 Npm.require 只能在服务器端工作,因此您需要将
trepafi:package.js
作为服务器端包含在内。例如:api.addFiles(['trepafi:package.js'], ['server']);
所以你的结构至少应该是:
trepafi:package/
- package.js
- trepafi:package.js
- <other files..>
Future
不需要 package.json.. 它已经包含在 Meteor 中。
您的 package.js
应该如下所示:
Package.describe({
name: 'trepafi:package',
summary: '',
version: '0.0.3',
git: 'https://github.com/trepafi/meteor-package.git'
});
Package.onUse(function(api) {
api.versionsFrom('METEOR@1.0');
api.use(['tracker', 'underscore','meteor'], ['client']);
api.addFiles(['trepafi:package.js'], ['server']);
api.export('Package', ['client']);
});
//if you really need Npm.depends:
Npm.depends({
'prerender-node': '1.0.6',
'send' : '0.10.1'
});
// we don't need no package.json
您的 trepafi:package.js
应该如下所示:
var Future = Npm.require('future');
var future = new Future();
// use your future to make Doc Brown proud.
var useFuture = function(asyncFunc) { //expects function with callback somewhere
asyncFunc(function(err, result) {
if(err) future.throw("OMG something went wrong!");
else return future.return(result);
});
return future.wait();
};
Meteor.startup(function() {
//something
});
阅读:
package.js
中的显着变化是 versionsFrom
和 api.use
现在添加了 meteor
。
祝你好运!
关于meteor - 在 Meteor 包中使用 npm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116328/