Hello,
I have several server-side components of my application that retrieve very large, gzip’d files from the Internet on a periodic basis, uncompress, parse with JSON.parse
; organize, normalize, and enrich the data; then insert into a Mongo db. The issue I’m having is that I frequently exceed the JavaScript maximum heap space, even with TOOL_NODE_FLAGS=--max-old-space-size=12288
.
Therefore, I’ve decided to move each of these components to child processes.
const { fork } = require('child_process');
const path = require('path');
// Resolve proper application path based on dev or prod
let root = path.resolve('../../../..');
if (path.basename(root) === '.meteor') { // development
root = path.resolve(`${root}/..`);
}
// Import data
const child = fork(`${root}/imports/path/to/script.js`);
const handleMessages = message => console.log(message);
child.on('message', handleMessages);
This works fine. The script loads and executes as a child process. However, none of my meteor packages can be resolved. If I use import { Meteor } from 'meteor/meteor';
in script.js
I receive the following error:
/mnt/code/project/imports/path/to/script.js:1
(function (exports, require, module, __filename, __dirname) { import { Meteor } from 'meteor/meteor';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
If I change that line to const { Meteor } = require('meteor/meteor');
instead, I receive this error:
module.js:549
throw err;
^
Error: Cannot find module 'meteor/meteor'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/mnt/code/project/imports/path/to/script.js:1:82)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
I’ve tried wrapping the code with Meteor.bindEnvironment
like so, but that results in no errors and no code executing at all:
// Import data
Meteor.bindEnvironment(() => {
const handleMessages = message => console.log(message);
const child = fork(`${root}/imports/path/to/script.js`);
child.on('message', handleMessages);
});
Any ideas on how to resolve Meteor packages from within a child process, or any alternative method for solving my problem? Thanks!