我正在制作一个简单的create-react-app应用程序,并尝试使用Binpacking NPM软件包(https://github.com/jsmarkus/node-bin-packing)
我收到以下错误:
未捕获的TypeError:Packer不是构造函数
我正在使用NPM软件包,如文档中所述:
import binpack from 'binpacking'
const Packer = binpack.Packer
console.log(binpack)
const packer = new Packer(30,20)
违规行是const Packer = binpack.Packer
NPM软件包的主要文件是(全部)
exports.GrowingPacker = require('./packer.growing.js').GrowingPacker;
exports.Packer = require('./packer.js').Packer;
当我控制台日志binpack
时,我看到GrowingPacker和Packer都未定义。因此,似乎它们没有从子文件“ packer.growing.js”和“ packer.js”正确导入
'packer.js'是
-function (exports) {
var Packer = exports.Packer = function(w, h) {
this.init(w, h);
};
Packer.prototype = {
init: function(w, h) {
this.root = { x: 0, y: 0, w: w, h: h };
},
fit: function(blocks) {
var n, node, block;
for (n = 0; n < blocks.length; n++) {
block = blocks[n];
if (node = this.findNode(this.root, block.w, block.h))
block.fit = this.splitNode(node, block.w, block.h);
}
},
findNode: function(root, w, h) {
if (root.used)
return this.findNode(root.right, w, h) || this.findNode(root.down, w, h);
else if ((w <= root.w) && (h <= root.h))
return root;
else
return null;
},
splitNode: function(node, w, h) {
node.used = true;
node.down = { x: node.x, y: node.y + h, w: node.w, h: node.h - h };
node.right = { x: node.x + w, y: node.y, w: node.w - w, h: h };
return node;
}
}
}(typeof window !== 'undefined' ? window : module.exports);
最后,NPM软件包的目录结构为
我在这里不知所措-我确定这与需求或进口有关,但我迷失了。我尝试在文件中使用“ require”代替“ import”,但是并没有改变。