41

While loading the .env file to pass env values to the getToken.js script in the cypress root folder throws Cannot find module 'dotenv'error. I have installed npm install dotenv. Could someone please advise what I am missing here ? .env file is available in cypress root folder.

Environment : Windows 10 > git bash /command prompt

    const puppeteer = require("puppeteer");
    require('dotenv').config({path: '.env'})
    
    const baseURL = process.env.CYPRESS_BASE_URL
    const testsUser = process.env.CYPRESS_TESTS_USERNAME
puppeteer
  .launch({ headless: true, chromeWebSecurity: false, args: ['--no-sandbox'] })
  .then(async browser => {
    const page = await browser.newPage();
    await page.goto(`${baseURL}/login`);

    await page.waitFor(2000);
    await page.waitForSelector("input[name=username]");
    await page.type("input[name=username]", testsUser , {
      delay: 50
    });

    browser.close();
  });

package.json

"scripts": {
    "cy:run": "cypress run",
    "get-token-main": "node getToken.js && mv tokenData.json cypress/fixtures",
    "cy:open-qa": "npm run get-token-main && cypress open"
internal/modules/cjs/loader.js:797
    throw err;
    ^

Error: Cannot find module 'dotenv'
Require stack:
- /e2e/getToken.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
    at Function.Module._load (internal/modules/cjs/loader.js:687:27)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/e2e/getToken.js:3:16)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/e2e/getToken.js' ]
anutter
  • 366
  • 2
  • 17
soccerway
  • 10,371
  • 19
  • 67
  • 132

5 Answers5

84

You should check if dotenv is installed, otherwise type in terminal:

npm install --save dotenv 

or dotenv-extended

npm install --save dotenv-extended 
Layla Boyd
  • 150
  • 7
Diana
  • 1,091
  • 1
  • 9
  • 11
4

The reason to have that issue is that you have to add in your tsconfig.json under 'compilerOptions', the 'moduleResolution' value 'node'. With that, everything should work fine! :) In other words:

tsconfig.json:

{
  "compilerOptions": {
    "moduleResolution": "node",     // add this line.
   ...
  },
...
3

I was also facing the same problem when I delete the package-lock.json file and create a new one. To fix this run the following cmd:

 npm install dotenv

Try to install again even if you have to installed dotenv.

3

Just throwing out another answer here for those that might need it.

In my case, it was because the .env file wasn't in same folder as my entry point file.

My entry point was src/index.js, and my .env file was outside of src. Once I moved .env to the same directory, I stopped having this issue.

isaacsan 123
  • 1,045
  • 8
  • 11
0

I had the same error and in my case in order to deploy on https://www.cyclic.sh/ it looked like that:

"scripts": {
    "test": "mocha tests/unit/",
    "dev": "nodemon src/index.ts",
    "start": "node src/index.ts"
},

I correct the feature

"start": "node src/index.ts"  to "start": "nodemon src/index.ts"

and the problem was gone!

azizkale
  • 37
  • 2
  • 6
  • what you did is switch from `node` which is the runtime environment to `nodemon` which automatically schedules jobs to be run in `node` if files change. this has absolutely nothing to do with `dotenv` – intcreator May 28 '23 at 00:02