2

Following simple code:

const { exec, spawn, fork, execFile } = require('child_process')


const startAllJobs = async () => {
  const process = fork('app/util/nlp/sentenceHelper.js');
};

sentenceHelper:

import { split } from 'sentence-splitter';
/*....more but unimportant since it breaks at first line*/

Babel setup:

starter.js

// Transpile all code following this line with babel and use 'env' (aka ES6) preset.
require("@babel/register")({
  extensions: ['.js', '.ts'],
  "presets": [["@babel/preset-env"
  ], "@babel/preset-typescript"],
  "plugins": ["@babel/plugin-transform-runtime"]
})
require("@babel/polyfill");

require('datejs');


// Import the rest of our application.
module.exports = require('./app/index.js');

Error:

import { split } from 'sentence-splitter';
       ^

SyntaxError: Unexpected token {

How can I use ES6 Syntax within child processes?

Node version: 12.0.0

Fabian Lurz
  • 103
  • 11
  • This problem isn't about `child-process`, but ES6 modules. Do you really need to use ES6 modules with node.js? – macabeus Jan 04 '20 at 01:16
  • You could use ES6 modules in Node 12: https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node – macabeus Jan 04 '20 at 01:17
  • But IMO is better to use commonjs in Node, because the ecosystem in Node already use it for a long time – macabeus Jan 04 '20 at 01:17

0 Answers0