javascriptember.jsember-clijs-amd

Import 3rd party module into Ember


I am trying to import this module into an Ember.js (version 2.5) app:

https://github.com/hapijs/nes/blob/master/lib/client.js

This is how I'm doing it (following the docs):

app.import('bower_components/nes/lib/client.js', { exports: { 'nes': ['default', 'client'] } });

And the import declaration:

import client from 'nes';

Now I keep getting the error below:

loader.js:201 Uncaught Error: Could not find module nes imported from myapp.com/components/chart-widget

I assume the import statement is right and the actual module doesn't export itself in a way Ember can consume it. However, how can I fix it? Is there a way to write a wrapper around 3rd party libraries such as this one?


Solution

  • TL:DR your module is accessible as a global variable with nes.Client.

    You are looking at the wrong section in the guide. hapijs/nes is not a standard AMD asset and thus cannot be directly imported using the import keyword. This means that your library is to be found in the global context.

    Debugging through bower_components/nes/lib/client.js will show you that the code is branching in this case : https://github.com/hapijs/nes/blob/master/lib/client.js#L24

    You can simply access your third party module via a global variable named nes.Client.

    If you'd like to access it with the ES6 import keyword for more clarity, you can generate a vendor-shim as describe in the guide : https://ember-cli.com/user-guide/#managing-dependencies.

    Note that this will not remove nes from the global context and will just allow you to import it with the import keyword.