Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read from cwd() in ES6 with import

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';

like image 815
suprita shankar Avatar asked Sep 19 '25 13:09

suprita shankar


1 Answers

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

  1. Use this awesome plugin https://github.com/tleunen/babel-plugin-module-alias (this is what I ended up doing)

  2. Rearrange the files

  3. If you must have dynamic variables then use require :)

Thanks!

like image 128
suprita shankar Avatar answered Sep 21 '25 02:09

suprita shankar