【问题标题】:Error: Cannot find module 'jquery' on node.js on Heroku错误:在 Heroku 上的 node.js 上找不到模块“jquery”
【发布时间】:2017-10-16 16:59:05
【问题描述】:

我正在尝试将 jquery 和 jsdom 包含到托管在 Heroku 上的 node.js 服务器中,以便我可以使用 jQuery 从控制台(而不是网页)进行 ajax 调用,例如 $.ajax({...}) .

在 Heroku 上部署 Node 服务器时,我不断收到以下错误:

Error: Cannot find module 'jquery'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/app/server.js:19:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
Process exited with status 1

这是我的 package.json 的样子:

{
  "name": "node-api",
  "main": "server.js",
  "dependencies": {
    "body-parser": "1.17.1",
    "express": "4.15.2",
    "jquery": "3.2.1",
    "jsdom": "10.1.0",
    "mongoose": "4.9.9"
  },
  "engines": {
    "node": "6.10.3"
  }
}

这是我在 server.js 中包含 jquery 的方式:

global.jquery = require('jquery');
window.$ = window.jquery = jquery;

任何帮助将不胜感激!

谢谢,

JS

【问题讨论】:

    标签: javascript jquery node.js ajax heroku


    【解决方案1】:

    我最终使用了najax,它将自己定义为“节点中的 jQuery ajax-stye http 请求”。

    它可以在我的本地机器和 heroku 上运行。

    我希望它可以帮助其他一些人。

    【讨论】:

      【解决方案2】:

      您可以使用node-fetch 或类似的包。他们自述文件中的示例代码:

      // json 
      
      fetch('https://api.github.com/users/github')
          .then(function(res) {
              return res.json();
          }).then(function(json) {
              console.log(json);
          });
      
      // catching network error 
      // 3xx-5xx responses are NOT network errors, and should be handled in then() 
      // you only need one catch() at the end of your promise chain 
      

      【讨论】:

      • 我尝试了 fetch,但在处理错误时我一直在运行一些问题(例如 nhandledPromiseRejectionWarning: Unhandled Promise RejectionWarning: Unhandled Promise Rejection)。所以我尝试了一些其他方法来让 jQuery 工作,最后我找到了一个名为 najax 的“jQuery ajax-stye http requests in node”。我现在正在尝试它,它似乎工作得很好!我希望这可以帮助其他一些人
      【解决方案3】:

      heroku 似乎没有安装新软件包

      您可以尝试将 npm 缓存的 heroku 标志设置为 false 以强制重新获取包并再次部署

      heroku config:set NODEMODULESCACHE=false
      git commit -am 'build' --allow-empty 
      git push heroku master
      heroku config:unset NODEMODULESCACHE
      

      【讨论】:

      • 不,它在我的本地机器上也不起作用。我最终找到了一个名为 najax 的“jQuery ajax-stye http requests in node”。我现在正在尝试它,它似乎工作得很好!我希望这可以帮助其他一些人
      猜你喜欢
      • 2012-04-22
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2020-11-08
      • 2020-01-17
      • 2021-07-13
      • 2019-12-12
      相关资源
      最近更新 更多