In ES5 it is like this
UserInfoModel = require(process.cwd() + '/server/models/Users');
How do I write the same in ES6?This does not work
import { UserModel } from '/server/models/User';
I do not want to do this. Is there a better way? [The below works btw]
import { UserModel } from '../../../server/models/User';
After discussing with fellow mentors on other channels. The answer is - it is not possible. One of the optimizations ES6 made over ES5 is that imports had to be strictly statically analyzable. So it cannot depend on any variables.
Options to avoid ugly code
Use this awesome plugin https://github.com/tleunen/babel-plugin-module-alias (this is what I ended up doing)
Rearrange the files
If you must have dynamic variables then use require
:)
Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With