gpt4 book ai didi

javascript - Node.js 从 0.10.2 更新到 0.12.2 后出现错误 "ReferenceError: global is not defined"

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:46 26 4
gpt4 key购买 nike

以下代码用于使用 Node.js 在服务器上执行沙盒 JavaScript 代码并重定向控制台。

在我们将版本从 Node.js 0.10.2 更新到 Node.js 0.12.2 之前,它发挥了作用

var EOL = require('os').EOL;

DataView = undefined;
DTRACE_NET_SERVER_CONNECTION = undefined;
// DTRACE_NET_STREAM_END = undefined;
DTRACE_NET_SOCKET_READ = undefined;
DTRACE_NET_SOCKET_WRITE = undefined;
DTRACE_HTTP_SERVER_REQUEST = undefined;
DTRACE_HTTP_SERVER_RESPONSE = undefined;
DTRACE_HTTP_CLIENT_REQUEST = undefined;
DTRACE_HTTP_CLIENT_RESPONSE = undefined;
COUNTER_NET_SERVER_CONNECTION = undefined;
COUNTER_NET_SERVER_CONNECTION_CLOSE = undefined;
COUNTER_HTTP_SERVER_REQUEST = undefined;
COUNTER_HTTP_SERVER_RESPONSE = undefined;
COUNTER_HTTP_CLIENT_REQUEST = undefined;
COUNTER_HTTP_CLIENT_RESPONSE = undefined;
global = undefined;
process.argv = undefined;
process.versions = undefined;
process.env = { NODE_DEBUG: false };
process.addListener = undefined;
process.EventEmitter = undefined;
process.mainModule = undefined;
process.removeListener = undefined;
process.config = undefined;
// process.on = undefined;
process.openStdin = undefined;
process.chdir = undefined;
process.cwd = undefined;
process.umask = undefined;
GLOBAL = undefined;
root = undefined;
setTimeout = undefined;
setInterval = undefined;
clearTimeout = undefined;
clearInterval = undefined;
setImmediate = undefined;
clearImmediate = undefined;
module = undefined;
require = undefined;
msg = undefined;

delete DataView;
delete DTRACE_NET_SERVER_CONNECTION;
// delete DTRACE_NET_STREAM_END;
delete DTRACE_NET_SOCKET_READ;
delete DTRACE_NET_SOCKET_WRITE;
delete DTRACE_HTTP_SERVER_REQUEST;
delete DTRACE_HTTP_SERVER_RESPONSE;
delete DTRACE_HTTP_CLIENT_REQUEST;
delete DTRACE_HTTP_CLIENT_RESPONSE;
delete COUNTER_NET_SERVER_CONNECTION;
delete COUNTER_NET_SERVER_CONNECTION_CLOSE;
delete COUNTER_HTTP_SERVER_REQUEST;
delete COUNTER_HTTP_SERVER_RESPONSE;
delete COUNTER_HTTP_CLIENT_REQUEST;
delete COUNTER_HTTP_CLIENT_RESPONSE;
delete global;
delete process.argv;
delete process.versions;
delete GLOBAL;
delete root;
delete setTimeout;
delete setInterval;
delete clearTimeout;
delete clearInterval;
delete setImmediate;
delete clearImmediate;
delete module;
delete require;
delete msg;

var content = '';
process.stdin.resume();
process.stdin.on('data', function(buf) { content += buf.toString(); });
process.stdin.on('end', function() {
var inputData = content.trim().split(EOL);
var result = code.run(inputData);
if (result !== undefined) {
console.log(result);
}
});

var code = {
run: function Solve(args) {
console.log('works');
}
};

现在代码抛出异常:

util.js:67
if (isUndefined(global.process)) {
^
ReferenceError: global is not defined
at Object.exports.deprecate (util.js:67:19)
at net.js:1414:34
at NativeModule.compile (node.js:805:5)
at NativeModule.require (node.js:774:18)
at tty.js:23:11
at NativeModule.compile (node.js:805:5)
at Function.NativeModule.require (node.js:774:18)
at process.stdin (node.js:533:34)
at Object.<anonymous> (C:\Program Files\nodejs\test.js:75:8)
at Module._compile (module.js:460:26)

我的第 75 行是:

process.stdin.resume();

问题是否与流程变量有关,如果是,我如何在 Node.js 0.12.2 中使用 stdin/stdout 进行读/写?

此外,如果问题出在 process.stdin.resume(); 上,我如何使用 Node.js 0.12.2 获得相同的结果?

编辑

作为documentation

The process object is a global object and can be accessed from anywhere.

这更令人困惑,因为异常是 ReferenceError: global is not Definedprocess.stdin.resume(); 行。

最佳答案

全局 = 未定义;

删除全局;

不要这样做。

关于javascript - Node.js 从 0.10.2 更新到 0.12.2 后出现错误 "ReferenceError: global is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29428500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com