在浏览器中运行node

在浏览器中运行node

本文介绍了在浏览器中运行node.js的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用我的浏览器运行一个具有node.js功能的应用程序。我检查了终端,并且包含node.js函数的JavaScript文件运行良好。然而,当我运行连接到JavaScript文件的html文件时,浏览器返回一个错误,说明require函数没有被定义。我知道这是因为浏览器无法单独运行node.js



我尝试运行的node.js代码是访问Watson视觉识别api :

  var watson = require('./ node_modules / watson-developer-cloud'); 

watson.visual_recognition({
用户名:'49f5d504-9387-45c6-9fda-9b58a9afc209',
密码:'IITqAn0VPaFr',
版本:'v2- ('),
version_date:'2015-12-02'
})。listClassifiers({},function(err,response){
if(err){
console。 log(err);
} else {
console.log(JSON.stringify(response,null,2));
}
});

我知道我拥有所有必需的文件,因为文件在终端中运行。因此,我继续包括:

 < script src =https://cdn.socket.io/socket.io- 1.4.5.js>< /脚本> 

在我的index.html连接我的javascript文件之前。 然而,我的JavaScript文件仍然返回相同的错误,即require函数未定义。难道我做错了什么?有没有什么办法可以在浏览器中运行这个JavaScript文件,它具有node.js,但是特别是没有使用browserify(这在过去导致了一些目录问题)?

解决方案

虽然Node和浏览器都运行JavaScript(ECMAScript),但这并不意味着环境是相同的。



,在浏览器中可用的许多API和全局变量不在节点(文档,窗口等)以及Node中的其他全局和API中不可用(例如,需要等)



为了执行像 require()那样使用特定于Node的全局变量的代码,需要定义和填充用于您。这正是webpack或者browserify或者systemjs为你所做的。即使那样你也需要确保你所带的模块会在浏览器中运行。


I'm trying to run an app with node.js functions my browser. I checked in the terminal and my javascript file which includes node.js functions runs well. However when I run the html file which is connected to the javascript file the browser returns an error which says the require function is not defined. I understand that this is because the browser can't run node.js alone

The node.js code I'm trying to run is to access a Watson visual recognition api:

var watson = require('./node_modules/watson-developer-cloud');

watson.visual_recognition({
    username : '49f5d504-9387-45c6-9fda-9b58a9afc209',
    password : 'IITqAn0VPaFr',
    version : 'v2-beta',
    version_date : '2015-12-02'
}).listClassifiers({}, function(err, response) {
    if (err){
        console.log(err);
    } else {
        console.log(JSON.stringify(response, null, 2));
    }
});

I know I have all of the required files because the file runs in the terminal. Therefore I proceeded to include:

<script src ="https://cdn.socket.io/socket.io-1.4.5.js"></script>

in my index.html before connecting my javascript file.

However my javascript file still returns the same error that the require function is not defined. Am I doing something wrong? Is there any way I could run this javascript file in the browser that has node.js but specifically without using browserify (which caused me some directory problems in the past)?

解决方案

While Node and the browser both run JavaScript (ECMAScript), that does not mean that the environment is the same.

In particular, there are many APIs and globals available in the browser that aren't available in node (document, window, etc.) and other globals and APIs that are in Node that aren't available in the browser (require, etc.)

In order to execute code that uses Node specific globals like require(), something needs to define and polyfill those for you. That's exactly what something like webpack or browserify or systemjs does for you. Even then you need to make sure the module you are bringing in will run in a browser.

这篇关于在浏览器中运行node.js的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:45