browserify无法调用我捆绑的function,它没有定义

我有一些与browserify问题,我想捆绑下面的node.js文件项目upload.js ,我修改了下面的代码的文件,并在upload.js相同的目录下调用upload2.js文件:

var SketchfabDataApi = require( '../../index' ); var Swagger = require('swagger-client'); var fs = require('fs'); var path = require('path'); var api = new SketchfabDataApi(); function UploadModelBySketchfabdataApi(token,idinputfile) { //var file = jQuery(idinputfile)[0].files[0]; var file = document.getElementById(idinputfile).files[0] if (file) { var fileName = file.name; } var fullPathFile = document.getElementById(idinputfile).value; //var fullPathFile = window.location.protocol + "//" + window.location.host; //if (window.location.port != "") fullPathFile += ":" + window.location.port + "/"; //fullPathFile = fullPathFile + '/private/' + fileName; console.info('START UPLOAD2:' + fullPathFile); api.then(function (client) { // This is how you authenticate your requests with the "Token" scheme client.clientAuthorizations.add("Token", new Swagger.ApiKeyAuthorization( "Authorization", "Token " + token , "header" ) ); // This is how you upload a file client.apis.models.post_v3_models({ isPublished: 'false', modelFile: fs.createReadStream(path.resolve(fullPathFile)), private:false, }).then(function (response) { if (response.status === 201) { // The model URI is immediately returned, even if processing hasn't finished yet console.log('After processing, model will be available at: ' + response.headers.location); var uid = response.headers.location .replace('https://api.sketchfab.com/v3/models/', ''); // You can poll the processing status to know when the model is ready // See how `pollStatus` is implemented below pollStatus(client, uid, function (err, res) { console.log(err, res); }); window.location.href = window.location.protocol + "//" + window.location.host + "/stealth/#/stealth/models3d/models3d"; } }).catch(function (error) { console.error("ERROR ON UPLAOD:" + error); }); }).catch(function (error) { console.log("ERROR ON AUTHENTICATION:" + error); }); } /** * Poll processing status * @param {object} client Swagger client * @param {string} uid Model uid * @param {function} callback will receive (err, result) */ function pollStatus(client, uid, callback) { client.apis.models.get_v3_models_uid({ uid: uid }).then(function (response) { if (response.obj.status.processing === 'SUCCEEDED') { callback(null, response.obj.status); } else if (response.obj.status.processing === 'FAILED') { callback(response.obj.status.processing, null); } else { setTimeout(function () { console.log(response.obj.status); pollStatus(client, uid, callback); }, 1000); } }); } 

现在我运行browserify的命令,

 browserify upload2.js -o bundleSketchFabDataApi.js -d 

在这里我的call.js脚本:

  <script type="text/javascript" src="vendor/sketchfab/SketchfabDataApi/bundleSketchFabDataApi.js"></script> <script type="text/javascript" src="vendor/sketchfab/SketchfabDataApi/SketchfabDataApi.js"></script> ............................ UploadModelBySketchfabdataApi("mytoken", "myfile"); 

但我总是在控制台上的相同错误“ 引用是未定义的 ”: 在这里输入图像说明

更新 Ty到dnitrobuild议现在我可以访问我的函数与窗口variables,但我必须继续做一些与browserify错误,因为现在我的机器看不到我的fs模块返回文本fs.createReadStream不是一个像在屏幕截图: 在这里输入图像说明

任何意见,提前。

Browserify不允许variables污染全球范围。 如果你想使用它,你应该把它附加到全局variables。

在这里,如果你想UploadModelBySketchfabdataApi函数可用于window ,你可以附加它:

 window.UploadModelBySketchfabdataApi = function (token, idinputfile) { ... 

更新

Browserify不支持fs模块。 查看兼容性列表 。

您可以使用使用级别文件系统的 browserify-fs 。 他们声称:

fs模块中的所有asynchronous方法都受支持并经过了充分testing(包括链接!)

但要小心浏览器支持:

在这里输入图像说明

安装

 npm install browserify-fs 

用法

直:

 var fs = require('browserify-fs'); 

或者使用常规的fs模块并在捆绑时用browserify-fsreplace它:

 var fs = require('fs'); // CLI browserify main.js -r fs:browserify-fs -o bundle.js 

或者使用常规的fs模块并使用package.json 浏览器来replace它:

 var fs = require('fs'); // package.json "browser": { "fs": "browserify-fs" }