Importing node module in ember-cli-build causing error in finding module `ember-resolver`

#1

I’m trying to import an external dependency in my project by doing npm install --save-dev --registry=http://someDependency, and then use app.import('node_modules/someDependency') in ember-cli-build.js

But this is causing the following error:

Could not find module `ember-resolver` imported from `myapp/resolver`

All I have in myapp/resolver.js is:

import Resolver from 'ember-resolver';

export default Resolver;

Seems that it’s looking for ember-resolver in myapp/resolver instead of node_module? But when I delete app.import('node_modules/someDependency') in ember-cli-build.js things are working fine.

Could someone give me some hint about what’s going on here? Thanks a lot!


ember version: 2.18.2

node version: 9.0.0

pm version: 5.5.1

#2

I’ve seen the resolver show error messages pointing to unrelated places in the code when a dependency or file isn’t able to import correctly. My hunch is that the new dependency you’re importing has a problem and that problem is causing things to fail downstream. This resolver failure seems like a red herring. Try loading your app with the debugger pausing on caught and Uncaught exceptions and see if anything changes with/without the new import.

2 Likes
#3

Thanks a lot for the answer! The problem is indeed with importing the new dependency :smiley:

#4

Yes, the explanation here is that most things you author in an Ember app are Javascript modules, which compile to a list of AMD define statements for the browser:

define('your-app/components/foo', [exports, someDep], function(exports, someDep) {
  exports.default = Ember.Component.extend(...);
});
define('something-else', function() {
  ...
});

Exceptions inside those modules end up inside the functions, and so they don’t effect the initial evaluation of the whole Javascript bundle.

But code that you app.import() is in script context, not module context, which for our purposes means it doesn’t get automatically wrapped in a define(function(){}). It will run inline with those define statements.

define('some-stuff', function() {
  ...
});
yourAppImportedCodeMightRunHere();
define('something-else', function() {
  ...
});

So if that code throws an exception, it will stop the later defines from happening. Then if the resolver tries to locate one of those things, it will be missing.

3 Likes