0

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.

Rabby
  • 322
  • 4
  • 15
Adam Winster
  • 69
  • 1
  • 9
  • `require('./scrapedin');`? – Cyclonecode Oct 12 '20 at 04:06
  • Same error as above. I think it is still looking in the Laravel public directory when it needs to be looking for this file outside the Laravel app. /usr/local/lib └─┬ scrapedin@1.0.21 – Adam Winster Oct 12 '20 at 04:16
  • I think not, try to first go in to the directory where the script is e.g `/var/www/website/public/node/test.js` and try to run `node test.js` and I think it will work just fine. I think problem is that node is not resolving the paths correctly to your node_modules folder and so on. – Cyclonecode Oct 12 '20 at 04:20
  • I think you first need to change directory to the path where test.js i located or perhaps you could try doing something like `const scrapedin = require(__dirname + '/node_modules/scrapedin');`. I guess you could also install the package globally if possible `npm i -g scrapedin` which I think also would solve the problem. I think you will have a similar issue with the `cookies.json` file. – Cyclonecode Oct 12 '20 at 04:25
  • Does the `public/node` folder have a `node_modules` folder? – Cyclonecode Oct 12 '20 at 04:32
  • Tried just installing the package globally and using an absolute path does not work, so I guess you will need to actually install the package locally in the same folder as your script. Is this possible? – Cyclonecode Oct 12 '20 at 04:41
  • The public/node folder only contains the test.js file. The node_modules is located in the root directory of the server. I am able to install the package locally in the public/node folder. Just to clarify, it would only be the scraper package I am installing in this directory? – Adam Winster Oct 12 '20 at 05:01
  • Quick side note that might be relevant: I had a similar issue running a Python script. Adding the following line to the top of the Python script resolved the issue: #!/usr/bin/env python – Adam Winster Oct 12 '20 at 05:12
  • const scrapedin = require('/usr/local/lib/node_modules/scrapedin') and const cookies = fs.readFileSync('/usr/cookies.json') - moved cookies.json to usr directory - resolved the errors. However the output is appearing as string(0) "" – Adam Winster Oct 12 '20 at 05:53
  • Yes if you install it locally then it would only be the scraper package since fs is a node core module. – Cyclonecode Oct 12 '20 at 08:02
  • It now works if I run node /var/www/website/public/node/test.js from terminal. When i try run it through Laravel the browser continues to load until it time outs. – Adam Winster Oct 12 '20 at 10:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222927/discussion-between-cyclonecode-and-adam-winster). – Cyclonecode Oct 12 '20 at 16:03

0 Answers0