2017-08-29 101 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