1

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

-assets
-src  
 -component   
  -index.jsx  
 -container   
  -index.jsx

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

src-component

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

src-component

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It is not working on windows. do i need to change something on that node : { __dirname: true }
You shouldn't have to. I suspect webpack's own implementation of __dirname isn't capable of working with Windows paths. You might want to open up an issue in the webpack Github repository.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.