php-node Node Express cannot find module - php

The error is:
Cannot find module 'php'
Error: Cannot find module 'php'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at new View (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\view.js:50:49)
at EventEmitter.app.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\application.js:545:12)
at ServerResponse.res.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\response.js:938:7)
at F:\Users\MichaelJacksonIsDead\publisherServer\routes\index.js:12:6
at Layer.handle [as handle_request] (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\layer.js:82:5)
at next (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\route.js:110:13)
The Route for this page to render as php is as follows:
router.get('/index.php', function(req, res, next) {
var render = require('php-node')({bin:"F:\\xampp\php\\php.exe"});
res.render('index.php');
});
I have run a npm install -g, npm install node-php -g at both project level and level above with no success:
F:\Users\MichaelJacksonIsDead\publisherServer>npm install -g
publisherServer#0.0.0 F:\Users\MichaelJacksonIsDead\AppData\Roaming\npm\node_mod
ules\publisherServer
├── php-node#0.0.2
├── debug#2.2.0 (ms#0.7.1)
├── serve-favicon#2.2.1 (ms#0.7.1, fresh#0.2.4, parseurl#1.3.0, etag#1.6.0)
├── cookie-parser#1.3.5 (cookie#0.1.3, cookie-signature#1.0.6)
├── morgan#1.5.3 (basic-auth#1.0.1, depd#1.0.1, on-finished#2.2.1)
├── body-parser#1.12.4 (bytes#1.0.0, content-type#1.0.1, depd#1.0.1, raw-body#2.
0.2, on-finished#2.2.1, qs#2.4.2, iconv-lite#0.4.8, type-is#1.6.2)
├── express#4.12.4 (merge-descriptors#1.0.0, utils-merge#1.0.0, methods#1.1.1, f
resh#0.2.4, cookie#0.1.2, escape-html#1.0.1, cookie-signature#1.0.6, range-parse
r#1.0.2, content-type#1.0.1, parseurl#1.3.0, finalhandler#0.3.6, vary#1.0.0, ser
ve-static#1.9.3, content-disposition#0.5.0, path-to-regexp#0.1.3, depd#1.0.1, on
-finished#2.2.1, qs#2.4.2, etag#1.6.0, proxy-addr#1.0.8, send#0.12.3, type-is#1.
6.2, accepts#1.2.7)
├── hjs#0.0.6 (hogan.js#3.0.2)
└── less-middleware#1.0.4 (mkdirp#0.3.5, node.extend#1.0.10, less#1.7.5)
F:\Users\MichaelJacksonIsDead\publisherServer>npm start
> publisherServer#0.0.0 start F:\Users\MichaelJacksonIsDead\publisherServer
> node ./bin/www
GET /index.php 500 41.145 ms - 954
Does anyone have any expirence with this issue? As far as I can tell I have correctly installed it... also to make sure I added the package to my json file as follows:
"dependencies": {
"body-parser": "~1.12.4",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"express": "~4.12.4",
"hjs": "~0.0.6",
"less-middleware": "1.0.x",
"morgan": "~1.5.3",
"serve-favicon": "~2.2.1",
"php-node": "0.0.2"
}
Lastly I did not have enough rep to open a tag php-node for the project # https://www.npmjs.com/package/php-node, not a issue but would of been nice to have a more speficic tag :)

I have found the answer, thank you #mscdex and #adeneo
I replaced the default:
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
with
// use PHP as view engine in Express
var phpnode = require('php-node')({bin:"F:\\xampp\\php\\php.exe"});
app.engine('php', phpnode);
app.set('view engine', 'php');
under routes:
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index.hjs', { title: 'Express' });
});
router.get('/index.php', function(req, res, next) {
res.render('index.php');
});
The app still supports php and index.hjs pages.

You should only use -g when you're installing a module from npm that contains a command line utility (e.g. express-generator is an example of this). However in almost all cases you should just simply do npm install <module name> which will install the module locally and allow you to require() it.
So do npm install in your project's root directory and require() should be able to find php-node.

only if the plugin are use you are not 100% happy of it there is a simple method(this is only a prototype so if you are node.js expert you may improve it) to build FROM ONLY ONE SERVER APPS NODE.JS - PHP (and theoretical you have yet sqlite3 and after installing a mariadb or mysql server you can use them as well) :
https://stackoverflow.com/a/68422021/5781320

