Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 imports using module names without path

I would like to be able to import my modules using just name without path.

For example, say I want to use:

import ViewportChecker from 'viewport-checker';

instead of

import ViewportChecker from '../ViewportChecker';

But I want to keep the file where it is and not create a new npm module of it. Would it be possible to define it as module for exmaple in package.json?

like image 353
jeff Avatar asked Feb 12 '26 12:02

jeff


2 Answers

You can use webpack's alias feature : https://webpack.js.org/configuration/resolve/#resolve-alias

like image 90
Cyrille Avatar answered Feb 14 '26 01:02

Cyrille


You can put a default file with naming convention of index.js which contains all the exports.

like:

-App
--home
---index.js // <--it will hold all the exports from otherClass.js and anotherClass.js
---otherClass.js
---anotherClass.js

Now you have to import just folder name and it will automatically search for index.js file to get the particular exported item.

It might look like this:

import Home from './home';
like image 42
Jai Avatar answered Feb 14 '26 02:02

Jai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!