2017-01-16 53 views
4

我是新来Browserify和尝试以下操作: 我创建了一个节点服务器,并试图获得一个所谓的“openbci”在浏览器中运行包。的NodeJS Browserify遗漏的类型错误:存在不是一个函数

所以我有以下文件结构:

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

app.js文件设置服务器,以服务public文件夹

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

然后我创建了以下openBCI.js

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

终于推出了browserif Y命令:

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

但一旦叫我index.html文件,我在Function.getRoot有一个Uncaught TypeError: exists is not a function

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, '..') 
    } 
} 

看来,它无法找到该模块的原始路径。 你能告诉我什么改变?或者,如果我完全理解browserify是如何工作的? :)

回答

1

我注意到,似乎奇怪的代码几件事情。

  1. exists在JavaScript或节点中未定义。它似乎是fs.exists的别名 - 是吗?

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

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

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

相关问题