How to call Node index.js in my code?

I have a puppeteer project and in order to run the script i have to run index.js in the terminal. But how do i make it to run in my javascript code automatically?

If you mean execute a node script from a node script, then like:

import {fork} from "child_process";


But this feels like it’s maybe not quite what you want (I’m assuming index.js is the entry point of the app you’re testing with Puppeteer?)? Might be exactly what you need, might not, would need to know context here.

1 Like

well i only have the puppeteer file now(index.js) :stuck_out_tongue:P
but the idea is that i want the puppeteer file (index.js) to run whenever i press a button on the html. How can i do that? :slight_smile:

basically what i want is to Execute Node command on button click

Ah, so other way round: yeah fork a child process, that can run the puppeteer script.

fork is used to create an entirely new V8 process, so I think that’s what you want here (execute puppeteer in a new instance of node from your node app). exec might be enough, you might not need a new instance but I’m not sure. You’ll need to play around with this, and what you’ll also need to do is handle stdio, you need the main process to pick up exit/error codes.

1 Like

i feel like what you’re saying is that i need to learn node :stuck_out_tongue:
i havent done anything with the back end yet, but i guess i’ll try to figure it out somehow