Alan Bogu
Alan Bogu

Reputation: 785

How to use ES6 module in ember.js (with ember cli)?

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?

Upvotes: 2

Views: 813

Answers (1)

ykaragol
ykaragol

Reputation: 6221

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

See Description section from MDN.

Upvotes: 4

Related Questions