未定义Meteor ReferenceError Buffer

时间:2017-06-19 06:48:12

标签: node.js meteor

我正在运行Meteor 1.4.2.3并在添加一些处理文件的代码后,我得到一个引用错误,说“缓冲区”没有定义。

错误在util.js

exports.isPrimitive = isPrimitive; exports.isBuffer = Buffer.isBuffer;

Uncaught ReferenceError: Buffer is not defined
    at util.js (modules.js:29525)
    at fileEvaluate (modules-runtime.js:343)
    at require (modules-runtime.js:238)
    at meteorInstall.node_modules.meteor-node-stubs.node_modules.readable-stream.lib._stream_readable.js (modules.js:25209)
    at fileEvaluate (modules-runtime.js:343)
    at require (modules-runtime.js:238)
    at meteorInstall.node_modules.meteor-node-stubs.node_modules.readable-stream.readable.js (modules.js:25144)
    at fileEvaluate (modules-runtime.js:343)
    at require (modules-runtime.js:238)
    at meteorInstall.node_modules.meteor-node-stubs.node_modules.stream-browserify.index.js (modules.js:24653)
    at fileEvaluate (modules-runtime.js:343)
    at require (modules-runtime.js:238)
    at meteorInstall.node_modules.gm.index.js (modules.js:21644)
    at fileEvaluate (modules-runtime.js:343)
    at require (modules-runtime.js:238)
    at meteorInstall.both.collections.files.collection.js (app.js:2317)
    at fileEvaluate (modules-runtime.js:343)
    at require (modules-runtime.js:238)
    at app.js:2583

Buffer的依赖是什么?我错过了一个包吗?

2 个答案:

答案 0 :(得分:2)

Buffer是Node.js中的内置类,并不存在于浏览器中。如果存在单独的浏览器兼容版本的依赖项,请尝试使用该版本。如果没有,NPM中有一个名为buffer的包可用作浏览器,React Native和其他非Node.js环境中的polyfill。

import Buffer from 'buffer';

if (typeof this.Buffer === 'undefined') {
  this.Buffer = Buffer.Buffer;
}

答案 1 :(得分:0)

在Startup上添加此代码对我来说很有效

 global.Buffer = function() {}
 global.Buffer.isBuffer = () => false

完整代码如下

Meteor.startup(()=> {
  global.Buffer = function() {}
  global.Buffer.isBuffer = () => false
  ReactDOM.render(<App />, document.getElementById('container'));
});