简体   繁体   English

与browserify捆绑在一起时,模块在控制台中显示未定义

[英]modules showing undefined in console upon bundling them with browserify

I am new to browserify and trying to understand it before I shift my project to it. 在将项目转移到浏览器上之前,我是新来的浏览器并试图理解它。

I went through the docs and understood how to use it, I am trying to include some more dependencies but upon usage, they are being stated not defined! 我浏览了文档并了解了如何使用它,我试图包括更多的依赖项,但是根据用法,它们被声明为未定义!

Here are my files: 这是我的文件:

main.js main.js

window.jQuery = $ = require("jquery");
require('bootstrap');
require("jquery-confirm");
require("html2canvas");

package.json 的package.json

{
  "dependencies": {
    "bootstrap": "^3.3.7",
    "html2canvas": "^0.5.0-beta4",
    "jquery": "^3.2.1",
    "jquery-confirm": "^3.3.2",
    "jshint": "^2.9.5",
    "qrcodejs": "^1.0.0"
  }
}

Html2canvas, jshint and qrcode all are being shown undefined when I try to access these through browser console but jquery-confirm seems to work fine. 当我尝试通过浏览器控制台访问Html2canvas,jshint和qrcode时,所有这些都未定义,但jquery-confirm似乎运行良好。

Let me know where I am wrong on this. 让我知道我错了。

-Thanks -谢谢

That's because you're loading them as modules. 那是因为您要将它们作为模块加载。 The whole point of being able to use a bundling system such as Browserify is so you don't have to have global variables. 中能够使用的捆绑系统,如Browserify整点是让你不必须有全局变量。

If you check the return value from one of those return calls, you'll see. 如果您从其中一个返回调用中查看返回值,则会看到。

Example: 例:

var html2canvas= require('html2canvas');
console.log(html2canvas);

If you would like to make them global (and you should really caution against doing this) then you can attach them to the window object. 如果您想将它们设置为全局(并且应该特别注意不要这样做),则可以将它们附加到window对象。

window.html2canvas = require('html2canvas');

But I thoroughly want to suggest that you don't make things global unless you have to. 但是我很想建议您除非必须这样做,否则不要使事情全球化。 For example, you can use jQuery in any of your files just by doing this at the start: 例如,只需在开始时执行以下操作即可在任何文件中使用jQuery:

var $ = require('jquery');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM