catamphetamine / universal-webpack

Isomorphic Webpack: both on client and server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wait for file triggering before file is finished writing

adailey14 opened this issue · comments

I reported this before at some point, but it's back and is happening more often now for my growing project. When I am compiling my project using 'npm run dev' I run into this error every few times I build:

[universal-webpack] Your server source file must export a function. Got '{}'

I find that if I add a 1 second timeout to 'wait for file.js' I get rid of the error:

//around line 47
if (condition_is_met)
{
  setTimeout(done, 1000);
  // instead of return done()
}

I believe what is happening is while the file is writing, the filesystem will report that it exists. But then when the file is loaded via 'require', the contents are not fully written, so it fails and returns an empty {}. By putting in the 1 second delay, the file is able to finish writing.

An alternative solution would be to make server.js retry requiring 'starter' (after a delay) when it doesn't return a function. Let me know if you need further info.

Just to make sure: are you having that error message with the latest version?
It used to be JSON.stringify( in the older version, but then became util.inspect(.

Also, you can add an extra debugging line in node_modules/universal-webpack/build/server.js:

        // Start webpage rendering server
        // (this module will be compiled by Webpack server-side build from './source/server.js')

        const starter = require(server_bundle_path)

console.log('@@@@@@@@@@@@', require('fs').readFileSync(server_bundle_path, 'utf8'))

And see what are the actual contents of the file.

Ok I will try this. I do have the latest version I was typing the error from memory so not quite right.

Ok here is what I got on my first try (looks like the file contents are a few blank lines?). I will try a few more times and see if I get a different result:

(waiting for Webpack build to finish)
("/Users/andrewdailey/Code/mortgagehipporeact/build/server/server.js" not found)
(waiting for Webpack build to finish)
("/Users/andrewdailey/Code/mortgagehipporeact/build/server/server.js" not found)
(waiting for Webpack build to finish)
("/Users/andrewdailey/Code/mortgagehipporeact/build/server/server.js" not found)
(waiting for Webpack build to finish)
@@@@@@@@@@@@ 


Error: [universal-webpack] Your server source file must export a function. Got {}
    at /Users/myname/Code/myproject/node_modules/universal-webpack/source/server.js:68:3

Hmm, really, two blank lines.
You can change it even further to:

console.log('@' + require('fs').readFileSync(server_bundle_path, 'utf8') + '@')

To know for sure

As for the solution, I could, for example, check for the file contents.
If it's blank, then try after a second (recursively).
I guess I'll go that route.

Tried with the second console.log options, and got no contents:

("/Users/andrewdailey/Code/mortgagehipporeact/build/server/server.js" not found)
(waiting for Webpack build to finish)
@@
Error: [universal-webpack] Your server source file must export a function. Got {}

Oh, that's better. So it's not blank, it's simply empty. That makes sense. Releasing the fix.

Try the latest version and see if it works for you (in time)

Ok great thanks!