ReferenceError 'process not defined' when executing Cypress test

Viewed 6710

Trying to execute any of the tests leads to this error popping up.

I am using Cypress 6.5.0

Really clueless about what to do.

Sorry for the image, but it was much better to show it this way.

enter image description here

And also the StackTrace.

at Object../node_modules/is-ci/node_modules/ci-info/index.js (webpack:///node_modules/is-ci/node_modules/ci-info/index.js:5:1)
at __webpack_require__ (webpack:///webpack/bootstrap:19:1)
at Object../node_modules/is-ci/index.js (webpack:///node_modules/is-ci/index.js:3:18)
at __webpack_require__ (webpack:///webpack/bootstrap:19:1)
at Object.eval (webpack:///node_modules/cypress/lib/util.js:21:14)
at Object../node_modules/cypress/lib/util.js (http://localhost:37869/__cypress/tests?p=test/e2e/support/index.js:87250:31)
at __webpack_require__ (webpack:///webpack/bootstrap:19:1)
at Object.eval (webpack:///node_modules/cypress/index.js:9:14)
at Object../node_modules/cypress/index.js (http://localhost:37869/__cypress/tests?p=test/e2e/support/index.js:82972:31)
at __webpack_require__ (webpack:///webpack/bootstrap:19:1)
From previous event:
at runScriptsFromUrls (http://localhost:37869/__cypress/runner/cypress_runner.js:177985:98)
at Object.runScripts (http://localhost:37869/__cypress/runner/cypress_runner.js:177999:11)
at $Cypress.onSpecWindow (http://localhost:37869/__cypress/runner/cypress_runner.js:167733:19)
7 Answers

This happened for me when I imported cypress within my test, removing that fixed the issue

Sometimes by mistake, Visual Studio Code will auto-import unnecessary libraries. You should delete them. For me it was

import { cli } from 'cypress';

It looks like you have something in /support/index.js (since it's mentioned in the stack trace) that should be in /plugins/index.js (since it has an invalid use of process which is only available in node.js).

Cypress runs both a browser process and a background node.js process.

/support/index.js is used to augment the browser process, and /plugins/index.js is used to augment the node.js process.

If you mix them up when adding a library, a task, or a file preprocessor you get an error of the type you show.

What's in /support/index.js?

If you just updated to react-scripts 5.x and facing this issue, there is currently a PR to fix it but a workaround to get your tests working ASAP is to update the env.js file in the react scripts node module as referenced in this PR.

NB: If you have a private artifact repository manager on jfrog it is advisable to push this fix to your registry and pull react scripts from there until the fix becomes publicly available.

TLDR; You just updated to react 5.x and your cypress tests show the error below;

Cypress process is not defined error

Solution File: node_modules/react-scripts/config/env.js

Change the stringified method to:

    // Stringify all values so we can feed into webpack DefinePlugin
    const stringified = {
    'process':{}, // This is the only line added to the previous method
    'process.env': Object.keys(raw).reduce((env, key) => {
      env[key] = JSON.stringify(raw[key]);
      return env;
    }, {}),
    };

When we drive out gloval variable from JSON visual studiao automatically Import import { cli } from 'cypress';

I commented following line from support/command.js and that reso I am using cypress 8.7

const { defineConfig } = require('cypress')

Remove import cypress from "cypress" from your POM fi

Related