Run function in running process on node js using command line - javascript

I would like to know if it's possible to run a specific function on a running process on Node JS. And how to do this
actually im using
npx run-func server.js functionName
but it completely restarts the file.
For information, I can't split the file in two since I use a database that can only be read by one process. So... If you have any advice please

Related

Not able to fork external JavaScript server file in Electron

so I am making an application that requires a backend API, and it uses certain node_modules which don't work when compiling with Electron. To fix this, I put the API code into a separate JavaScript file, which I am attempting to fork using child_process.
I have gotten this to work when compiling, but it immediately stops working after I move the "win-unpacked" folder or try to install the app using the compiled installer.
I have checked, and it is not the path that is wrong, it is correctly pointing to the file. From testing, it appears that the file actually does get forked, but immediately exits with the status code 1.
I can't use require(./filepath.js) because that will just include the code in the compiler, which doesn't work with the modules I am using.
I am hoping someone knows what is wrong and what I should do to fix it, or have any ideas for other ways to run the server code without including it in the compiler.
I am using Vue.js 3 and vue-cli-electron-builder version 2.1.1
The server I am attempting to run is a express server.

JavaScript on Visual Studio Code - is Node required to run a file?

I am currently trying to catch up with some JS basics, before continuing with my node.js-project.
I wanted to execute the file and kept getting the error 'console is not defined', when I wanted to use console.log('Hello!')
The only helpful way to run the file normally was with: node ./myjsfile.js
I installed node.js for that node project.
But for just running super basic JavaScript I don't want node.js.
What are better (lighter!) ways to run my JavaScript files inside Visual Studio Code?
Like what's the most basic way to do it?
No, if you do not currently have Node, you will not have the 'play button' to run the code. So, if you go to Run in the toolbar at the top, you should see some options to run the file.
I'm pretty sure you can just press F1 and then Run Code

Unexpected command line syntax error in node js

I have been trying to run some node js files. I have checked the files and they seem to have no syntax errors whatsoever. However, upon running the file on the node js command line i get this error
node first.js
^^^^^
SyntaxError: Unexpected identifier
Lately, I have also been unable to run my apache web server from XAMPP. Could this be a port assignee error. Or is it something else. I have also, checked the directory of the file and it checks out
Some help would be greatly appreciate
When you're inside node's CLI, you can run JS or rather node supported instructions. Unfortunately, there is no global variable or keyword with name "node". If you want to run "first.js", you'll have to come out from node's cli and then run the command.
You can't run file with node first.js inside file js
node is undefined
That is't right syntax
If you want run first.js file, you can include it with require
let first = require('first);
but this way run it once (first time call it), you can use method inside first.js with export(or module.exports) of first.js.
But you just want call and run first.js, you can use child_process.

I can't figure out how to run this simple program I found on GitHub

https://github.com/Valish/sherdog-api
After downloading it what I did was I first installed node.js, which I think I need in order to get everything for this (I have never used any of this stuff before at all). Then I went into command prompt and navigated to the place where the program im trying to use is. I then typed npm install and it installed all the files the program needs. I think that installed it correctly.
I then went into my code editor and tried to run the code through a browser. That didn't work because there is a function or something called "require" that I guess the browser doesn't have access to or anything.
Online, people seem to be saying that you run node.js programs through command prompt?
To test that I created a JavaScript file that has just this in it console.log('Hello');.
I then navigated to the file location and typed in command prompt "node hello.js" and it printed to the console so I know that works. What I don't know is it relevant that it works. This simple JavaScript file could have nothing to do with how the other program runs. I don't know.
Knowing that the simple file ran like this I did the same thing with the index.js file of the program I want. But when I do this nothing happens in the command prompt, it just brings up another command line. I can't figure it out.
On the GitHub site in the readme.md file for this program there is a "usage" section. However it doesn't explain how to use it as far as I can tell. It says
var sherdog = require('sherdog');
var url = "http://www.sherdog.com/fighter/Matt-Riddle-34072"
sherdog.getFighter(url, function(data) {
console.log(data);
})
If this is how you use it I don't know what to do with this, where to input it, or anything. There is no real documentation to go with it at all. I'm stuck at this point; I don't know where to learn or what to look at.
You need to type the sample code into a new .js file. sherdog-api is an api, which means it will not run any code by itself. Run your new .js file with node.
Node is a server running with JavaScript. It is mainly based on modules. A module just contains some code that helps you, most modules can't actually be run directly just like in your case. Instead you have to install the module, and then you can write code that works with it. At first make a new folder for your project, e.g. myCoolServer then open a command prompt in that folder and run:
npm init
To set up everything you need to run nodejs here, then run
npm install sherdog
To install the module. It will appear under /node-modules/. Now just create a new file like you did with hello.js, put the code into it:
var sherdog = require('sherdog');
var url = "http://www.sherdog.com/fighter/Matt-Riddle-34072";
sherdog.getFighter(url, function(data) {
console.log(data);
});
console.log("searching...");
And then run it using node thatfile.js, and you should see the data appearing after a while.
If you succeeded with that, you can continue here...

call to functiolity just one time in node js

There is method which I need to run just once when the node application is started ,I think about to put it in the server.js which is my executable file to the application but Im not sure that this is the right way to go,which other option I can use in this case?
Depending on when specifically it's required to be run, it may be suitable to put the function into a separate javascript file, and execute it as part of your npm start script within your package.json?
Based on your comment below stating it is a child process. It would be appropriate to execute it from the server file, but best practice would be to abstract the function out into a separate file, require it into your server file and then call it.

Categories