Nodejs Browserify未捕获TypeError:存在不是一个函数

我是Browserify的新手,尝试以下方法:我创build了一个节点服务器,并尝试获取在浏览器上运行的名为“openbci”的包。

所以我有以下文件结构:

Myapp -... -public --app.js --index.html --openBCI.js --... --javascript ---openBCI ----bundle.js ---... -node_modules --openbci ---openBCIBoard.js --browserify --... 

我的app.js文件将服务器设置为公用文件夹

 // app.js var express = require('express'); var app = express(); app.use(express.static('public')); app.listen(myPort); 

然后我创build了以下openBCI.js

 // openBCI.js var OpenBCIBoard = require('openbci').OpenBCIBoard; exports.OpenBCIBoard = OpenBCIBoard; 

最后启动了browserify命令:

 $ browserify public/openBCI.js > public/javascript/openBCI/bundle.js 

但一旦在我的index.html文件中调用,我有一个Uncaught TypeError: exists is not a function Function.getRoot函数:

 exports.getRoot = function getRoot (file) { var dir = dirname(file) , prev while (true) { if (dir === '.') { // Avoids an infinite loop in rare cases, like the REPL dir = process.cwd() } **if (exists(join(dir, 'package.json')) || exists(join(dir, 'node_modules'))) {** // Found the 'package.json' file or 'node_modules' dir; we're done return dir } if (prev === dir) { // Got to the top throw new Error('Could not find module root given file: "' + file + '". Do you have a `package.json` file? ') } // Try the parent dir next prev = dir dir = join(dir, '..') } } 

看来,它找不到模块的原始path。 你能告诉我要改变什么吗? 或者,如果我完全理解浏览器是如何工作的? 🙂

我注意到一些关于代码看起来很奇怪的东西。

  1. exists未定义在JavaScript或节点。 这似乎是fs.exists的别名 – 对吗?

如果是这样,fs.exists已被弃用。 根据文档,您可以使用fs.statfs.access获得相同的效果。 但请注意,您应该提供callback(最好)或使用这些方法的同步版本。

  1. 如果您尝试在浏览器中使用文件系统工具,您将遇到问题,因为您正试图从浏览器访问服务器的文件系统。 有一个插件, browserify-fs ,它给你一个浏览器中的fs等价物。 但是,这似乎访问浏览器的本地IndexedDB,而不是您的服务器上的存储。

我会build议运行的代码依赖于服务器端的服务器端文件,而不是浏览器。