Cypress Configuration File: Step-by-Step Setup
In this article
This guide walks you through configuring Cypress using its configuration file (cypress.config.js or cypress.config.ts). You'll learn how to set base URLs, timeouts, viewport sizes, environment variables, and more. By the end, you'll have a tailored Cypress setup that matches your project's needs.
What You'll Need
- A project with Cypress installed (npm install cypress --save-dev)
- Node.js (version 18 or higher recommended)
- A code editor (VS Code, WebStorm, etc.)
- Basic familiarity with JavaScript or TypeScript
Step-by-Step
- Locate or create the configuration file. After installing Cypress, a default
cypress.config.jsis created in your project root. If it's missing, create it manually. For TypeScript projects, usecypress.config.ts. - Open the file in your editor. The default content looks like this:
If you're using TypeScript, you'll seeconst { defineConfig } = require('cypress'); module.exports = defineConfig({ e2e: { setupNodeEvents(on, config) { // implement node event listeners here }, }, });import { defineConfig } from 'cypress';andexport default defineConfig({...}). - Set your base URL. Add
baseUrlinside thee2eblock. This saves you from typing the full URL in everycy.visit()call.e2e: { baseUrl: 'https://example.com', setupNodeEvents(on, config) { ... } } - Configure timeouts. Add
defaultCommandTimeout(default 4000 ms) andrequestTimeout(default 5000 ms) at the top level of the config object. These control how long Cypress waits for commands and HTTP requests.module.exports = defineConfig({ defaultCommandTimeout: 10000, requestTimeout: 10000, e2e: { ... } }); - Set the viewport size. Add
viewportWidthandviewportHeight(default 1000x660). This is useful for testing specific screen resolutions.viewportWidth: 1280, viewportHeight: 720, - Define environment variables. Use the
envkey to store values like API endpoints or test credentials. Access them in tests viaCypress.env('key').env: { apiUrl: 'https://api.example.com', adminUser: 'admin@example.com', } - Configure the test file pattern. By default, Cypress looks for spec files in
cypress/e2e. You can change this withspecPatterninside thee2eblock.e2e: { specPattern: 'cypress/tests/**/*.cy.{js,jsx,ts,tsx}', } - Add support for Node events (optional). The
setupNodeEventsfunction lets you tap into Cypress's lifecycle events, such ason('before:browser:launch')oron('task')for custom commands that run in Node.setupNodeEvents(on, config) { on('task', { log(message) { console.log(message); return null; }, }); } - Save and restart Cypress. After editing the config, close Cypress (if running) and start it again with
npx cypress openornpx cypress run. The new settings take effect immediately.
Common Mistakes
- Placing options in the wrong block. Top-level options like
viewportWidthgo at the root, whilebaseUrlandspecPatterngo insidee2e(orcomponentfor component testing). - Forgetting to export the config. If you use
module.exportswithoutdefineConfig, Cypress may still work, but you lose type checking and autocomplete. Always wrap withdefineConfig. - Using single quotes inside the config when the file uses double quotes. This is a syntax error. Stick to one style consistently.
- Overwriting the default
setupNodeEventsfunction. If you add a second one, the first is ignored. Keep only one. - Using environment variables without the
envkey. Putting them at the root level won't make them accessible viaCypress.env().
Tips
- Use comments to document your config. Especially for timeouts and viewport sizes, future you will appreciate the context.
- Leverage environment-specific config files. For example, create
cypress.config.dev.jsandcypress.config.prod.jsand switch using the--config-fileflag. - Use the
--configCLI flag for quick overrides. For example,npx cypress run --config viewportWidth=800overrides the config file without editing it. - Take advantage of TypeScript types. If you use a
.tsconfig file, you get autocomplete for all options, reducing typos. - Keep sensitive data out of the config file. Use environment variables (like
process.env) or Cypress'senvoption with acypress.env.jsonfile that is gitignored.
Troubleshooting
- Cypress doesn't pick up my changes. Make sure you restart Cypress after editing the config. The config is read only when Cypress launches.
- Getting a syntax error. Check for missing commas, mismatched brackets, or a stray semicolon. Use a linter or run
node cypress.config.jsto see the exact error. - baseUrl not working. Verify you placed it inside the
e2eblock. Also check that your URL doesn't have a trailing slash unless you intend it. - Environment variables are undefined. Ensure they are under the
envkey, not at the root. Also check that you're usingCypress.env('key')correctly in your tests. - Spec files not found. Double-check the
specPatternpath. Use forward slashes and ensure the glob pattern matches your file extensions. - TypeScript config not recognized. Make sure your project has TypeScript installed and that the file is named
cypress.config.ts. Also check yourtsconfig.jsonincludes Cypress types.
Frequently Asked Questions
Where is the Cypress configuration file located?
By default, it's cypress.config.js (or .ts) in the root of your project. You can specify a different file using the --config-file flag.
Can I use environment variables in the config file?
Yes, you can access system environment variables via process.env inside the config file, and you can define Cypress-specific environment variables under the env key.
What's the difference between baseUrl and env?
baseUrl is used as the prefix for cy.visit() and cy.request() when you pass a relative path. The env object stores arbitrary key-value pairs that you access with Cypress.env().
How do I override the config for a single run?
Use the --config CLI option, e.g., npx cypress run --config defaultCommandTimeout=8000. This merges with the config file values.
Can I have multiple config files for different environments?
Yes, create separate files like cypress.config.staging.js and run with --config-file cypress.config.staging.js. You can also use a single file and conditionally set values based on process.env.NODE_ENV.
Why is my setupNodeEvents not being called?
Make sure you have only one setupNodeEvents function and that it's defined inside the e2e (or component) block. Also verify that you're not accidentally overwriting it in a plugin file (the old plugins/index.js approach is deprecated).