keeweb / kdbxweb

Web Kdbx library
https://app.keeweb.info
MIT License
413 stars 57 forks source link

window is not defined - running under NodeJS #26

Closed perry-mitchell closed 5 years ago

perry-mitchell commented 5 years ago

Hi! We use your library over at Buttercup, and we're upgrading kdbxweb. It seems we've encountered an issue where the current copy of this library references window:

/*! kdbxweb v1.5.2, (c) 2019 Antelle, opensource.org/licenses/MIT */
(function webpackUniversalModuleDefinition(root, factory) {
    if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory(require("crypto"), require("xmldom"));
    else if(typeof define === 'function' && define.amd)
        define(["crypto", "xmldom"], factory);
    else if(typeof exports === 'object')
        exports["kdbxweb"] = factory(require("crypto"), require("xmldom"));
    else
        root["kdbxweb"] = factory(root["crypto"], root["xmldom"]);
})(window, function(__WEBPACK_EXTERNAL_MODULE__42__, __WEBPACK_EXTERNAL_MODULE__44__) {
return /******/ (function(modules) { // webpackBootstrap

Which is of course undefined:

ReferenceError: window is not defined
    at eval (webpack:///./node_modules/kdbxweb/dist/kdbxweb.js?:4:4)
    at Object../node_modules/kdbxweb/dist/kdbxweb.js (/Users/pez/git/buttercup-importer/dist/buttercup-importer.js:2311:1)
    at __webpack_require__ (/Users/pez/git/buttercup-importer/dist/buttercup-importer.js:21:30)
    at eval (webpack:///./source/importers/KDBXImporter.js?:2:15)

This only cropped up after updating from 1.2.7 to 1.5.2. I'm using Node v10 at the moment.

perry-mitchell commented 5 years ago

Ok, I fixed the issue myself as I'm using Webpack to bundle for node+browser - Using imports-loader I simply do: const KDBXWeb = require("imports-loader?window=>this!kdbxweb");

This of course doesn't fix it on Node, but only if you bundle this library further.

antelle commented 5 years ago

Fixed this, it should work well in 1.5.3.

perry-mitchell commented 5 years ago

This.. is probably the fastest response and fix I've ever seen. Amazing @antelle!