meteor - 在 Meteor 包中使用 npm

标签 meteor npm

试图找出将 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 某处,你就可以开始了。为此:

  1. 不要对 package.js 名称含糊不清。请改用 trepafi:package.js

  2. 您不需要 package.json.. Npm.depends 已满足您的需求。

  3. 删除这个:api.addFiles(['package.js'], ['client']); 因为它看起来像一个循环依赖.. Yo dawg i把 package.js 放在 package.js 中。.. 不酷,Xzibit。

  4. 并且由于 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 中的显着变化是 versionsFromapi.use 现在添加了 meteor

祝你好运!

关于meteor - 在 Meteor 包中使用 npm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116328/

相关文章:

javascript - Cordova BarcodeScanner 连续读取

javascript - TRACKER : error TRK0005: Failed to locate: "CL.exe". 系统找不到指定的文件

javascript - 使用 npm 安装 font awesome 5 以供 scss 使用

node.js - Windows 11 - npm 错误! errno -4058 - 代码 ENOENT bash

NPM 坚持使用错误的注册表 URL

javascript - 用另一个包完全覆盖 NPM 包

javascript - 如何在 meteor 中正确使用 fromNow()、momentjs 表达式?

Meteor查询排序错误 "Requests.find(...).sort is not a function"

javascript - MeteorJS : Error in Meteor. 调用

node.js - 由 bcrypt 引起的无效 ELF header