Refat Rafi Refat Rafi - 1 year ago 56
React JSX Question

Getting working directory and file name in react & webpack

I am working with a react js project. i am using webpack and redux with that. Here is the folder structure of my project.


For now i want to use dynamic className for the index.jsx files according to their working directory name. Example:

for index.jsx in the folder component, the className will be


for index.jsx in the folder container, the className will be


I have tried to use npm module path for that. But the __dirname gives the output of the url of the browser '/'. How can i get that pwd from the jsx file.

Answer Source

By default, webpack mocks Node's __dirname to "/". You can enable the real dirname by adding the following to your webpack configuration:

node: {
    __dirname: true

After that, __dirname will be set, relative to the resolve context in your webpack configuration.