通过ES6导入节点模块

时间:2016-05-26 09:49:18

标签: javascript node.js import npm ecmascript-6

我想用NPM导入PhotoSwipe库。 虽然它不仅适用于PhotoSwipe,而且适用于NPM模块,我猜。

如果https://www.npmjs.com/package/photoswipe中提到的npm i photoswipe -S安装了。 我没有运行任何笨拙的任务,因为我看到dist地图不是空的。

我只使用vanilla JS,所以我的代码如下:

import 'photoswipe';

const galleryElem = document.querySelector('.gallery');

if (typeof(galleryElem) !== 'undefined' && galleryElem !== null) {
    const imgArray = [];
    const galleryImgs = document.querySelectorAll('.gallery__image');

    // Fill imgArray trough looping.. (skipped for this post)

    const gallery = new window.PhotoSwipe(galleryElem, false, imgArray);
    gallery.init();
}

但不幸的是我得到了错误: TypeError: window.PhotoSwipe is not a constructor

导入为import * as PhotoSwipe from 'photoswipe';会导致同样的错误。

我也尝试导入以下代码https://gist.github.com/di5abled/d8d84af3be5e1bf12507

中的库

然后我收到错误:Cannot find module ..

我的所有JS文件都捆绑在页脚中加载的名为app.js的文件中。

所以我不确定如何导入库并正确使用它。 提前感谢您的帮助。

0 个答案:

没有答案
相关问题