【问题标题】:Node Module Issue节点模块问题
【发布时间】:2017-08-29 16:22:19
【问题描述】:

我根本不是 Node 方面的专家,但我正在学习!我最近遇到了一个我想包含在项目“响应式背景”https://www.npmjs.com/package/responsive-backgrounds 中的模块。

所有都安装了npm install --save-dev responsive-backgrounds 并且看起来很好(即我可以在我的缩小和丑化的分发 JS 中看到 ResponsiveBackgrounds)。在我的主 JS 文件中,我有:

require("responsive-backgrounds");
options = {
    lazy: true,
    transition: 0.5
};
new ResponsiveBackground("#featuredimage", options);

但是,当我运行浏览器化和丑化的 JS 时,我得到了 Uncaught ReferenceError: ResponsiveBackground is not defined。我错过了一些重要的东西吗?我敢肯定还有其他库可以完成相同的工作甚至更好,但这更多是为了了解如何正确包含节点模块。

【问题讨论】:

    标签: node.js npm browserify


    【解决方案1】:

    需要为您尝试导入的模块设置变量 试试:

      let ResponsiveBackground = require("responsive-backgrounds");
        options = {
            lazy: true,
            transition: 0.5
        };
        new ResponsiveBackground("#featuredimage", options);
    

    【讨论】:

    • 在我的例子中,我使用了 'var' 而不是 'let'。完整的代码是:` var ResponsiveBackground = require("responsive-backgrounds"); jQuery(document).ready(function() { options = { lazy: true, transition: 0 }; new ResponsiveBackground("#featuredimage", options); }); ` 知道为什么 'let' 会产生错误吗?
    • 哦,我没有意识到这是在浏览器中,出于某种原因以为您使用的是 node.js。据我所知,浏览器尚不支持 es6 语法 let/const,但在 node.js 中。这是关于 let developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 的一些文档
    • 实际上我在使用 Node 时对为什么会失败感到有些困惑。当我运行“browserify”时,我预计“let”会被换掉,但这似乎没有发生。我知道这取决于我的理解,而不是其他任何事情。
    • 你使用的是什么版本的节点?
    • nodejs 是 v6.11.3 而 npm(如果有帮助的话)是 5.3.0
    猜你喜欢
    • 2015-03-31
    • 1970-01-01
    • 2017-10-03
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2019-07-02
    相关资源
    最近更新 更多