流星:ReferenceError:需要未定义 [英] Meteor: ReferenceError: require is not defined

查看:61
本文介绍了流星:ReferenceError:需要未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了meteorhacks:npm 并在packages.json {"apn": "1.6.2"} 中定义了apn.但是,当我运行以下代码时,出现错误 ReferenceError: require is not defined:

I have installed meteorhacks:npm and defined apn in packages.json {"apn": "1.6.2"}. However, I get an error ReferenceError: require is not defined when I run the following code:

var apn = Npm.require('apn'),
    path = Npm.require('path'),
    apnOptions = Meteor.settings.apnOptions || {},
    alertSound = apnOptions.sound || "alert.aiff",
    apnConnection

// default apn connection options
apnOptions = _.extend({
  cert: path.join(appRootPath, "private", "cert.pem"),
  key: path.join(appRootPath, "private", "key.pem"),
}, apnOptions)
apnConnection = new apn.Connection(apnOptions)

完整的错误信息:

W20150116-23:30:51.097(2)? (STDERR)           
W20150116-23:30:51.100(2)? (STDERR) /Users/Mika/.meteor/packages/meteor-tool/.1.0.38.zbazwc++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:173
W20150116-23:30:51.101(2)? (STDERR)                         throw(ex);
W20150116-23:30:51.101(2)? (STDERR)                               ^
W20150116-23:30:51.101(2)? (STDERR) ReferenceError: require is not defined
W20150116-23:30:51.102(2)? (STDERR)     at app/node_modules/apn/lib/connection.js:1:49
W20150116-23:30:51.102(2)? (STDERR)     at app/node_modules/apn/lib/connection.js:774:3
W20150116-23:30:51.106(2)? (STDERR)     at /Users/Mika/Devaus/meteor/flashcards/.meteor/local/build/programs/server/boot.js:175:10
W20150116-23:30:51.107(2)? (STDERR)     at Array.forEach (native)
W20150116-23:30:51.107(2)? (STDERR)     at Function._.each._.forEach (/Users/Mika/.meteor/packages/meteor-tool/.1.0.38.zbazwc++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
W20150116-23:30:51.107(2)? (STDERR)     at /Users/Mika/Devaus/meteor/flashcards/.meteor/local/build/programs/server/boot.js:86:5

我的仓库:https://github.com/mvaisanen/flashcards

推荐答案

使用 Meteor.npmRequire insted of Npm.require 用于通过 meteorhacks:npm.例如:

Use Meteor.npmRequire insted of Npm.require for packages loaded via meteorhacks:npm. For example:

var apn = Meteor.npmRequire('apn');
var path = Npm.require('path');

另请注意,需要 npm 包仅适用于服务器,因此请确保代码位于您的 /server 目录中或由 Meteor.isServer 保护.

Also note that requiring an npm package will only work on the server, so make sure that code is in your /server directory or guarded by Meteor.isServer.

这篇关于流星:ReferenceError:需要未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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