CordovaPromiseFS 未定义

CordovaPromiseFS is not defined

我无法 cordova-promise-fs 在我的 cordova 应用程序中工作。

我有:index.html 其中包括一个由 browserify 创建的 js 文件。这个js文件包含cordovapromisefs.js、promiscuous.js的内容和我自己的代码,即:

require('../node_modules/cordova-promise-fs/dist/CordovaPromiseFS.js');

var fs = CordovaPromiseFS({
            persistent: true, // or false
            storageSize: 20 * 1024 * 1024, // storage size in bytes, default 20MB 
            concurrency: 3, // how many concurrent uploads/downloads?
            Promise: require('../node_modules/promiscuous/promiscuous.js') // Your favorite Promise/A+ library! 
        });

(function () {
    "use strict";


    document.addEventListener( 'deviceready', onDeviceReady.bind( this ), false );

    function onDeviceReady() {
        console.log(fs);
    };  
} )();

我认为这应该可以完成工作,但这让我想到了 "Uncaught ReferenceError: CordovaPromiseFS is not defined"

希望被指出正确的方向。

尝试从 require:

中分配 returns 的值
var CordovaPromiseFS = require('../node_modules/cordova-promise-fs/dist/CordovaPromiseFS.js');

您也可以使用 webpack 自己将 CordovaPromiseFS 构建为浏览器库。 the repo's readme 建议您克隆源代码和 运行 可用的 prepublish 命令:

npm install webpack -g
npm run-script prepublish

此命令基本上打包 CordovaPromiseFS 的 index.js 文件和打包的 dist 文件,并将其公开给变量 (see the source)。

您正在使用 Browserify 来构建您的包,因此不需要已经捆绑的 cordova-promise-fs/dist/CordovaPromiseFS.js 文件,而是需要模块本身,如下所示:

var CordovaPromiseFS = require('cordova-promise-fs');

这将在您的捆绑包中包含 cordova-promise-fs/index.js 文件,与已经捆绑的文件不同,它是一个导出工厂函数的 CommonJS 模块。