javascript ReferenceError:角度未在 WebStorm 中定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27347978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:22:17  来源:igfitidea点击:

ReferenceError: angular is not defined in WebStorm

javascriptangularjsnode.jswebstorm

提问by EpicPandaForce

I'm completely new to AngularJS and I might have missed some crucial but not obvious step along the way of starting a new AngularJS project in WebStorm.

我对 AngularJS 完全陌生,在 WebStorm 中开始一个新的 AngularJS 项目的过程中,我可能错过了一些关键但不明显的步骤。

I installed Node.JS, installed Angular with npm, I even installed bower, I even installed angular in bower, but at this point I'm not sure what I am missing.

我安装了 Node.JS,用 npm 安装了 Angular,我什至安装了凉亭,我什至在凉亭中安装了 angular,但此时我不确定我错过了什么。

On Debug, I get the following message:

在调试时,我收到以下消息:

c:\Users\YourUser\WebstormProjects\angularjs-template\app\app.js:6
angular.module('myApp', [
^
ReferenceError: angular is not defined
    at Object.<anonymous> (c:\Users\YourUser\WebstormProjects\angularjs-template\app\app.js:6:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain [as _onTimeout] (module.js:497:10)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

On Run, I get the following message:

在运行时,我收到以下消息:

c:\Users\YourUser\WebstormProjects\angularjs-template\app\app.js:6
angular.module('myApp', [
^
ReferenceError: angular is not defined
    at Object.<anonymous> (c:\Users\YourUser\WebstormProjects\angularjs-template\app\app.js:6:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

App.js is the following

App.js 如下

'use strict';



// Declare app level module which depends on views, and components
angular.module('myApp', [
  'ngRoute',
  'myApp.view1',
  'myApp.view2',
  'myApp.version'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.otherwise({redirectTo: '/view1'});
}]);

And yes, the order of Angular in the HTML is the following:

是的,HTML 中 Angular 的顺序如下:

  <script src="bower_components/angular/angular.js"></script>
  <script src="bower_components/angular-route/angular-route.js"></script>
  <script src="app.js"></script>

It's funny because I'm literally just trying to run the basic template generated with WebStorm.

这很有趣,因为我实际上只是在尝试运行使用 WebStorm 生成的基本模板。

Run Configuration has

运行配置有

 Node Interpreter: C:\Program Files\nodejs\node.exe
 Working directory: C:\Users\YourUser\WebstormProjects\angularjs-template
 JavaScript file: app\app.js
 After launch: http://localhost:63342/angularjs-template/app/index.html

And nope! Angular is undefined.

不!角度未定义。

What on earth am I doing wrong?

我到底做错了什么?

EDIT: Exact output

编辑:精确输出

"C:\Program Files (x86)\JetBrains\WebStorm 9.0.1\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" app\app.js

c:\Users\Zhuinden\WebstormProjects\angularjs-template\app\app.js:6
angular.module('myApp', [
^
ReferenceError: angular is not defined
    at Object.<anonymous> (c:\Users\Zhuinden\WebstormProjects\angularjs-template\app\app.js:6:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

Process finished with exit code 8

回答by lena

Angular code can't be run with node.js. Steps to start with a new Angular project in WebStorm:

Angular 代码不能与 node.js 一起运行。在 WebStorm 中开始一个新的 Angular 项目的步骤:

  1. create a new Angular project using File/New project

  2. open built-in terminal, run 'npm install'

  3. right-click app/index.html, choose 'Debug' - your Angular application will be run on WebStorm built-in server

  1. 使用 File/New project 创建一个新的 Angular 项目

  2. 打开内置终端,运行“npm install”

  3. 右键单击 app/index.html,选择“调试” - 您的 Angular 应用程序将在 WebStorm 内置服务器上运行

回答by Mikalai

It looks like you are trying to run app.js on node.js. I think in project template should exist server's js file for that.

看起来您正在尝试在 node.js 上运行 app.js。我认为在项目模板中应该存在服务器的 js 文件。

回答by Franco Manzur

You have to run the index.html, not the app.js as usual. Algo in the root folder of the project you can run npm start and it will run up the server with the page in the port:8000

您必须像往常一样运行 index.html,而不是 app.js。Algo 在项目的根文件夹中,您可以运行 npm start,它将运行服务器,端口为:8000