Javascript 如何在node.js中运行jQuery

Javascript 如何在node.js中运行jQuery,javascript,jquery,json,node.js,Javascript,Jquery,Json,Node.js,我尝试了npm安装jQuery——保存并保存在我的节点文件var$=require('jQuery')但是,当我使用 $.getJSON('https://en.wikipedia.org/wiki/Washington,_D.C.', function(data) { //data is the JSON string }); 我得到了错误 TypeError: $.getJSON is not a function at Object.<anonymous> (

我尝试了
npm安装jQuery——保存
并保存在我的节点文件
var$=require('jQuery')但是,当我使用

$.getJSON('https://en.wikipedia.org/wiki/Washington,_D.C.', function(data) {
    //data is the JSON string
});
我得到了错误

TypeError: $.getJSON is not a function
    at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:16:3)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
刚刚回来

TypeError: require(...).env is not a function
    at Object.<anonymous> (C:\Users\Karim\node 2\tweetPic.js:3:18)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
TypeError:require(…)。env不是函数
反对。(C:\Users\Karim\node 2\tweetPic.js:3:18)
在模块处编译(Module.js:643:30)
在Object.Module._extensions..js(Module.js:654:10)
在Module.load(Module.js:556:32)
在tryModuleLoad时(module.js:499:12)
在Function.Module.\u加载(Module.js:491:3)
位于Function.Module.runMain(Module.js:684:10)
启动时(bootstrap_node.js:187:16)
在bootstrap_node.js:608:3
我以类似的方式安装了jsdom包。我的jquery代码本身有问题吗?我怎样才能解决这个问题


编辑:看来jQuery并不是我真正需要的。我将研究另一种检索json数据的方法

对不起,我不知道如何安装jquery。但是你显然需要它来请求一个网站并获取json。你可以用它。希望我能帮助你

你可以这样做:

request('https://en.wikipedia.org/wiki/Washington,_D.C.', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});

您是否正在尝试在Node.js上安装jQuery以便可以发出简单的HTTP请求?Nicholas我正在尝试从特定url获取json数据。我不确定这是否转化为一个简单的HTTP请求。这是一个简单的HTTP请求。省去你的麻烦,按照下面@JeremyM的答案来做。jQuery主要是一个前端框架,它只包含用于发送HTTP请求的助手函数。将其安装到服务器上只是为了做一些可以在服务器上运行的其他专门的、轻量级模块轻松完成的事情,这是一种过分的做法,比如
request
。Karim,如果您仍然在寻找解决方案,这是:(2018年更新)
var jsdom=require(“jsdom”)
const{JSDOM}=JSDOM
vconst{window}=newjsdom()
const{document}=(新JSDOM(“”))窗口
global.document=document
var$=jQuery=require('jQuery')(窗口)request('https://en.wikipedia.org/wiki/Washington,_D.C.', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});