Ember-CLI: Fix for “'Ember' is not defined

2020-06-28 01:32发布

问题:

When using Ember-CLI and running ember server, I get the following error from JSLint:

[app_path]/filename.js: line 1, col 16, 'Ember' is not defined.

Adding import Ember from 'ember'; fixes this.

Is this the official way to go now on all my files? The documentation does not mention this change yet.

回答1:

EDIT

From Stephan Penner:

We explicitly left it [Ember] out [of the .jshintrc file], please import ember instead.

We plan on exposing more and more of ember as es6, someday this will allow the tooling to remove parts of ember you are not using. Resulting in smaller builds.

Still, until that date it's probably going to save you a lot of hassle to put it in .jshintrc.

OUTDATED ANSWER

In your .jshintrc file (ortests/.jshintrc), add anything in the global namespace you don't want to have to define in each module to the predef object. For example:

{
  "predef": {
    "document": true,
    "window": true,
    "SprintStatusENV": true,
    "Ember": true, // Added
    "$": true, // ADDED
    "Modernizr": true // ADDED
  },
  "browser" : true,
  "boss" : true,
  "curly": true,
  "debug": false,
  "devel": true,
  "eqeqeq": true,
  "evil": true,
  "forin": false,
  "immed": false,
  "laxbreak": false,
  "newcap": true,
  "noarg": true,
  "noempty": false,
  "nonew": false,
  "nomen": false,
  "onevar": false,
  "plusplus": false,
  "regexp": false,
  "undef": true,
  "sub": true,
  "strict": false,
  "white": false,
  "eqnull": true,
  "esnext": true,
  "unused": true
}

In this example, I define Ember (could also define Em), jQuery using '$' and Modernizr. This will stop the jshint error messages appearing in the terminal.

This is per the ember-cli docs:

"If you want to use external libraries that write to a global namespace (e.g. moment.js), you need to add those to the predef section of your project’s .jshintrc file and set its value to true. If you use the lib in tests, need to add it to your tests/.jshintrc file, too."



回答2:

Expressly importing the Ember modules seems to be the official way now. The Ember documentation under Using Modules & the Resolver now says that you must expressly import Ember when you want to use Ember and import DS for Ember Data.

I've been searching for the reason and haven't found anything yet, but I'm presuming it's to make dependencies explicit and to make it possible to create plain old JavaScript object files.