Related

How to use rtlcss node package with twitter-bootstrap on Laravel?

It may sound stupid but I've recently started working with laravel mix for compiling scss and js files. But I can't understand something.
I want to use rtlcss npm to make the twitter-bootstrap rtl.
This is the default app.scss asset of Laravel
// Fonts
#import url("https://fonts.googleapis.com/css?family=Raleway:300,400,600");
// Variables
#import "variables";
// Bootstrap
#import "~bootstrap-sass/assets/stylesheets/bootstrap";
And this is the default app.js asset:
window._ = require('lodash');
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');
} catch (e) {}
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
I've installed the rtlCSS through node package, now I what I want to happen is when I run npm run dev or the watcher, it compiles the bootstrap scss file and make it RTL and put it in the public css directory like it does with the default LTR that is.
Your can easily generate RTL css with Laravel Mix:
First, to use Laravel mix you should install the necessary libraries from npm:
npm install
Then, install webpack-rtl-plugin: npm install webpack-rtl-plugin --save-dev
Then, in webpack.mix.js (located at the root of your laravel app) import the pulgin at the top:
let WebpackRTLPlugin = require('webpack-rtl-plugin');
Then, replace the:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
with the following:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css')
.webpackConfig({
plugins: [
new WebpackRTLPlugin()
]
});
Finally, run: npm run dev (for develpoment) or npm run prod (for production)
As a result, Larave Mix will generate:
public/css/app.css (the original css from sass)
public/css/app.rtl.css (the RTL css version)
and public/css/app.js
I tried multiple solutions with no luck after multiple tries the below code works fine and it covers all mix features like versioning, source-maps, etc.
Don't forget to install rtlcss package
mix.sass('resources/sass/app.scss', 'public/css');
mix.postCss('public/css/app.css', 'public/css/app.rtl.css', [
require('rtlcss'),
]);

Gulp Command running Error in Laravel 5.3

I am new in Laravel. I am trying to run gulp command in CMD to get CSS and JS (vue.js) file. I am getting following error while I am trying to run gulp command in CMD. I am using Laravel 5.3.
{ [Error: ./resources/assets/js/app.js
Module build failed: ReferenceError: Unknown plugin "add-module-exports" specified in "base" at 0, attempted to resolve relative to "D:\\php7\\htdocs\\addbook\\
resources\\assets\\js" at D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:176:17
at Array.map (native) at Function.normalisePlugins (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:154:20)
at OptionManager.mergeOptions (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:229:36)
at OptionManager.init (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\options\option-manager.js:374:12)
at File.initOptions (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\index.js:216:65)
at new File (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\file\index.js:139:24)
at Pipeline.transform (D:\php7\htdocs\addbook\node_modules\babel-core\lib\transformation\pipeline.js:46:16)
at transpile (D:\php7\htdocs\addbook\node_modules\babel-loader\index.js:38:20)
at Object.module.exports (D:\php7\htdocs\addbook\node_modules\babel-loader\index.js:131:12)]
message: './resources/assets/js/app.js\nModule build failed: ReferenceError: Unknown plugin "add-module-exports" specified in "base" at 0, attempted to resolv
e relative to "D:\\\\php7\\\\htdocs\\\\addbook\\\\resources\\\\assets\\\\js"\n at D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\f
ile\\options\\option-manager.js:176:17\n at Array.map (native)\n at Function.normalisePlugins (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\t
ransformation\\file\\options\\option-manager.js:154:20)\n at OptionManager.mergeOptions (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transforma
tion\\file\\options\\option-manager.js:229:36)\n at OptionManager.init (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\file\\optio
ns\\option-manager.js:374:12)\n at File.initOptions (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\file\\index.js:216:65)\n at
new File (D:\\php7\\htdocs\\addbook\\node_modules\\babel-core\\lib\\transformation\\file\\index.js:139:24)\n at Pipeline.transform (D:\\php7\\htdocs\\addboo
k\\node_modules\\babel-core\\lib\\transformation\\pipeline.js:46:16)\n at transpile (D:\\php7\\htdocs\\addbook\\node_modules\\babel-loader\\index.js:38:20)\n
at Object.module.exports (D:\\php7\\htdocs\\addbook\\node_modules\\babel-loader\\index.js:131:12)',
showStack: false,
showProperties: true,
plugin: 'webpack-stream',
__safety: { toString: [Function: bound ] } }
Could anyone give me any solution regarding this ?
gulpfile.js
var elixir = require('laravel-elixir');
require('laravel-elixir-vue');
elixir(function(mix) {
mix.sass('app.scss');
});
elixir(function(mix) {
mix.webpack('app.js');
});
elixir(function(mix) {
mix.version(['css/app.css', 'js/app.js']);
});
From the Laravel Docs:
If you are developing on a Windows system or you are running your VM
on a Windows host system, you may need to run the npm install command
with the --no-bin-links switch enabled:
npm install --no-bin-links
Laravel Docs: Elixir Installation and Setup
Remove /node_modules and run npm install with the --no-bin-links switch like mentioned in the docs and quoted above.

