How to Get It!
📦 NPM Package: detect-is-on-glitch
💾 Source Code: GitHub
The package is lightweight; works fast even via
npx detect-is-on-glitch
.
Intro
What is Glitch?
For those not aware, Glitch is an online code editor, with a focus on collaboration, sharing, and live editing. You can edit and run static websites, as well as full-stack projects that depend on build tools, NodeJS scripts, and other bits and pieces.
What is this tool?
detect-is-on-glitch
is exactly what it sounds like; a tool to detect whether or not your code is running on Glitch, as opposed to your local PC, build server, or anywhere else.
The idea for this is that you might want your app to behave differently on Glitch versus anywhere else. Maybe you want to serve a slimmed down version of your app for Glitch’s live hosting, or display special console instructions to those remixing your project versus regular users. This tool can let you do that.
How Does It Work
This will probably the least complex NodeJS package I ever create. You can check out the source code yourself, but to sum up; it checks process.env
and the hosted domain for any traces that Glitch normally leaves.
Right now, this works for all the samples I have tried, but might need to be updated in the future if Glitch introduces new changes.
You can call it via CLI (alias is isglitch
), NodeJS, or even front-end code.
Tools Used
- NodeJS
- JavaScript
- TypeScript
- Glitch
- 🧠–> Research, to find what Glitch exposed as unique properties
Sample
If you are still trying to think of how this could be used, here is a quick sample: running a different “start” command on Glitch versus anywhere else
When Glitch detects a code change, or your project has “gone to sleep” due to inactivity, it will run your start
script. You can read more about Glitch’s deterministic task setup in my Glitch cheatsheet.
Using this tool, you can have a different command execute (node server-light.js
) for Glitch
package.json
:
{
"scripts": {
"start": "npx detect-is-on-glitch && node server-light.js || node sever.js"
}
}