Skip to content

Modus-Logo-Long-BlackCreated with Sketch.

  • Services
  • Work
  • Blog
  • Resources

    OUR RESOURCES

    Innovation Podcast

    Explore transformative innovation with industry leaders.

    Guides & Playbooks

    Implement leading digital innovation with our strategic guides.

    Practical guide to building an effective AI strategy
  • Who we are

    Our story

    Learn about our values, vision, and commitment to client success.

    Open Source

    Discover how we contribute to and benefit from the global open source ecosystem.

    Careers

    Join our dynamic team and shape the future of digital transformation.

    How we built our unique culture
  • Let's talk
  • EN
  • FR

ES6 Import Statement Without Relative Paths Using Webpack

Published on April 14, 2016
Last Updated on December 3, 2022
Application Development, DevOps

The ES2015 module system is probably familiar to you by now. If you’ve used it, you might have faced the familiar relative path inclusion problem.

Using relative paths for imports makes your code base very hard to maintain, not to mention the hassle required to figure out where the inclusion is relative to the current path. If your code resembles the example below, today might be your lucky day!

import Header from '../../components/Header';
import Grid from '../../components/Grid';
import TransactionForm from '../TransactionForm';
import TransactionSummary from '../TransactionSummary';
import * as AppActions from '../../actions';

Import Path Resolver

With a very minor tweak to our Webpack configuration, we can get it to load files relative to the app root. Actually, we can configure the resolver to load from multiple root folders and our import declaration will get a major overhaul.

import Header from 'components/Header';
import Grid from 'components/Grid';
import TransactionForm from 'containers/TransactionForm';
import TransactionSummary from 'containers/TransactionSummary';
import * as AppActions from 'actions';

That looks much better, doesn’t it?

Webpack 1.x Path Resolver Configuration

Here’s the trick that makes the above possible:

resolve: {
  root: [
    path.resolve('./client')
  ]
},

Use this in your Webpack v1.x configuration. You might already have a ‘resolve’ setting in there. Keep everything there, but add a root key and specify all the folders that you would like to load your files from. In my case it’s the ./client/ folder.


NEW RESEARCH: LEARN HOW DECISION-MAKERS ARE PRIORITIZING DIGITAL INITIATIVES IN 2024.

Get Report


Adding too many folders may not turn out to be a good idea so don’t go wild with it either. Recursive search in too many paths may result in slower webpack builds, and it makes it harder to keep track of where the imports are coming from.

Webpack 2.x Path Resolver Configuration

In Webpack 2, resolvers from root (as used above), modulesDirectories, and fallback settings will merge into a single property – modules. Here is how we can do the same trick in Webpack 2:

resolve: {
  modules: [
    path.resolve('./client'),
    path.resolve('./node_modules')
  ]
},

You can specify a number of directories in modules, but make sure not to forget node_modules or npm package dependencies will fail to load.

Sample Project With Webpack Configuration

You can see the above setup in a sample project that uses Webpack + Redux + React. Feel free to use it in your own projects.

Please share this if you think your network might find this information helpful. Let me know how this worked for you in the comments below or on Twitter.

Posted in Application Development, DevOps
Share this

Grgur Grisogono

Grgur Grisogono is a software architect at Modus Create, specializing in JavaScript performance, React JS, and Sencha frameworks. He helped clients ranging from Fortune 100 to major governments and startups successfully release enterprise and consumer-facing web applications. He has also organized three tech conferences and co-authored Ext JS in Action SE. If Grgur's posts were helpful, maybe his 16 years of experience could help your project too.
Follow

Related Posts

  • Optimizing-React-ES6-Webpack-Production-Build
    Optimizing React + ES6 + Webpack Production Build

    Writing a web application in React using the ES6 awesomeness and spiced up with Webpack…

  • Webpack Tree Shaking
    Webpack 2 Tree Shaking Configuration

    Tree Shaking, a modern dead code elimination algorithm for ECMAScript 2015+ is one of the…

Want more insights to fuel your innovation efforts?

Sign up to receive our monthly newsletter and exclusive content about digital transformation and product development.

What we do

Our services
AI and data
Product development
Design and UX
IT modernization
Platform and MLOps
Developer experience
Security

Our partners
Atlassian
AWS
GitHub
Other partners

Who we are

Our story
Careers
Open source

Our work

Our case studies

Our resources

Blog
Innovation podcast
Guides & playbooks

Connect with us

Get monthly insights on AI adoption

© 2025 Modus Create, LLC

Privacy PolicySitemap
Scroll To Top
  • Services
  • Work
  • Blog
  • Resources
    • Innovation Podcast
    • Guides & Playbooks
  • Who we are
    • Our story
    • Careers
  • Let’s talk
  • EN
  • FR