catamphetamine / universal-webpack

Isomorphic Webpack: both on client and server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

__dirname is / in server modules

nickm-scribestar opened this issue · comments

Hey, thanks for this module, trying to get css modules and webpack working with some server side rendered react components at the moment.

I set it up just like the instructions in the readme and now my express code can't find the jade views as __dirname is now '/'

my express code looks something like:

app.set('views', __dirname + 'views')

// ... other stuff

res.status(200).render('index.ejs', {reactOutput, initialState})

which of course now fails because my views directory can't be found.

What have I done wrong? :)

This issue has been encountered before
#16
Webpack sets __dirname to /

webpack/webpack#1599 (comment)
They say that this works:

node: {
  __dirname: false,
  __filename: false,
}

So I suggest you do the following:

in your server configuration do this:

import { server_configuration } from 'universal-webpack'
import settings from './universal-webpack-settings'
import configuration from './webpack.config'

const config = server_configuration(configuration, settings)

config.node = {
  __dirname: false,
  __filename: false
}

export default config

And post back on results

yup confirmed that worked great - thanks loads!

I'm including this in the library then.
Install the latest version and tell me if it works without the hack:
npm install universal-webpack@latest --save

Yup, removed my hack and can confirm v0.1.40 fixes it. Thanks again.

Reading the referenced issue I can't help thinking actually your recommended solution in this comment might be the better way to go anyway though in the end?

Actually, I don't think I understood what he was saying in that issue report.
And I don't remember why I answered him that way.

okay yes thinking about it the same server module that is loading in my jade view would also need to do the react server rendering so a separation wouldn't be possible.