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.
Related
I'm building a static blog using Nextjs 13 and deploying it on Vercel. I builded and started the project locally and everything was working, but on Vercel I got this error:
ERROR Error: ENOENT: no such file or directory, open 'posts/second.md' at Object.openSync (node:fs:600:3) at Object.readFileSync (node:fs:468:35) at getPostBySlug (/var/task/.next/server/chunks/207.js:146:63) at Post (/var/task/.next/server/app/posts/[slug]/page.js:603:52) at T (/var/task/.next/server/chunks/760.js:11441:25) at Ma (/var/task/.next/server/chunks/760.js:11604:33) at Array.toJSON (/var/task/.next/server/chunks/760.js:11397:32) at stringify () at V (/var/task/.next/server/chunks/760.js:11696:53) at ping (/var/task/.next/server/chunks/760.js:11496:43) { errno: -2, syscall: 'open', code: 'ENOENT', path: 'posts/second.md'}
The error happens when I go to the "/posts/second" route for example, not in the main page
This is the code interested:
const getPostBySlug = (slug: string) => {
const folder = "posts/";
const file = `${folder}${slug}.md`;
const content = fs.readFileSync(file, "utf8");
return matter(content)
};
The posts folder is located in the root folder.
I tried to modify the next config by adding the output option and setting it to 'standalone':
const nextConfig = {
// config
output: 'standalone',
}
I also tried to modify the path to locate the folder but nothing seems to work.
If more information is needed. project is published on GitHub
I solved my problem by looking at a tutorial provided by Vercel, so thanks to this line of code path.join(process.cwd(), 'posts'); the folder path is resolved.
In a Mongo shell script I need to read a file to delete documents by _id but I can't import the FileReader library.
I launch script from bash to do simple find() and it works:
mongosh --host xxx --port 27017 --username xxx --password xxx --eval "var country='$country';var file='$inputDataFile'" --file scriptFile.js
But whenever I try to import a library in the js it shows the error:
TypeError: Cannot assign to read only property 'message' of object 'SyntaxError:
'import' and 'export' may appear only with 'sourceType
The same js file I call from nodejs and the import is correct.
Right now I do the deletion of _ids contained in a file using nodejs. I would like to find a way to use Mongo shell script for all my queries
I managed to solve my problem with the help of Include External Files and Modules in Scripts
Create package.json in the same directory as your js
Add the necessary libraries to package.json
Install libraries
Use require to import it
Code
var fs = require('fs');
console.log(country)
console.log(file)
db = db.getSiblingDB('mongotest')
const allFileContents = fs.readFileSync(file, 'utf-8');
var lines = allFileContents.split("\r")
for (var i = 0; i < lines.length; i++) {
const user = JSON.parse(lines[i]);
var userId = user._id
if (!(userId instanceof ObjectId)) {
userId = new ObjectId(userId);
}
db.userChat.deleteOne({ _id: userId })
}
Thanks for your help
so I'm trying to set up a very basic tfjs project but am stuck in an early part of getting it running. Running into a file path not found error: 'Error: ENOENT: no such file or directory'. My code looks like this:
const tf = require('#tensorflow/tfjs')
require('#tensorflow/tfjs-node')
async function test_function() {
const house_sales_dataset = tf.data.csv('file://./datasets/kc_house_data.csv');
const sample = house_sales_dataset.take(10);
try {
const dataArray = await sample.toArray();
} catch(err) {
console.log(err)
}
}
test_function();
My file structure for the npm project is the following:
Root project directory is called 'tensorflow-practice'. Inside this is app.js, package.json, package-lock.json, node-modules, and a folder called 'datasets'. Inside 'datasets' folder is the .csv file called 'kc_house_data.csv'.
https://imgur.com/a/jWLr3wt
Not sure why this isn't working...any help much appreciated.
I'm having trouble identifying the path to a file in the public directory c:\TEMP\todos\.meteor\local\build\programs\server\public\main.py. Meteor complains the file or directory doesn't exist. Already searched the other postings about the similar issue (e.g., Reading files from a directory inside a meteor app) but didn't help.
Here is the error message.
=> Your application has errors. Waiting for file change.
=> Modified -- restarting.
=> Meteor server restarted
W20151206-04:05:57.893(-5)? (STDERR) Error inside the Async.runSync: ENOENT, no such file or directory 'c:\TEMP\todos\.meteor\local\build\programs\server\public'
Client code
Meteor.call('runPython', function(err, response) {
if(err){
} else {
console.log(response);
}
})
Server code
Meteor.startup( function (){
Meteor.methods({
runPython: function (){
var PythonShell = Meteor.npmRequire('python-shell');
var fs = Meteor.npmRequire('fs');
var runPython = Async.runSync(function (done){
var files = fs.readdirSync('./public/');
// PythonShell.run('main.py', function ... was tried first but Meteor complained that "main.py doesn't exist". So below is a different attempt.
var py = _(files).reject(function(fileName){
return fileName.indexOf('.py') <0;
})
PythonShell.run(py, function (err) {
// PythonShell.run(path.join(py,"main.py") ... was also tried but resulted in the same error
if (err) throw err;
console.log('script running failed');
});
})
return "Success";
}
})
})
All files inside the public folder should be read using '/':
var files = fs.readdirSync('/');
More here: http://docs.meteor.com/#/full/structuringyourapp
For server-side only (might be your case and probably a better solution) you can put everything under the private/ folder and access them by using the Assets API: http://docs.meteor.com/#/full/assets_getText
Clearly I was overthinking it. Specifying a full path to the file was all I needed to do.
PythonShell.run('c:\\project\\public\\main.py', function ...
If your application allows moving the Python script to /private instead of /public, you can take advantage of Meteor's Assets:
Assets allows server code in a Meteor application to access static server assets, which are located in the private subdirectory of an application’s tree. Assets are not processed as source files and are copied directly into your application’s bundle.
e.g. If you move your script to /private/scripts/script.py you can generate the absolute path the Meteor way by doing Assets.absoluteFilePath('scripts/script.py').
On Windows 7. In a proprietary app that contains chromium browser. Running on Node.js and Express. In my file 'javascripts/getMyFiles.js' I'm trying to use the following code from: http://nodeexamples.com/2012/09/28/getting-a-directory-listing-using-the-fs-module-in-node-js/
#!/usr/bin/env node
var fs = require("fs"),
path = require("path");
var p = "../"
fs.readdir(p, function (err, files) {
if (err) {
throw err;
}
files.map(function (file) {
return path.join(p, file);
}).filter(function (file) {
return fs.statSync(file).isFile();
}).forEach(function (file) {
console.log("%s (%s)", file, path.extname(file));
});
});
I have 3 problems with this code:
I get and 'Unexpected token ILLEGAL' error from the '#!/use/bin/end node' line
After deleting that first line of code I get the error 'Uncaught ReferenceError: require is not defined'. If I move the '...require...' lines to my root app.js file then the object fs is undefined in my getMyFiles.js file.
I need to specify which folder to list when my app gets an event
I get that require is not available on the client-side, but I don't want to get a list of files from the client side. The files I want to list are in the same path as all of my other files, '/public/Docs'. I can load a known file straight away, but I need to present the user with a list of available documents first. Any assistance will be most appreciated.