【问题标题】:Meteor cannot find module "module"流星找不到模块“模块”
【发布时间】:2014-02-04 23:23:46
【问题描述】:

我正在尝试在流星中安装Spooky 模块(这个在我的公共文件夹中:app/public/node_modules)。

我已阅读答案in this post 并在 server/server.js 中添加了以下代码

Meteor.startup ->
    path = Npm.require 'path'
    fs = Npm.require 'fs'
    base = path.resolve '.'
    isBundle = fs.existsSync base + '/bundle'
    modulePath = base + (if isBundle then '/bundle/static' else '/public') + '/node_modules'
    spooky = Npm.require modulePath + '/spooky'

但是当我运行流星时,我得到:

Error: Cannot find module '/Users/mac/Documents/websites/app/.meteor/local/build/programs/server/public/node_modules/spooky'

【问题讨论】:

  • 只需使用 -g (npm install -g packagename) 安装软件包。 public 文件夹朝外,每个人都可以看到。你不想要那个。加上流星/节点它没有在那里找到它。

标签: meteor


【解决方案1】:

您需要创建一个智能包以在您的应用中使用 Npm 模块。或者你可以使用meteor-npm。

您不能将Npm.require 单独用于像 spooky 这样的非标准 npm 模块。

如果你使用meteor-npm,你可以用meteorite安装它:mrt add npm

然后在您将模块添加到您的 packages.json 之后使用 Meteor.require("spooky")。您可以在这里查看更多详细信息:http://meteorhacks.com/complete-npm-integration-for-meteor.html

官方的做法是制作自己的智能包来包裹npm模块。有这样一个包的例子:https://github.com/avital/meteor-xml2js-npm-demo

该示例使用 xml2js 作为 npm 模块,但您可以交换名称以使其变得怪异。

然后您可以将此包添加到您的/packages 文件夹中(例如名称为spooky),然后使用meteor add spooky 将其添加到您的流星项目中。

atmote.meteor.com 上的包有更多这样的例子,它们几乎做同样的事情(例如条纹 (https://atmosphere.meteor.com/package/stripe))。

【讨论】:

  • 谢谢阿克沙特! :) 顺便说一句,您将“模块写入您的 package.json”。它应该是 packages.json。
【解决方案2】:

The articleAkshat linked to 已更新:

cd project
meteor add meteorhacks:npm

编辑project/packages.json:

{
  "redis": "0.8.2",
  "github": "0.1.8"
}

使用那些 npm 模块:

var Github = Meteor.npmRequire('github');
var github = new Github();

github.gists.getFromUser({user: 'arunoda'}, function(err, gists) {
  console.log(gists);
});

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 2017-05-30
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2018-03-26
    • 2016-12-23
    相关资源
    最近更新 更多