You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
child_process.fork a ts file with import statement and args && execArgv !== undefined #619
import * as test from 'fs';
console.log('File loaded')
Note: You need ts-node, typescript, and @types/node
I was using node version 10.4.1
It will work if you:
Remove test.ts's import statement,
Change either empty array to undefined in index.ts,
Or use vanilla js/compiled typescript
PS C:\Users\ShayB\Desktop> node -r ts-node/register index.ts
C:\Users\ShayB\Desktop\test.ts:1
(function (exports, require, module, __filename, __dirname) { import * as test from 'fs';
^
SyntaxError: Unexpected token *
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:670:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at startup (internal/bootstrap/node.js:238:19)
PS C:\Users\ShayB\Desktop> node -r ts-node/register index.ts
Note: the error is not the * it will be at the same spot no matter what you import.
I actually ran into this in a real project too, incredible.
changed the title [-]child_process.fork a ts file with import statement and arg !== undefined[/-][+]child_process.fork a ts file with import statement and args && execArgv !== undefined[/+]on Jun 27, 2018
I think the real issue here is child_process.fork() forks a new Node.js instance, but it is unaware of ts-node.
I have the same problem and the title containing things like args and execArgv is really leading in the wrong direction.
I'm also running my code via ts-node. In it I need to create a child proc with fork().
If I run code like child_process.fork('ChildProcess.ts') and ChildProcess.ts contains some typescript only constructs (eg: import {}, export, ...), then the interpreter being node, not ts-node, will fail.
It may be recommended to use something like child_process.exec('node ./node_modules/.bin/ts-node my-typescript-module.ts), but I really want/need the IPC communication channel that gets set up between the parent and child processes when fork() specifically is used.
Any ideas on how to achieve this?
Reacted by Vitaliy Zaytsev, ollie, Lennart Hase, Rui Campos, Herman Butch Berlemann , Dan Falk, Zhiming Guo, Alex Leung, blackening, Hou Quan and 6 more
Reacted by michael, Tomas Petras Rupšys, Bartosz Brzeziński, Serż Kwiatkowski, Jason H, CS-BTurner, redeyes420420, ⛥ 𝕲𝖍𝖔𝖘𝖙𝖌𝖑𝖎𝖙𝖈𝖍 ☯ ∞Hz, Patrick Staarink, Ricardo Seromenho and 2 more
Reacted by George Thomas, Serż Kwiatkowski, Hellon Canella, ⛥ 𝕲𝖍𝖔𝖘𝖙𝖌𝖑𝖎𝖙𝖈𝖍 ☯ ∞Hz, Patrick Staarink and Mo Yaseen
I know this is old, but hopefully it helps someone.
@RichAyotte's response worked for me. In my case I needed to specify the relative path to the file: const childProcess = fork("./src/controllers/child-process.ts", [], { execArgv: ["-r", "ts-node/register"] });
Reacted by Perry Ogwuche, ⛥ 𝕲𝖍𝖔𝖘𝖙𝖌𝖑𝖎𝖙𝖈𝖍 ☯ ∞Hz, Patrick Staarink and SiberianMark
Activity
[-]child_process.fork a ts file with import statement and arg !== undefined[/-][+]child_process.fork a ts file with import statement and args && execArgv !== undefined[/+]blakeembrey commentedon Jun 29, 2018
You're overriding
execArgvwhich means this isn't even running through TypeScript.Please read the documentation for functions you're trying to use: https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options.
jrhite commentedon Sep 29, 2018
I think the real issue here is
child_process.fork()forks a new Node.js instance, but it is unaware ofts-node.I have the same problem and the title containing things like
argsandexecArgvis really leading in the wrong direction.I'm also running my code via
ts-node. In it I need to create a child proc withfork().If I run code like
child_process.fork('ChildProcess.ts')andChildProcess.tscontains some typescript only constructs (eg:import {},export, ...), then the interpreter beingnode, notts-node, will fail.It may be recommended to use something like
child_process.exec('node ./node_modules/.bin/ts-node my-typescript-module.ts), but I really want/need the IPC communication channel that gets set up between the parent and child processes whenfork()specifically is used.Any ideas on how to achieve this?
RobinBuschmann commentedon Jan 11, 2019
@jrhite This should work:
RichAyotte commentedon Jul 15, 2019
This is what worked for me.
Perry5 commentedon Sep 15, 2019
I know this is old, but hopefully it helps someone.
@RichAyotte's response worked for me. In my case I needed to specify the relative path to the file:
const childProcess = fork("./src/controllers/child-process.ts", [], { execArgv: ["-r", "ts-node/register"] });blakeembrey commentedon Sep 15, 2019
Heads up: This should never have been an issue. If you are encountering an issue please let me know by creating a new issue.
I did additional work in January to ensure args from the
ts-nodeexecutable also forward correctly to children: #760.Edit: The original issue is still valid though, if you override
execArgvit won't be able to use the default behavior of re-usingts-node.carlosalaniz commentedon Jan 6, 2021
You can also omit the extension, once compiled the resulting JavaScript will be used. Ex.
ParentProcess.ts
ChlidProcess.ts
Alexzanderk commentedon Apr 25, 2021
How can I use
ts-node-devinstead ofts-node?