简体   繁体   English

在浏览器中运行 node.js 的问题

[英]Problems running node.js in browser

I'm trying to run an app with node.js functions my browser.我正在尝试使用 node.js 功能运行我的浏览器的应用程序。 I checked in the terminal and my javascript file which includes node.js functions runs well.我检查了终端,我的包含 node.js 函数的 javascript 文件运行良好。 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.但是,当我运行连接到 javascript 文件的 html 文件时,浏览器返回一个错误,指出 require 函数未定义。 I understand that this is because the browser can't run node.js alone我理解这是因为浏览器无法单独运行 node.js

The node.js code I'm trying to run is to access a Watson visual recognition api:我试图运行的 node.js 代码是访问 Watson 视觉识别 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.在连接我的 javascript 文件之前在我的 index.html 中。

However my javascript file still returns the same error that the require function is not defined.但是,我的 javascript 文件仍然返回未定义 require 函数的相同错误。 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)?有什么方法可以在具有 node.js 的浏览器中运行这个 javascript 文件,但特别是不使用 browserify(这在过去导致了一些目录问题)?

While Node and the browser both run JavaScript (ECMAScript), that does not mean that the environment is the same.虽然 Node 和浏览器都运行 JavaScript (ECMAScript),但这并不意味着环境是相同的。

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.)特别是,浏览器中有许多 API 和全局变量在 node(文档、窗口等)中不可用,而 Node 中的其他全局变量和 API 在浏览器中不可用(require 等)。 )

In order to execute code that uses Node specific globals like require() , something needs to define and polyfill those for you.为了执行使用 Node 特定全局变量(如require() ,需要为您定义和填充它们。 That's exactly what something like webpack or browserify or systemjs does for you.这正是 webpack、browserify 或 systemjs 之类的东西为你做的。 Even then you need to make sure the module you are bringing in will run in a browser.即便如此,您也需要确保引入的模块将在浏览器中运行。

Browser doesn't have(yet) any build-in module system like nodejs.浏览器没有(还)任何像 nodejs 这样的内置模块系统。 If you want to use modules, like in node, consider either browserify or requirejs library.如果你想使用模块,比如在 node 中,可以考虑browserifyrequirejs库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM