javascript 包含 NPM 包时,Meteor.js 中的“需要未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20040061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:33:21  来源:igfitidea点击:

'require is not defined' in Meteor.js when including NPM package

javascriptnode.jsmeteornpmmeteorite

提问by Nyxynyx

I'm trying to use a npm package from Meteor.js (Release 0.6.6.3)using Meteor.require. However it throws an error saying that requireis not defined. Why is this and how can we solve it?

我试图使用从Meteor.js一个NPM包(版本0.6.6.3)使用Meteor.require。但是它抛出一个错误,说require没有定义。为什么会这样,我们该如何解决?

mrt add npm
npm install github

packages.json

包.json

{
    "github": "0.1.8"
}

github.js

github.js

var GITHUB = Meteor.require('github');

Error

错误

ReferenceError: require is not defined


The npm package has lines such as

npm 包中有几行,例如

var https = require('https')
var url = require('url')
var crypto = require('crypto')

Must the package's code be manually edited to use Npm.require? Editing them manually got rid of the errors.

必须手动编辑包的代码才能使用Npm.require吗?手动编辑它们消除了错误。

However theres a line:

但是有一行:

module.exports = SOMETHING

How should we call modulefrom within meteor?

我们应该如何module从流星内部呼叫?

回答by Andrew Mao

Meteor.requireis a function added by the meteor npm smart package, which actually doesn't do much for using npm other than wrapping some asynchronous callbacks. It's a few months old, so you might want to try using Meteor's Npm.requiredirectly in case something broke.

Meteor.require流星 npm 智能包添加的一个函数,除了包装一些异步回调之外,它实际上对使用 npm 没有太大作用。它已经有几个月的历史了,因此您可能想尝试Npm.require直接使用 Meteor ,以防万一出现问题。

The monkey-patching of the Meteorglobal by this package is misleading.

Meteor这个包对全局的猴子补丁具有误导性。

回答by user728291

Making comments above an answer.

在答案上方发表评论。

Is Meteor.require() a typo? That is what is in your code though your question text refers to the correct Npm.require().

Meteor.require() 是错字吗?尽管您的问题文本指的是正确的 Npm.require(),但这就是您的代码中的内容。

I think module.exports is there for non-meteor use of the same file. Within meteor variables for export should be

我认为 module.exports 可用于非流星使用同一文件。在用于导出的流星变量中应该是

  1. declared as globals inside the package
  2. exported with api.export() within the package.js file.
  1. 在包内声明为全局变量
  2. 在 package.js 文件中使用 api.export() 导出。

The documentation on this is a bit rough but look at namespacingand writing packages. Also looking into the various meteor packageson github is very useful.

关于此的文档有点粗糙,但请查看命名空间编写包。在 github 上查看各种meteor 包也非常有用。

回答by FreePender

Make sure you're using the meteor-npmpackage.

确保您正在使用该meteor-npm软件包。

https://www.npmjs.com/package/meteor-npm

https://www.npmjs.com/package/meteor-npm

回答by StormTrooper

Use Npm.require() in meteor.

在流星中使用 Npm.require()。

Like this:

像这样:

var fs = Npm.require("fs");

For that you need to have a Meteor package: meteorhacks:npm, npm-container

对于您需要有一个流星包:meteorhacks:npmnpm-container