Esm: Import relative to project root

Created on 6 May 2018  路  6Comments  路  Source: standard-things/esm

(If not already) it would be awesome to have the option to import relative to the project root. With require there are a few options like the rfr package or binding to require.main and with babel you have plugins like this available.

Anything along these lines to reduce the amount of ../../../ in imports would be appreciated!

question

Most helpful comment

All 6 comments

Hi @MattSturgeon!

There are lots of ways to handle custom path resolution. Baking each approach into esm doesn't seem like the right call. Instead, you can use rfr or babel with esm to load ESM modules if you so choose.

Apologies if I wasn't clear: I'm asking if it is possible to resolve paths relative to the project root using import syntax and without preprocessors like babel. rfr would be a different syntax, as it is built on top of commonjs.

Is there a way to customize path resolution within esm? I can't see anything obvious in your README.

Out of the box esm has CJS interop enabled so you should be able to use packages like rfr to load ESM files. When experimental module hooks are a bit more baked we'll add support for those, but they will likely not be as powerful as the CJS alternatives.

So if I want to use import thing from '/thing' esm syntax I am stuck with standard path resolution and the only option for root base resolution is commonjs syntax?

Setting NODE_PATH=. and cjs.paths: true is a good enough solution for now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pvdlg picture pvdlg  路  3Comments

StphamZ picture StphamZ  路  3Comments

OmgImAlexis picture OmgImAlexis  路  3Comments

mAAdhaTTah picture mAAdhaTTah  路  3Comments

MVSICA-FICTA picture MVSICA-FICTA  路  3Comments