How to run node.js file from PHP file - node.js

so I've been trying to run an SMS script after the user submits a form.
If I use
node send-sms.js
locally on my machine, it works.
However, if I try to use
$sendSmsPath = "send-sms.js";
exec('/public_html/node_modules/node '.$sendSmsPath);
Nothing happens. No error, nothing. I tried using a relative path, as well as an absolute one. My folder scheme is as follows: public > other_folder > php > php_file.php AND public > node_modules
Any ideas are welcome, thanks

You are using the wrong path to Node.js. Try this:
<?php
$sendSmsPath = "send-sms.js";
exec('/c/Program Files/nodejs/node '.$sendSmsPath);
node_modules is for the dependencies of your script, not the actual Node.js executable.
If you'd like to run your script on another machine make sure it has Node.js installed globally and you know the path of the executable (can be found with which node)

Related

PATH variables empty in electron?

I'm trying to access items in my PATH environment variable from my electron instance, when I run it with npm start while developing it through node.js I get all the expected variables, but when I run the electron application with my resources inside I'm left with only usr/bin
This is how it looks like when I run it from npm:
And this is how it looks when run from the electron mac application precompiled:
Does anyone know why this could be the case? And if I can do anything to reach my normal PATH variables
UPDATE:
After a lot of research I found out that GUI applications ran from finder or docker in Mac OSX use different environment variables compared to if they are ran from the terminal:
This can be edited through plist files, either globally or application specific
You can use fix-path package. Works perfectly!
const fixPath = require('fix-path');
console.log(process.env.PATH);
//=> '/usr/bin'
fixPath();
console.log(process.env.PATH);
//=> '/usr/local/bin:/usr/bin...'

nodejs module.js:340 error: cannot find module

