首页 > 解决方案 > Heroku 给出 ReferenceError 并且在节点应用程序上无法识别“$”

问题描述

我正在尝试在 Heroku 上部署一个简单的应用程序。它在本地完美运行。当我尝试在 Heroku 上提供它时,我收到以下错误:

ReferenceError: $ is not defined

为什么我被告知这$是一个问题,即使我在本地运行它时它工作得很好?当我运行 heroku local web 时出现该错误。当我尝试在 heroku 上部署应用程序时,日志告诉我尝试 npm start 时它失败了。虽然这是一个问题,但我认为如果我不使用 heroku 本地网络在本地运行它,它将无法工作。代码如下:

src/js/app.js

App = {
  ...
};

$(function() { // THIS IS THE FAILING LINE.
  $(window).load(function() {
    // App.listen(process.env.PORT || 8000);
    App.init();
  });
});

src/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <link rel="shortcut icon" type="image/png" href="static/favicon.ico"/>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/box.css" rel="stylesheet", type="text/css">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    </head>
    <body>
      <div class="container" style="width: 850px;">
           ...
      </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script src="jquery.min.js"></script>
    <script src="js/app.js"></script>
    <!-- <script -->
  </body>
</html>

包.json

{
  "name": "theName",
  "version": "1.0.0",
  "description": "",
  "main": "truffle.js",
  "directories": {},
  "scripts": {
    "dev": "lite-server"
  },
  "author": "Me",
  "license": "MIT",
  "devDependencies": {
    "lite-server": "^2.3.0"
  }
}

档案

web:node src/js/app.js

标签: javascriptnode.jswebherokudeployment

解决方案


推荐阅读