Develop Your App Locally
Learning Objectives
After completing this unit, you’ll be able to:
- Establish a local environment mirroring Heroku’s.
- Use Heroku Local to run an app locally.
- Implement effective strategies for testing and debugging.
Set Up Your Local Environment
After addressing some issues, Wei realizes that her Ursa Major team’s local development environment doesn’t match the Heroku environment. To streamline her development process, she ensures that her local environment closely mirrors the production environment. She also ensures her fellow developers have a matching environment.
Local Server Management
It’s common practice to manage a local server environment for development and testing purposes. Tools like Heroku Local make it easy.
Heroku Local is part of the Heroku CLI (command line interface) and allows you to run applications locally on your own machine. This tool simulates the Heroku runtime. You can test and tweak your applications in an environment similar to Heroku’s servers. It helps with identifying and fixing bugs. You can also test new features to ensure the application runs smoothly before deploying it.
The command heroku local web
runs the app locally on port 5001. You can see the running app by visiting http://localhost:5001 in your browser.
When running your app on Heroku, you use a set of config vars to capture the app’s configuration. Those values are stored on Heroku. When you run your app locally, you need a way to pass environment variables to your local app.
Use the .env
file for the config vars needed to run your app locally. When you start your app using any of the heroku local
commands, the tool reads the .env
file and inserts each name/value pair into the environment. Make sure that you add .env
to your .gitignore
file to avoid committing your environment variables to your Git repo.
If your application connects to databases or other services, set up local instances of these services. This process can involve running a local database server or using a Docker container to simulate a more complex service. The goal is to mimic the production setup as closely as possible.
Test and Debug
To debug a Node.js app, use the built-in debugger by adding the --inspect
flag to your Procfile’s start command. This flag enables the debugging mode, which has real-time debugging, allowing you to closely monitor and modify the behavior of your app as it runs. After debugging locally, remove the --inspect
flag from the Procfile to avoid pushing this change to Heroku in future deployments.
Use automated tests such as unit tests and integration tests as part of the development process. Node offers a native tool called TestRunner to create Javascript tests. You can also use testing libraries like Mocha, Jest, AVA, or other libraries to write unit tests. Run these tests locally to ensure that individual parts of the application, like functions, classes, or modules, work as intended.
Wei is learning the essential steps for testing and debugging a Heroku app locally, feeling empowered to ensure its reliability and performance.
Now that you learned how Wei tackles working with the app locally, read on to see how she faces the task of making her production app scale.