I'm trying to get the following code which should execute shell command to wor in OSX and Windows.
const exec = require("child_process").exec;
const runCommand = (cmd) => exec(cmd,
function (error, stdout, stderr) {
if (stdout.length > 0) {
console.log(stdout);
}
});
For the following runCommand("./node_modules/.bin/someBinary") it does not work in Windows. But it works fine in OSX. So I wonder what modifications do I need for it to run in Windows as well?
Have you considered giving a try at npm-exec ?
Loads the same environment that would be present for npm run-script , but executes arbitrary bash command lines. (even on windows! Thanks to bashful). This includes modifying $PATH so scripts in node_modules/.bin will be used before global modules.
So you could npm-exec someBinary instead of using a relative path breaking Windows / OSX compatibility
If this module does not suit you, you can also give a try at npm bin as explained in this answer.
Okay, so in the end I solved it with checking which platform I'm in and in turn I just wrote the same command with OS specific so it would run.
In short I used process.platform to find out which OS it is. Not the most elegant. But at least it solved my problem.
Related
I'm getting this error below everytime I try to run 'npx percy exec -- node snapshots.js'.
PowerShell Terminal
Problem Image ->
https://i.stack.imgur.com/XCSj6.png
I have followed this Tutorial -> https://docs.percy.io/docs/percyscript-tutorial
Anyone know how to solve this? I looked everywhere and found nothing.
Thank you in advance!
PowerShell has a different syntax for working with Environment Variables.
Try this:
$env:PERCY_TOKEN = "token"
Powershell Help - About Environment Variables
To set up PERCY_TOKEN on windows we have to use powershell.
1.Open powershell on windows
2. Go to project library through commands cd, cd..
3.Set the PERCY_TOKEN $env:PERCY_TOKEN='your token'
4. Then run your project using powershell through your command npm run test:percy
I want to block chrome.exe in windows firewall. The command for accomplishing the task is
netsh advfirewall firewall add rule name="Block Chrome" dir=out action=block program="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" enable=yes'
Above command works fine when issued via PowerShell. But when I tries to do this
in Nodejs
const execSync = require('child_process').execSync,
command = 'netsh advfirewall firewall add rule name="Block Chrome" dir=out action=block program="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" enable=yes'
const result = execSync( command )
console.log( result )
It prints
Ok.
By output, I am assuming that command is getting executed. But Chrome is not getting affected by that. It can still access the internet.
When I run the same command in PowerShell, it blocks chrome immediately. I am wondering why running the command on Nodejs is not working as expected?
Given that Nodejs has Administrator Rights, what can be the issue here?
You need to escape backslashes:
command = 'netsh advfirewall firewall add rule name="Block Chrome" dir=out action=block program="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" enable=yes'
Using windows my code I currently use:
var file = JSON.parse(fs.readFileSync(filePath), 'utf8');
file[id] = JSON.parse(`{"name":name}`);
fs.writeFileSync(filePath, JSON.stringify(file,null,2);
This works just fine on windows. However, when I transfer this code onto my linux machine for cross-platform testing. The file doesn't get updated until I stop running the application.
What i've tried:
running 'chmod -r 0777 path/to/dir/'
and
checked if it was due to max file open limit (not the case as my limit is extremely large)
Linux: Mint-Linux 4.10.0-38-generic #42~16.04.1-Ubuntu (from uname -a)
Node: 8.10.0
Editor: intelliJ idea
Im also running the code directly from the run button in intelliJ, im not sure if this may be the cause, if it is the cause why would this be the case.
Adding the option {mode: 0o777} as the last arg in the write fixed the files not updating.
The basic setup of the project I'm working on (Windows) is an Electron app which communicates with R via R serve. Now everything is working fine except one thing thats been driving me crazy lately.
The problem is that when I use node from cmd (etc. node startR.js) and execute an R script with a command like this:
var exec = require('child_process').exec;
var executableR = exec('Rscript startR.R', function(error, stdout, stderr) {
console.log(stdout);
});
and R code like:
library('Rserve')
Rserve()
everything goes fine. But, when I try to execute the very same line in the main.js (entrypoint) file for my Electron app, I get the error that the R libraries (i.e. Rserve) can't be found from within R.
I have checked for possible causes for this and I have found out that R library paths differ (.libPaths()) when R is called from cmd node and from electron. Namely, R libs are installed in MyDocuments folder, and R called from cmd node seems to see them properly. On the other hand, the same exec from electron makes R see only the default library path (in program files where R is installed), so it fails to find libraries.
I know that this must be environment issue (like its run from an environment different from the current user), but I'm pretty uncertain what to try. I thought that passing process.env to {env: } options for child_process.exec but to no avail.
I'm trying to deploy my Rails app to a new VPS which runs with Debian. I'm using Nginx and Phusion Passenger as my server.
I've installed Node.js as a JavaScript runtime. Sadly I'm seeing the following error message:
Fatal error in v8::V8::AddGCPrologueCallback()
V8 is no longer usable
Edit:
It is fixed now. Apparently gr security was causing the error.
The Problem occurs on kernels with grsecurity and certain restrictive rules.
node.js needs to exec code in certain areas of memory,where the server does not like it.
You seem to need to switch two flags for the "node" binary.
Also you might need to switch those for your ruby binary.
Toggle SEGMEXEC on
paxctl -S `which node` && paxctl -S `which ruby`
Toggle MPROTECT off
paxctl -m `which node` && paxctl -m `which ruby`
Test if node works now
node -e "console.log(1+1);"
Fun fact is that i can now Toggle SEGMEXEC off and node still works :S... however switching it on seemed to initially fix it for me.
If it does not work, play with the flags and try the node -e
Good luck!
gizmore