I installed nodejs in C:\Program Files (x86)\nodejs
then I created a .js file and saved it in my desktop just to output 'hello world' in the console:
console.log('hello world');
When I tried to run the file from the command prompt:
C:\Users\Laura>cd desktop
C:\Users\Laura\Desktop>node nodeTest.js
I get:
module.js:340
throw err;
^
Error: Cannot find module 'C:\Users\Laura\Desktop\testNode.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
I read many other related questions, and some of them recommend to run the install, and so I did.
C:\Users\Laura>npm install -g express
But no luck, still getting the same error message.
EDIT: This answer is outdated. With things like Yarn and NPM 5's lockfiles it is now easier to ensure you're dependencies are correct on platforms like Heroku
I had a similar issue related to node_modules being modified somehow locally but the change was not reflect on Heroku, causing my app to crash. It's relatively easy fix if this is your issue:
# Remove node_modules
rm -fr node_modules
# Reinstall packages
npm i
# Commit changes
git add node_modules
git commit -m 'Fix node_modules dependencies.'
git push heroku master
Hope that helps for others with a similar issue.
I was having the same problem with a server someone had written a while back. It turns out I didn't have a few things installed that the script required.
This question was helpful to me.
I ended up being able to use:
npm install yourMissingDependency
Although in your instance it may just be a file that it doesn't have a path to.
You could try putting that file in the same folder you are running the node script from.
I think the issue is the NODE_PATH environment variable. I had the same problem but once I issued (on Windows)
set NODE_PATH=C:\Users\MyUser\node_modules
then all worked ok.
Cheers,
Restart your command prompt and check your path variable (type: path).
If you can't find find nodejs installation dir from output add it to the path variable and remember to restart cdm again...
I had a nearly identical issue, turned out my JS file wasn't actually in the folder I was calling it from, and I had gone one folder too deep.
I went up one directory, ran the file, it recognized it, happily ever after.
Alternatively, if you go one folder up, and it gives you the same error, but about a different module, take that same file in your parent folder and move it into the subfolder you were previously trying to run things from.
TL;DR- your file or its module(s) is not in the folder you think it is. Go up one level
Try typing this into the Node command-line environment:
.load c:/users/laura/desktop/nodeTest.js.
It should work for what you're trying to do.
If you want to call the file directly, you'd have to have it in the root directory where your Node installation resides.
Hi fellow Phonegap/Cordova/Ionic developers,
I solved this issue by doing the following
1. C: drive -> Users -> "username" eg. john -> AppData -> Roaming
2. Inside the "Roaming" folder you need to delete both "npm" and "npm-cache"
folder.
3. Now build your project, and it should work
Happy coding!!!
I faced the same problem. I just copied the testNode.js file(that contain the test code) and pasted into the root of nodejs directory manually. I tried this command C:\Program Files (x86)\nodejs>node testnode.js
Bingo! I received this message.
Then I typed this url in a browser and received the message "Hello World".
Hope this help somebody.
You need to:
Remove the tick of Hide extensions for known file types from Windows Explorer folders option
Your file will appear as testNode.js.txt
Remove the trailing .txt so as the file to be resolved as JS file
That's it, now it works!
I had the same problem then I found that I wasn´t hitting the node server command in the proper directory where the server.js is located.
Hope this helps.
Try npm install --production and then npm start.
Easy way for this problem
npm link e
I also got this issue and this was due to wrong path that we mention while running. Check your file path and also make sure that there is no space between the name of your directory name.
Try npm start in Node.js Command Prompt.
Look at the end of the messages - it gives you the path of log file in "Additional Logging Details ..." something like c:\users\MyUser\npm-debug.log
Open this file in Notepad and find the real address of Node.exe :
something like C:\\Program Files\\nodejs\\\\node.exe
Try cd to this path
Call node.exe + <full path to your server file.js>
Server is listening on port 1337 !
I had the same problem, but finally I tried copying the Node_modules folder in the same project folder, and it worked.
I executed following command and it works for me.
PM> npm install ee-first ee-first#1.1.0 node_modules\ee-first
While i tried to run my first node application i had encountered the same issue but when i tried to see what the issue might be it was the path of the file. I was running the node .js from different location and hence encountered the issue. When i fixed the path this worked fine.
In my case, i got this error because i was just in the wrong directory. So node couldnt find the module that i wanted to run. Just be sure your node file is in your Desktop directory.
If you are using a framework like express, you need to put the package.json file into the folder you are using and don't forget change main name.
Remove the fs extra from the path -
path: 'D:\\FrontEnd Dev\\Git projects\\Appname-master\\node_modules\\webpack-manifest-plugin\\node_modules\\fs-extra\\package.json',
requestPath: 'fs-extra'
And run the command npm i
Make sure you saved the file as JavaScript. Un check 'Hide extensions for all known type' check box in Folder Options window will show you the correct file extension(Folder>>view>>Option).
Faced the same problem while trying to run node-red.
node <directory structure where js is located>/red. js
In my case it was :
node AppData/Roaming/npm/node_modules/node-red/red.js

node jitsu cannot find local modules

I have an application successfully working locally so I know the code works. However when I go to deploy to node jitsu I get an error that it cannot find a local module. Here is what I have:
File Setup:
/index.js
/config/config.js
index.js
var cfg = require('./config/config.js');
When trying to deploy node jitsu is giving me an error:
Error: Cannot find module './config/config.js'
Since all this code works locally I do not believe this is a coding issue. I am under the impression that local modules do not need to be included in package.json but perhaps they do for node jitsu? I read their documentation but cannot find anything special for local modules.
Thanks!
Local modules like this should work properly.. so long as you don't have it in .gitignore or .npmignore.
Modules in the node_modules directory require that you add it to the bundledDependencies array in your package.json file.
An easy way to check for whether the file is included in your deploy is to run tar -tf $(npm pack).
I had this exact same error on deploy, but caused by a different root cause. In case anybody stumbles into the same problem:
File Setup:
/public/Data/TargetData.js
app.js require statement:
var target = require('./public/data/TargetData.js');
My local Mac OSX environment allowed the capitalization difference of /data/ vs. /Data/ - the Nodejitsu server did not.

Error code: 800A1391 Source: Microsoft JScript runtime error Running Grunt - Module is undefined

