Jump to content
McKay Development

Webpack can't find module './clientmetrics.json' - dynamically requiring file (w/ possible fix)


Epic

Recommended Posts

Hey,

I am currently working on an electron app which is relying on your 'steam-user' module, and am running into an issue with webpack when requiring the module.

Error: Cannot  find module './clientmetrics.json'

The file in question is located under 'node_modules/steam-user/protobufs/generated' and get's required by the '_load.js' file (auto-generated by '../scripts/generate-protos.js') in the same directory. The issue with webpack is it is running in build-time and thus cannot handle dynamic variables - since the 'load' function ('_load.js') gets executed with the filename as parameter webpack cannot require the JSON files.

I fixed it by editing the '_load.js' file and passing the required files as a paramter to the 'load' function:

mergeObjects(Schema, load(require('./clientmetrics.json')));
//... same for all other files

function load(filename){
  return require('protobufjs').Root.fromJSON(filename);
}

I guess you would also need to edit the 'generate-protos.js' file under '../scripts':

FS.readdirSync(__dirname + '/../protobufs').forEach((filename) => {
	if (!filename.match(/\.proto$/)) {
		return;
	}

	let filenameWithoutExtension = filename.replace('.proto', '');
	let cmdLine = PBJS_COMMAND_LINE.replace(/%s/g, filenameWithoutExtension);
	console.log(cmdLine);

	ChildProcess.execSync(cmdLine);
	loader += `mergeObjects(Schema, load(require('./${filenameWithoutExtension}.json')));\n`;	// FIX HERE

	let protoDefinition = require(Path.join(GENERATED_DIR, `${filenameWithoutExtension}.json`));
	if (protoDefinition.nested) {
		FS.writeSync(typesFile, `///////////////////////////////////////////////\n// ${filenameWithoutExtension}.proto\n///////////////////////////////////////////////\n\n`);
		FS.writeSync(typesFile, writeTypedef(protoDefinition.nested));
	}
});

//load function:
function load(filename) {
	return require('protobufjs').Root.fromJSON(filename);	// FIX HERE
}

Don't know if there would be anything else to change - just skipped through the file.

Do you think the fix is appropriate and could be implemented in your module? Or do you have another idea?

 

Thanks in advance!

 

 

 

Edited by Epic
Link to comment
Share on other sites

  • Epic changed the title to Webpack can't find module './clientmetrics.json' - dynamically requiring file (w/ possible fix)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...