2017-08-29 103 views
0

我不是Node的专家,但我正在学习!我最近遇到了一个模块,我想将其纳入项目“responsive-backgrounds”https://www.npmjs.com/package/responsive-backgrounds节点模块问题

所有与npm install --save-dev responsive-backgrounds一起安装,似乎很好(即我可以看到ResponsiveBackgrounds在我的minifed和uglified distributon JS)。我主要的JS文件我有:

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

但是我得到Uncaught ReferenceError: ResponsiveBackground is not defined运行我browserified和变丑JS。我错过了关键的东西吗?我确信有其他库可以完成相同的工作,甚至更好,但这更多的是了解如何正确包含节点模块。

回答

0

需要设置变量模块youre设法导入 尝试:

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

在我来说,我用来代替“让”变种'。完整的代码是: ' var ResponsiveBackground = require(“responsive-backgrounds”); jQuery的(文件)。就绪(函数() { \t选项= { \t \t懒:真, \t \t过渡:0 \t}; \t新ResponsiveBackground( “#featuredimage”,选项); }); '任何想法为什么'让'产生一个错误? – Zakalwe

+0

哦,我没有意识到这是在浏览器中,出于某种原因,认为你使用node.js.据我所知,es6语法let/const在浏览器中不支持,但在节点中。这里是一些关于让https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let –

+0

的文档其实我正在使用节点,所以有点困惑,为什么这会摔倒。当我运行“browserify”时,我预计“let”会被换掉,但这似乎没有发生。我知道这是根据我的理解而不是别的。 – Zakalwe