Any Way to Access Node.js Console?

I’m making a project in Node.js and I’d like a way to manually poke behind the scenes and execute commands for testing. I noticed that there is a console in Glitch, but it appears to be for something different since it wouldn’t accept Javascript commands and didn’t work with process.openStdin().

Is there any way to access a javascript console through Glitch?

The console starts with a linux bash shell, with the $ prompt. You can run node in there by typing node and any parameters you want, then it will give you the node > prompt.

$ node app.js
events.js:183
  throw er; // Unhandled 'error' event
  ^

Error: listen EADDRINUSE :::3000
  at Server.setupListenHandle [as _listen2] (net.js:1360:14)
  at listenInCluster (net.js:1401:12)
  at Server.listen (net.js:1485:7)
  at Function.listen (/rbd/pnpm-volume/40b69335-86e4-4e74-a149-2557639749bc/node_modules/.registry.npmjs.org/express/4.17.1/node_modules/express/lib/application.js:618:24)
  at Object.<anonymous> (/app/app.js:122:5)
  at Module._compile (module.js:653:30)
  at Object.Module._extensions..js (module.js:664:10)
  at Module.load (module.js:566:32)
  at tryModuleLoad (module.js:506:12)
  at Function.Module._load (module.js:498:3)

Seems like that doesn’t work because the app is already running (EADDRINUSE)

The node prompt didn’t show up because you told node to run a program instead of being interactive.

You can only have one app at a time listen to the same port.

Try either comment out the listen code, or edit package.json to not run the server app automatically.

Well I’m running a bot, with the idea that it is running 24/7. However, the idea is that if need be I can tinker from the back end without having to restart it. I’m sorry if that wasn’t clear.

You can run it as a webserver and tinker at the same time, here is one way …

1 . prevent it automatically running as an unattended server

Comment the execution of it in package.json

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

2 . Import the server code as a module when running from the console

This would be more useful if in the server.js you export some of the objects you want to tinker with, for example app

server.js

module.exports.app = app

console

$ node
> const s = require('./server.js')
undefined
> Your app is listening on port 3000

> s.app.route
[Function: route]
>

The server would only be active while you have the app running.

1 Like

Unfortunately, neither of those options work for me due to what I said previously. I guess I’m going to have to do it through a third party, i.e. Discord PMs from a specific user leading to eval().

Another way might be to conditionally redirect requests through an interactive proxy, using an internal port. It would require signalling the server script to let it know when to use the proxy, and a decently short timeout if the proxy falls over.