Nodejs error when executing from Laravel using Symfony Process

935 Views Asked by At

I am trying to execute a Nodejs file from a Laravel controller using Symfony Process:

$process = new Process('node node/test.js');
$process->run();

if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();

The node/test.js is located in the Laravel public directory and looks like this:

const scrapedin = require('scrapedin')
  const fs = require('fs')

  const cookies = fs.readFileSync('cookies.json')
  const options = {
    cookies: JSON.parse(cookies)
  }

  scrapedin(options)
.then((profileScraper) => profileScraper('https://www.linkedin.com/in/profile/'))
.then((profile) => console.log(profile))

When running this from Laravel I get the following error:

internal/modules/cjs/loader.js:638 throw err; ^ Error: Cannot find module 'scrapedin' at > > Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)

If I run the following command from the terminal the script will work fine:

node test.js

If I run the following command from the terminal I get the same error as above:

node /var/www/webste/public/node/test.js  

I guess, from the Laravel controller I need to execute the test.js file that is saved in the root of the server and not the test.js file located in the public/node directory of the Laravel app. I'm having a hard time figuring out how to do this.

0

There are 0 best solutions below