Search code examples
javascriptreactjswebpackconfigwebpack-4

Setting path to ./src in Webpack4?


Just getting started with building my own webpack config. I've looked around but cant seem to find it. How can I set the import path to start at root (src)?

- src
--components
---content
----Content.js
--constants
---example.js
---index.js

If I wanted to import a constant file to my component I'd have to take the ../../constant however I would like to just do ./constants

import { EXAMPLE } from "./constants"

Currently this is my resolve:

  resolve: {
    modules: [path.resolve(__dirname, './src'), 'node_modules'],
    extensions: ['.js'],
  },

But I am getting an error of cant resolve Module not found: Error: Can't resolve './constants'


Solution

  • I resolved this by using the alias:

      resolve: {
        alias: {
          src: path.resolve(__dirname, 'src')
        }
    
    import { EXAMPLE } from "src/constants"