Reputation: 6749
I have a file that creates child processes, the code is similar to this:
var cp = require('child_process');
function process () {
return cp.fork('./worker');
}
module.exports(process);
But ./worker
doesn't seem to be included in the bundle. Which causes the following error when running the process
function:
Error: Cannot find module 'C:\Users\USER\Documents\GitHub\PROJECT\worker'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
module.js:487
throw err;
^
How can I make sure worker files are bundled as well? I want my whole project in óne file.
Upvotes: 4
Views: 886
Reputation: 6668
There is a possible workaround. You can copy all your worker files into the dist directory. Let us say you have all your worker files in a workers
directory. And so your above code changes a little (includes worker.js from workers direcotory).
var cp = require('child_process');
function process () {
return cp.fork('./workers/worker');
}
module.exports(process);
And to copy the workers folder to your build/dist folder, you can use copy-webpack-plugin. In your case the plugin can be used like this in the webpack config file.
var CopyWebpackPlugin = require('copy-webpack-plugin')
module.exports = {
// your configuration for entry and output will be different, of course entry: './main.js', output: { filename: 'dist/bundle.js' }, target: 'node', plugins: [ new CopyWebpackPlugin([ { from: 'workers', to: 'dist/workers' } ]) ] }
Your bundle will still not have the worker.js file, but the reference will work for fork
method.
Upvotes: 4