Deidara Deidara - 1 month ago 7
Javascript Question

run terminal commands in node js

I'm trying to write a web crawler program using quickscrape, after I type

quickscrape \
--url https://peerj.com/articles/384 \
--scraper journal-scrapers/scrapers/peerj.json \
--output peerj-384
--outformat bibjson


and hit the "Enter"
in my terminal, the library will generate the result from the crawler and put them into a folder.

Now I want to write a javascript progarm so that is will call these terminal commands for me, but I am not sure how to interact with terminal in node.js.

Answer

I think child_process is that you need. https://nodejs.org/api/child_process.html

let exec = require('child_process').exec;
exec('quickscrape --url https://peerj.com/articles/384 --scraper journal-scrapers/scrapers/peerj.json --output peerj-384 --outformat bibjson', (error, stdout, stderr) => {
    //...
})