簡體   English   中英

node.js和browserify錯誤

[英]node.js and browserify error

我在設置node.js客戶端以連接到Java中的套接字時遇到困難。 瀏覽器總是給我這個錯誤

Uncaught ReferenceError: require is not defined

我在我的html頁面中使用了它

<script type="text/javascript">

    $(function() {

        // open websocket

        var net = require('net');
        var socket = net.connect(8000, 'localhost');

    })
    </script>

我已經使用git clone https://github.com/joyent/node.git安裝了node.js,並通過sudo npm install -g browserify 我使用sudo是因為它引發了permission error ,我應該以管理員級別執行命令。

安裝browserify后,我的瀏覽器仍然出現錯誤

Uncaught ReferenceError: require is not defined 

我了解我的錯誤,我正在嘗試將nodejs導入html,這是不適用的。 我做的下一件事是當我嘗試使用browserify index.js -o bundle.js時顯示的錯誤

/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:906
      ret = Buffer.concat(list, length);
                   ^
TypeError: Object function Buffer(subject, encoding, offset) {
  if (!(this instanceof Buffer)) {
    return new Buffer(subject, encoding, offset);
  }

  var type;

  // Are we slicing?
  if (typeof offset === 'number') {
    this.length = coerce(encoding);
    this.parent = subject;
    this.offset = offset;
  } else {
    // Find the length
    switch (type = typeof subject) {
      case 'number':
        this.length = coerce(subject);
        break;

      case 'string':
        this.length = Buffer.byteLength(subject, encoding);
        break;

      case 'object': // Assume object is an array
        this.length = coerce(subject.length);
        break;

      default:
        throw new Error('First argument needs to be a number, ' +
                        'array or string.');
    }

    if (this.length > Buffer.poolSize) {
      // Big buffer, just alloc one.
      this.parent = new SlowBuffer(this.length);
      this.offset = 0;

    } else {
      // Small buffer.
      if (!pool || pool.length - pool.used < this.length) allocPool();
      this.parent = pool;
      this.offset = pool.used;
      pool.used += this.length;
    }

    // Treat array-ish objects as a byte array.
    if (isArrayIsh(subject)) {
      for (var i = 0; i < this.length; i++) {
        this.parent[i + this.offset] = subject[i];
      }
    } else if (type == 'string') {
      // We are a string
      this.length = this.write(subject, 0, encoding);
    }
  }

  SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
} has no method 'concat'
    at fromList (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:906:20)
    at Transform.read (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:373:11)
    at flow (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:629:52)
    at Array.0 (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:600:7)
    at EventEmitter._tickCallback (node.js:190:38)

這是browserify錯誤嗎? 我的nodejs的例子

server.js

var http = require("http");
var url = require("url");

function start(route) {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    route(pathname);

    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
  }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;

router.js

function route(pathname) {
  console.log("About to route a request for " + pathname);
}
exports.route = route;

index.js

var server = require("./server");
var router = require("./router");

server.start(router.route);

是我的參考。

您已經錯過了browserify工作流程中的重要步驟。 browserify的作用是將您的javascript從節點內的作品轉換為瀏覽器內的作品,這需要在javascript上實際運行browserify命令行工具,這會為您生成一個新的javascript文件(“捆綁包” ),然后您可以在<script>標記中引用它,然后將其加載到瀏覽器中並正常工作。 再次閱讀教程,並注意該步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM