将 `require()` 与 `node --eval` 一起使用

     2023-03-07     47

关键词:

【中文标题】将 `require()` 与 `node --eval` 一起使用【英文标题】:Use `require()` with `node --eval` 【发布时间】:2011-06-02 17:27:22 【问题描述】:

在使用 node.js 对 --eval 的新支持时,当我尝试使用 require() 时出现错误 (ReferenceError: require is not defined)。

这是一个失败的例子:

$ node --eval 'require("http");'

undefined:1

^
ReferenceError: require is not defined
    at eval at <anonymous> (node.js:762:36)
    at eval (native)
    at node.js:762:36
$

这是一个使用输入到 REPL 中的 require() 的工作示例:

$ node
> require("http");
 STATUS_CODES: 
    '100': 'Continue'
   , '101': 'Switching Protocols'
   , '102': 'Processing'
   , '200': 'OK'
   , '201': 'Created'
   , '202': 'Accepted'
   , '203': 'Non-Authoritative Information'
   , '204': 'No Content'
   , '205': 'Reset Content'
   , '206': 'Partial Content'
   , '207': 'Multi-Status'
   , '300': 'Multiple Choices'
   , '301': 'Moved Permanently'
   , '302': 'Moved Temporarily'
   , '303': 'See Other'
   , '304': 'Not Modified'
   , '305': 'Use Proxy'
   , '307': 'Temporary Redirect'
   , '400': 'Bad Request'
   , '401': 'Unauthorized'
   , '402': 'Payment Required'
   , '403': 'Forbidden'
   , '404': 'Not Found'
   , '405': 'Method Not Allowed'
   , '406': 'Not Acceptable'
   , '407': 'Proxy Authentication Required'
   , '408': 'Request Time-out'
   , '409': 'Conflict'
   , '410': 'Gone'
   , '411': 'Length Required'
   , '412': 'Precondition Failed'
   , '413': 'Request Entity Too Large'
   , '414': 'Request-URI Too Large'
   , '415': 'Unsupported Media Type'
   , '416': 'Requested Range Not Satisfiable'
   , '417': 'Expectation Failed'
   , '418': 'I\'m a teapot'
   , '422': 'Unprocessable Entity'
   , '423': 'Locked'
   , '424': 'Failed Dependency'
   , '425': 'Unordered Collection'
   , '426': 'Upgrade Required'
   , '500': 'Internal Server Error'
   , '501': 'Not Implemented'
   , '502': 'Bad Gateway'
   , '503': 'Service Unavailable'
   , '504': 'Gateway Time-out'
   , '505': 'HTTP Version not supported'
   , '506': 'Variant Also Negotiates'
   , '507': 'Insufficient Storage'
   , '509': 'Bandwidth Limit Exceeded'
   , '510': 'Not Extended'
   
, IncomingMessage:  [Function: IncomingMessage] super_: [Function: EventEmitter] 
, OutgoingMessage:  [Function: OutgoingMessage] super_: [Function: EventEmitter] 
, ServerResponse:  [Function: ServerResponse] super_: [Circular] 
, ClientRequest:  [Function: ClientRequest] super_: [Circular] 
, Server: 
    [Function: Server]
     super_:  [Function: Server] super_: [Function: EventEmitter]  
, createServer: [Function]
, Client: 
    [Function: Client]
     super_:  [Function: Stream] super_: [Function: EventEmitter]  
, createClient: [Function]
, cat: [Function]

> 

有没有办法将require() 与节点的--eval 一起使用?

我在 Mac OS X 10.6.5 上的节点 0.2.6 上。

【问题讨论】:

【参考方案1】:

不,至少在2.x 中没有。

通过查看2.6 的source:

 else if (process._eval) 
  // -e, --eval
  var indirectEval= eval; // so the eval happens in global scope.
  if (process._eval) console.log(indirectEval(process._eval));

很明显,Node.js 没有为 eval 代码设置任何包装器代码,因此 require 等不可用。

但这是3.x 版本中的changed:

 else if (process._eval) 
  // -e, --eval
  var rv = new module.Module()._compile('return eval(process._eval)', 'eval');
  console.log(rv);

所以在3.x eval'd 代码就像一个常规模块一样工作。

【讨论】:

将 Async/Await 与 node-postgres 一起使用

