I am creating an addon project.
I have a file in app/utils
with the following import
import config from '../config/environment';
I want to move this to addon/utils
but the above import does not work anymore and I get an error when doing ember serve
. I need access to the consuming applications environment.
How do I do this from the addon folder?
You simply can't. The addon namespace is meant to be isolated from the app namespace and it is, that's why it won't let you import the environment config. If you want to access it it needs to be in the app namespace.
One way around this, is to keep it in the addon namespace, and require users to import it in their own app/utils and pass in the config themselves. It would look something like this:
// app/utils/their-util.js
import config from '../config/environment';
import yourUtil from 'the-addon/utils/your-util';
export default function() {
return yourUtil(config);
}
You could even do this in your addon's app namespace to have it included by default, and they could then override it if they wanted to do something custom.
Obviously this isn't an ideal solution but it's what's available today.