JavaScript, Tools, Webpack

Webpack: How to Import from Root

It can be easily set up with a module resolver:

1. Babel for Webpack

$ npm i babel-plugin-module-resolver --save-dev

Then create a .babelrc file under root:

{
    "plugins": [
        [
            "module-resolver",
            {
                "root": ["./"]
            }
        ]
    ]
}

2. EsLint for VSCode

For VSCode to acknowledge the import and run go-to-definition correctly, add this in your jsconfig.json, or create this file under project root if you don’t already have one:

{
    "compilerOptions": {
        "baseUrl": "."
    }
}
Standard

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.