Using a package from npm - error: "spawn is not a function"

I’ve just updated an older project of mine to Meteor 1.3 to try the new npm support.

To test this I’ve installed the wkhtmltopdf package like so:
npm install wkhtmltopdf --save

Using a simeple example from their docs I’ve created this event:

import wkhtmltopdf from 'wkhtmltopdf';

Template.MyTemplate.events({
	'click .mybutton': function() {
		wkhtmltopdf('<h1>Test</h1><p>Hello world</p>').pipe(res);
	}
});

When I click this button I’m getting the following error:
Uncaught TypeError: spawn is not a function --> modules.js?hash=fe8c32a…:208

I’m not sure whats happening here.
Very grateful for any help :slight_smile:

Did you npm install --save meteor-node-stubs ?

Yes, unfortunately I have already tried that.

Not sure what else to try.
Create a fresh meteor project and move everything over?

1 Like

wkhtmltopdf I’m fairly certain is a server-side package. That thing tries to spawn a new process that will run a headless webkit in order to parse your html and generate a PDF from it. You may not have been paying attention while attempting the above, because if you think about it it becomes quite clear that maybe that’s not an npm package that has any chance of running client-side :wink:

EDIT: And to explain the error message: ‘spawn’ is a method found in node’s child_process module. That’s not available on the client.

2 Likes

Looking at the source for wkhtmltopdf, it doesn’t seem to be a browser-compatible package - it uses child_process which is server-only