New Grunt user here who is using a lot of new tools (npm nodejs) today.
I've got Grunt "installed" and have been able to create a grunt.js file using the init task as described here: http://net.tutsplus.com/tutorials/javascript-ajax/meeting-grunt-the-build-tool-for-javascript/ and here: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md. But whenever I run the "grunt" command I get an error:
Windows Script Host
Script: c:\users\[]\Documents\code\grunt\grunt.js
Line: 2
Char: 1
Error: 'module' is undefined
Code: 800A1391
Source: Microsoft JScript runtime error
As explained in the FAQ, you need to type grunt.cmd instead on Windows because the OS tries to launch grunt.js
Or you can install grunt-cli globally instead. This package will run any version of Grunt if it's been installed locally to your project.
SOLVED !!
So, this problem occur because windows by default associative < *.js > files
with >>
"Microsoft Windows Based Script Host".
grunt need to open by default with (grunt.cmd).
it easy to slove, by change default app (open with..)
Guide :
Go to any javascript file with "js" extension. (any file)
Right-Click(mouse) > Properties > "Opens with:" Change...(button)
Choose Notepad ( or any javascript IDE ).
PROBLEM SOLVED ! :)
good luck
If you're getting a "Microsoft JScript runtime error" that means that node.js isn't even getting invoked; instead Windows Script Host is trying to run your code. That's probably a problem with filetype associations; IIRC Windows defaults to trying to execute a ".js" file with WSH. You may wind up having to create a shortcut to your script, specifying a command line (probably something like "node %1") and a starting directory in order to make sure that it's executed properly.
It would help if you could tell us exactly how you're trying to invoke your code.
it seems that in the latest versions of the grunt modules, you would have to do the following to have it work under windows:
remove any globally installed grunt
npm uninstall -g grunt
install grunt-cli globally
npm install -g grunt-cli
install grunt locally into your project
npm install grunt
installing grunt (v0.4.x) globally does not seem to create the necessary grunt.cmd anymore. it seems that the recommendation is now to have grunt installed locally to be able to use version-specific Gruntfiles
As Florian F suggested, running grunt.cmd works. This is because of the process Windows is looking for your grunt command.
When typing grunt -h Windows will proceed to look for the following files:
./grunt.cmd
./grunt.* (grunt.js is found in this case which is why you see "module is undefined")
%APPDATA%/npm/grunt.cmd
An alternative to using "grunt.cmd" is to use grunter which simply renames the command to grunter... then you no longer have this problem.
To answer this, first we need to understand that the error is caused because it is being executed by Windows Script Host.
Now, run the code from your cmd promt with the following syntax:
>node <application_name>.js
this will allow the Node.js application to open through V8 JavaScript engine(Google's).
P.S: Please reply back if this has helped in resolving your issue else post the problem you are facing after trying this.
I had a similar issue, the problem is file association, I would recommend:
right click on a .js file and choose open with.
then you choose nodejs/node.exe (somewhere in "program files" folder
then make tick box where it says "always open .js files " (paraphrasing)
That should do the trick.
I went through the same issue when running an old Node project.
The issue was with the name of the js file, it was node.js. So the while running the command node node.js, it was opening up a windows dialogue box.
I just changed the name of the file to app.js and the error flew away.
So, in my case i had tryed all the mentioned above with no result.
But i have fund that im dont type: node in the full sentence as the following snipet
node script.js.And remember never understimate your own miscoding.
Solution:
Go to any javascript file with "js" extension. (any file)
Right-Click(mouse) > Properties > "Opens with:" Change...(button)
Choose Notepad ( or any Javascript IDE like VS Code ).

getting node.js working

So I'm following:
http://giantflyingsaucer.com/blog/?p=894
and installing node.js.
I got up to the part with sudo make install.
It works, then it says to create a js file.
What I don't understand is where I put the sayhello.js?
Node.js looks at least for the programmer more like a interpreter. Thus, you can place your sayhello.js whereever you want and run it by executing node sayhello.js.
However, you might consider using external modules. Then you must check that they are set by full path or the relative path can be resolved from the location you execute node in.

Resources