Laravel BrowserSync without proxy

I'm trying to use Elixir BrowserSync, one of the newest features of Laravel.
I added it to the gulp file
elixir(function(mix) {
mix.sass('app.scss')
.browserSync();
});
when I run "gulp watch" it output this message
[20:49:20] Using gulpfile ~/Desktop/laravel/gulpfile.js
[20:49:20] Starting 'watch'...
[20:49:20] Finished 'watch' after 11 ms
[BS] Proxying: http://homestead.app
[BS] Access URLs:
----------------------------------
Local: http://localhost:3000
External: http://10.9.0.48:3000
Automatically a browser is launched with the url http://localhost:3000 but nothing loads.
I think the problem is related to this line:
[BS] Proxying: http://homestead.app
You can if you use BrowserSyncs server.
mix.browserSync({
proxy: false,
server: {
baseDir: './'
}
});
Are you running multiple homestead sites? It looks like it defaults to proxying through homestead.app if you do not include a proxy setting. If you don't host your local on homestead.app it won't find it.
Try
.browserSync({ proxy: 'domain.app' });
Where domain.app is what you are serving the site with.
The direct answer to your question - it is not possible to not proxy the .browserSync. Long side-answer - you can still use elixir's implementation of browserSync in gulp, but you must have a separate PHP server to interpret the files.
Edit: I saw a way to not proxy by providing the script in the pages manually (by not giving any arguments in the .browserSync() call, but don't remember where and how to do it exactly.
The reason is that browserSync requests all the data from the server for you, injects a little javascript listener and displays everything else:
<script type='text/javascript' id="__bs_script__">//<![CDATA[document.write("<script async src='/browser-sync/browser-sync-client.2.9.11.js'><\/script>".replace("HOST", location.hostname));//]]></script>
One way is to php artisan serve --host=0.0.0.0 in a separate terminal to start up the server and then use the .browserSync({proxy: 'localhost:8000'}). It all works - the BrowserSync UI and all the calls to refresh the pages.
Note: I could not connect to artisan serve without the --host=0.0.0.0.
For those wanting an all automated solution via gulp, here it is:
Use the gulp packages gulp-connect-php and laravel-elixir-browsersync2. The first starts up the php artisan serve and the other connects it to browserSync.
Install the packages:
npm install laravel-elixir-browsersync2 --save-dev
npm install gulp-connect-php --save-dev
Add the required lines to your gulpfile.js:
Code:
var elixir = require('laravel-elixir'),
gulp = require('gulp');
var connectPHP = require('gulp-connect-php');
var BrowserSync = require('laravel-elixir-browsersync2');
elixir(function(mix) {
mix.sass('app.scss');
});
elixir(function(mix) {
connectPHP.server({
base: './public',
hostname: '0.0.0.0',
port: 8000
});
BrowserSync.init();
mix.BrowserSync(
{
proxy: 'localhost:8000',
logPrefix : "Laravel Eixir BrowserSync",
logConnections : false,
reloadOnRestart : false,
notify : false
});
});
Then - simply gulp.
Disclaimer: This might not work with the gulp watch, but there might be people with the answer to that.
There is also a line PHP server not started. Retrying... before the [Laravel Eixir BrowserSync] Proxying: http://localhost:8000. It belongs to gulp-connect-php when trying to check if the server is running.
I found other solution on laracast for Bloomanity and it works with me like a charm!
$ php artisan serve --host=0
and then in your gulp add:
mix.browserSync({proxy: 'localhost:8000'});

Gulp PHP with BrowserSync and a proxy server throwing error

I had to redownload all of my dev tools after upgrading/reset on Windows 10. I can't seem to get my server working with gulp-connect-php, gulp-browser-sync, and http-proxy. The task (below) is actually from this answer: Gulp-webapp running BrowserSync and PHP.
This is on top of a generation of gulp-webapp from Yeoman. The regular "serve" works, but of course doesn't spin up PHP files. I had it working before the upgrade, but now the http-proxy errors out with an ECONNREFUSED when I try to bring up the URL. Note that it doesn't error out until I actually go to the URL, if the task is setup to not open the browser automatically, it won't error until I go to localhost:3000.
Any ideas the cause of this error?
Gulp Task
gulp.task('php-serve', ['styles', 'fonts'], () => {
phpConnect.server({
port: 9001,
base: 'app',
open: false
});
const proxy = httpProxy.createProxyServer({});
browserSync({
notify: false,
open: false,
port: 9000,
server: {
baseDir: ['.tmp', 'app'],
routes: {
'/bower_components': 'bower_components'
},
middleware: function (req, res, next) {
var url = req.url;
if (!url.match(/^\/(styles|fonts|bower_components)\//)) {
proxy.web(req, res, { target: 'http://127.0.0.1:9001' });
}
else {
next();
}
}
}
});
gulp.watch([
'app/*.html',
'app/*.php',
'app/scripts/**/*.js',
'app/images/**/*',
'.tmp/fonts/**/*'
]).on('change', reload);
gulp.watch('app/styles/**/*.scss', ['styles']);
gulp.watch('app/fonts/**/*', ['fonts']);
gulp.watch('bower.json', ['wiredep', 'fonts']);
});
Gulp Run Task with Error
$ gulp php-serve
[07:30:09] Requiring external module babel-core/register
[07:30:11] Using gulpfile ~\...\gulpfile.babel.js
[07:30:11] Starting 'styles'...
[07:30:12] Starting 'fonts'...
[07:30:12] Finished 'styles' after 1.08 s
[07:30:12] Finished 'fonts' after 306 ms
[07:30:12] Starting 'php-serve'...
[07:30:12] Finished 'php-serve' after 183 ms
[BS] Access URLs:
----------------------------------
Local: http://localhost:9000
External: http://x.x.x.x:9000
----------------------------------
UI: http://localhost:3001
UI External: http://x.x.x.x:3001
----------------------------------
[BS] Serving files from: .tmp
[BS] Serving files from: app
C:\...\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
I think I know the reason.
Check or you have php installed on your machine and added to your path variables.
open your command line and type:
$ php --version
If it returns:
sh.exe": php: command not found
It means it is not installed or not in the path variable.
You can install php by simply installing WAMP/XAMP or MAMP
https://www.apachefriends.org/index.html
To make sure it is in your path I like to use patheditor
https://patheditor2.codeplex.com/
There go through these simple steps
1) open Path Editor
2) click 'add' in the 'user' section
3) search the folder where php.exe is. (usually something like: C:/xampp/php)
4) click 'ok' and 'ok' again to close path editor
5) reopen your commandline tool and you will see that $ php --version will find the php.exe
Try to run your gulp task again. It should work!
There was a bad install of http-proxy and/or gulp-php-connect. I uninstalled them, cleared npm cache, and did a fresh install. Everything worked fine after that.

Laravel Elixr copy method not working

I am trying to copy some js files, using elixr, from my vendor/bower_components folder, but when running gulp they don't copy across.
elixir(function(mix) { mix.less('app.less')
.copy('underscore/underscore.js', 'public/js/vendor/underscore.js'); });
No error is given by gulp.
[14:12:58] Using gulpfile ~/Public/balloonprinting_l5/gulpfile.js
[14:12:58] Starting 'default'...
[14:12:58] Starting 'less'...
[14:12:58] Running Less: resources/assets/less/app.less
[14:12:59] Finished 'default' after 506 ms
[14:13:00] gulp-notify: [Laravel Elixir] Less Compiled!
[14:13:00] Finished 'less' after 1.74 s
[14:13:00] Starting 'copy'...
[14:13:00] Finished 'copy' after 2.86 ms
You need to specify full source path:
elixir(function(mix) { mix.less('app.less')
.copy(
'bower_components/underscore/underscore.js',
'public/js/vendor/underscore.js'
);
});
As you can see I added bower_components/ to source path, this is default bower packages folder.

Categories