2
votes

Standard project set up with ember cli seems to be using ES6 modules when I generate controllers/routes/models etc. with the cli. Sometimes though I want to import/export an additional function/module ie. I may want to write a function that I use in the controller in a separate file. When I try to import the function in the standard ES6 way ember-cli seems to have a problem with handling it. Let's say I've created controller with:

ember g route tesit

then I create a function in app/routes/testit/logger.js

const logger = function(msg) {
  console.log(msg);
};

export default logger;

and import it in my controller app/routes/testit.js:

import Ember from 'ember';
import logger from './testit/logger.js'

export default Ember.Route.extend({
  beforeModel() {
    logger('it works');
  }
});

then I get the following error:

Error: Could not find module myproject/routes/testit/logger.js imported from myproject/routes/testit

How can I resolve it?

1
remove .js from import logger from './testit/logger.js' - ykaragol
thanks ykaragol, interesting - babel would work with the extension as well as without it. That works though - awesome! :-) - Alan Bogu
thanks Alan Bogu. I copied it as answer and added a reference. - ykaragol
Also note that this is what Ember would consider a util and might be better put in the utils folder. At some point once the new Ember resolver is in place that may be a requirement... - acorncom

1 Answers

4
votes

Remove .js extension from import logger from './testit/logger.js'; line.

See Description section from MDN.