Can I detect if I'm running in local environment?

I’d like to be able to detect if I’m running in local dev environment (fdk run) so I can write some log messages only locally but not write those messages if deployed to the server.

For example, I have some log timing messages.

I push the logs thru a single “log()” function so it would be pretty easy to add a check in that location and defer the log message.

So for example, I have this:

var log=function() {
       console.log.apply(console, arguments)
}
var logTime=function() {
       not_local = <ENVIRONMENT IS NOT LOCAL>
       if ( not_local ) return()
	var args = Array.prototype.slice.call(arguments);
	console.log("API Time (%s) %s", args[1], args[0])
}

I’m looking for the code to put in

if not a standard way to do this, could I possibly check the URL of the webhook? If “localhost”, then its local. right?

1 Like

Hey Steve

You can check if the environment is local by writing a method similar to this

For app.js

const isLocal = () => {
  const params = new URL(window.location.href).searchParams;
  return params.get('dev') === 'true';
};

For server.js

const isLocal = () => {
  // a work around
  return process.cwd().includes('/path/to/your/app');
}

hope this helps!

3 Likes

Thanks! This is a great solution.

1 Like

@shravan.balasubraman This is a great solution but it won’t work for Custom Apps that are deployed to test: they also use ?dev=true query param.

Is there a way to tell explicitly that we are inside an application that is running locally using fdk run?

The problem I want to fix with this is that app.activated lifecycle method is not called after hot reload of local app. I don’t want to reload whole page manually after every change made to codebase and lose ability to have a working hot reload on local env. Any suggestions here?