...理错误我使用的一个例子是一个非常简单的查询constPool=require(\'pg\');letconfig;if(process.e 查看详情

node与socket.io搭配小例子-转载

//服务端代码io=require(‘socket.io‘).listen(app),fs=require(‘fs‘),cookie=require(‘cookie‘);request=require(‘request‘);global.userlist={};app.listen(8080);//io.set(‘loglevel‘,1);//将socket.io中的debug信息关闭functi 查看详情

将 Node.js 的 Express 与 vhost 一起使用时出现意外错误

...14【问题描述】:这是错误:代码如下:runapps.js:varexpress=require(\'express\');varapp=express();varfirst=require(\'./apps/ghos 查看详情

Node.js require() 与 RequireJS?

】Node.jsrequire()与RequireJS?【英文标题】:Node.jsrequire()vsRequireJS?【发布时间】:2012-02-1510:55:08【问题描述】:你好RequireJS我可以设置一个像这样的基本路径:base:\'./app/\'例如,当我在./app/foo/bar/并且我有一个使用require(\'foo\');的脚... 查看详情

reactnative:running‘[node,-e,console.log(require(‘react-native/cli’).bin);]’commandfailed.(代码片段)

报错Cannotrunprogram"node":error=2,Nosuchfileordirectory查看日志发现:ReactNative:Running‘[node,-e,console.log(require(‘react-native/cli’).bin);]’commandfailed.我这边的最终解决办法1.关掉AndroidStudio2.执行open-a 查看详情

reactnative:running‘[node,-e,console.log(require(‘react-native/cli’).bin);]’commandfailed.(代码片段)

报错Cannotrunprogram"node":error=2,Nosuchfileordirectory查看日志发现:ReactNative:Running‘[node,-e,console.log(require(‘react-native/cli’).bin);]’commandfailed.我这边的最终解决办法1.关掉AndroidStudio2.执行open-a 查看详情

将 GULP 与 node.js 集成

...de.js代码运行gulp任务时,问题就出现了。Gulp代码:vargulp=require(\'gulp\');varrunSequen 查看详情

将 Node.js 与 MySQL 连接的问题

...的代码时,它给了我错误。如何解决此问题?app.jsvarmysql=require("mysql");varconnection=mysql.createConnection(host:\'localhost 查看详情

将 CouchDB 与 Node.js 库一起使用

...有没有办法在这些javascript函数中使用node.js库?比如使用require(\'http\')或者安装了npm的一些第三方模块。谢谢。【问题讨论】:【参考方案1】 查看详情

将 JQuery 与 node.js 一起使用时出错

...运行一个非常基本的JQuery脚本,名为a.js,其中仅包含:require("jquery");$().jquery;不幸的是,这不会与JQuery一起运行,给我一个TypeError:C:\\ 查看详情

如何将 Node.js 中的 net 模块与 browserify 一起使用?

...在客户端(在浏览器中)使用来自Node.js的net模块:varnet=require(\'net\');所以我查找了如何将Node.js模块发送到客户端,browserify似乎是答案。我用jQu 查看详情

Node.js:如何将全局变量传递到通过 require() 插入的文件中?

】Node.js:如何将全局变量传递到通过require()插入的文件中?【英文标题】:Node.js:howdoIpassglobalvariableintoafilebeinginsertedthroughrequire()?【发布时间】:2015-01-1609:59:37【问题描述】:我已将gulpfile.js拆分为/gulp文件夹中的多个文件,以... 查看详情

Mailchimp 和 Node.js 与 typescript noob 问题:Import vs Require

】Mailchimp和Node.js与typescriptnoob问题:ImportvsRequire【英文标题】:MailchimpandNode.jswithtypescriptnoobquestion:ImportvsRequire【发布时间】:2021-03-1615:31:01【问题描述】:我正在创建一个使用Mailchimp发送某些交易电子邮件的应用程序。他们在... 查看详情

node(代码片段)

...运行命令nodeindex.js)运行3.1 nodejs原生服务器consthttp=require("http")constserver=http.createServer((req,res)=>res.write("使用node启动一个服务器")res.e 查看详情

node之get与post(代码片段)

 Get获取内容:varhttp=require(‘http‘),util=require(‘util‘),//util提供常用函数集合url=require(‘url‘);http.createServer(function(req,res)res.writeHead(200,‘Content-Type‘:‘text/html;charset=utf-8‘);varparams= 查看详情

尝试将谷歌云功能与 node.js 一起使用时,admin.firestore 不是一个功能

...【问题描述】:这是node.jsindex.js文件的标题:constfunctions=require(\'firebase-funct 查看详情

将每个与 node-orm2 一起使用的通用回调

】将每个与node-orm2一起使用的通用回调【英文标题】:GeneralCallBackusingeachwithnode-orm2【发布时间】:2014-05-2314:19:38【问题描述】:我正在使用node-orm2和express。我正在尝试使用remove删除数据库中的多行。全部删除后,我想在确定一... 查看详情

node入门--module&require

...组中‘;}//modulemodule.exports=counter;//在app.js文件使用,要用require引用//仅仅引用时 查看详情