Can I run PHP on Glitch?

Is there a way I can render if I add an index.php

We have a couple of experimental projects which do work and allow you to use PHP, but it’s unsupported for now: https://glitch.com/edit/#!/lamp-poc and https://glitch.com/edit/#!/php-poc

2 Likes

I’m new on glitch. I’ve used php for years. Now that glitch.com doesn’t support php I tried learning Nodejs . But W3C thoroughly confused me! When I access the nodejs file which is scripts.js, the server doesn’t render it at all. Please help

Please share your project name so we can take a look.

https://teresa.glitch.me/


I am not much familiar with node JS and quite honestly, a bit confused about what nodeJS exactly. A Server side scripting language or a server in itself.

Hi @jeet-patil :slight_smile:

I would say that Node is a server-side scripting language, but it depends on what you mean with “scripting language”. Perhaps it is less ambiguous to say the Node is a server-side runtime that executes code written in JavaScript. Looking at your project, for example, the content of script.js seem to be written for Node:

var http=require('http');
http.createServer(function server(req,res){
  res.write("This is teresa information charlie");
  res.end();
}).listen(80);

So this script creates an http server that listens on port 80. Now let’s understand why it is not working for you:

  1. Your project does not have a package.json. Glitch checks some key files in your repository to understand which kind of project you want to start. If it finds a package.json file, Glitch thinks you are writing a Node application, otherwise it just starts a static web server. The difference is of course that with a static web server you can’t have any script running on the server-side. So if you create a new project of type “Node app” you can copy package.json from there and bring it in your project, as a starting point. Glitch will turn your app into a Node app.

  2. You instructed the http server in script.js to start listening on port 80, which is not available. You should use process.env.PORT instead.

  3. The http server you created always respond the same to any http request you send at him. You probably want something more complex. Again, have a look at the code of the new project of type Node app for more information, or look up a tutorial about express or Node http servers in general. Or ask again here :slight_smile:

Having that said, if you want to realize a website and not a web application, that is, if you don’t need to have any server-side code running, you are better off with not using package.json. In this case, all the files in your project will only be executed on the client-side, the browser.

Thanks a lot. I tried the same. But my website immediately stopped working!
logs:
node v6.13.1, with pnpm

Installing…

Previous installation was done with node v9, recreating node_modules

Packages: +49

+++++++++++++++++++++++++++++++++++++++++++++++++

dependencies:

  • express 4.16.3

Total install time: 9794ms

node v9.8.0, with pnpm

Installing…

Previous installation was done with node v6, recreating node_modules

Packages: +49

+++++++++++++++++++++++++++++++++++++++++++++++++

dependencies:

  • express 4.16.3

Total install time: 10900ms

/etc/app-types/node/start.sh: line 33: file.js: command not found

Here is the package.json:
{
“//1”: “describes your app and its dependencies”,
“//2”: “https://docs.npmjs.com/files/package.json”,
“//3”: “updating this file will download and update your packages”,

“name”: “my-glitch-app”,
“version”: “0.0.1”,
“description”: “What am I about?”,
“scripts”: {
“start”: “file.js”
},
“dependencies”: {
“express”: “^4.16.3”
},
“engines”: {
“node”: “9.x”
},
“repository”: {
“url”: “url to my app”
},
“license”: “MIT”,
“keywords”: [
“node”,
“glitch”,
“express”
]
}

You’ve to set a valid start script, right now it is file.js, it should be node file.js or something like that.

I try once again. Thanks

Hey @jeet-patil!

If @etamponi’s solution doesn’t fix the error(s), make sure you named your file correctly. If the file name in your package.json file isn’t the same as your actual file name then it will give an error saying it can’t find it.

As for the file @Crazy , I’ve named it correctly

Now I get this error:
node v9.8.0, with pnpm

Installing…

Total install time: 5835ms

module.js:545

throw err;

^

Error: Cannot find module ‘/app/file.js’

at Function.Module._resolveFilename (module.js:543:15)

at Function.Module._load (module.js:470:25)

at Function.Module.runMain (module.js:690:10)

at startup (bootstrap_node.js:194:16)

at bootstrap_node.js:666:3

module.js:545

throw err;

^

Error: Cannot find module ‘/app/file.js’

at Function.Module._resolveFilename (module.js:543:15)

at Function.Module._load (module.js:470:25)

at Function.Module.runMain (module.js:690:10)

at startup (bootstrap_node.js:194:16)

at bootstrap_node.js:666:3

I’m sorry but what should this node file.js contain?

This error appears when your main file isn’t named the same in your package.json file. Your package file should look like this (let’s assume your main file’s named server.js):

"main": "server.js",
"scripts": {
  "start": "node server.js"
}

I see @Crazy, but should this file contain some specific code?

Not right away, no. It shouldn’t give any errors if empty and the package.json file is looking for the correct file.

I get it. But no success so far. I’m trying…

Mind sending the project link? I can take a look at it.

sure.