【问题标题】:Nodejs Browserify Uncaught TypeError: exists is not a functionNodejs Browserify Uncaught TypeError:存在不是函数
【发布时间】:2017-05-31 14:14:08
【问题描述】:

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

所以我有以下文件结构:

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;

最后启动了 browserify 命令:

$ 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 的工作原理? :)

【问题讨论】:

    标签: javascript node.js browserify


    【解决方案1】:

    我注意到代码中有一些奇怪的地方。

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

    如果是这样,则不推荐使用 fs.exists。根据文档,您可以使用fs.statfs.access 实现相同的效果。但请注意,您应该提供回调(首选)或使用这些方法的同步版本。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-29
      • 2019-11-06
      • 2017-09-18
      • 2020-06-05
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多