question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dotenv module doesn't work with cypress

See original GitHub issue
  • Operating System: Windows 10
  • Cypress Version: 1.4.2
  • Browser Version: Chrome 63

Bug

Current behavior:

https://www.npmjs.com/package/dotenv In order to use this module to have to require(‘dotenv’). If I do this anywhere in code it fails:

./node_modules/dotenv/lib/main.js
Module not found: Error: Can't resolve 'fs' in '<project dir>\node_modules\dotenv\lib'
resolve 'fs' in '<project dir>\node_modules\dotenv\lib'
Parsed request is a module
using description file: <project dir>\node_modules\dotenv\package.json (relative path: ./lib)
Field 'browser' doesn't contain a valid alias configuration
after using description file: <project dir>\node_modules\dotenv\package.json (relative path: ./lib)
resolve as module
<project dir>\node_modules\dotenv\lib\node_modules doesn't exist or is not a directory
<project dir>\node_modules\dotenv\node_modules doesn't exist or is not a directory
<project dir>\node_modules\node_modules doesn't exist or is not a directory
<project dir>\node_modules doesn't exist or is not a directory
<user dir>repo\node_modules doesn't exist or is not a directory
<user dir>repo\node_modules doesn't exist or is not a directory
C:\Users\node_modules doesn't exist or is not a directory
C:\node_modules doesn't exist or is not a directory
looking for modules in <project dir>\node_modules
using description file: <project dir>\package.json (relative path: ./node_modules)
Field 'browser' doesn't contain a valid alias configuration
after using description file: <project dir>\package.json (relative path: ./node_modules)
using description file: <project dir>\package.json (relative path: ./node_modules/fs)
no extension
Field 'browser' doesn't contain a valid alias configuration
<project dir>\node_modules\fs doesn't exist
  .ts
Field 'browser' doesn't contain a valid alias configuration
<project dir>\node_modules\fs.ts doesn't exist
  .js
Field 'browser' doesn't contain a valid alias configuration
<project dir>\node_modules\fs.js doesn't exist
as directory
<project dir>\node_modules\fs doesn't exist
looking for modules in <user dir>node_modules
No description file found
Field 'browser' doesn't contain a valid alias configuration
No description file found
no extension
Field 'browser' doesn't contain a valid alias configuration
<user dir>node_modules\fs doesn't exist
  .ts
Field 'browser' doesn't contain a valid alias configuration
<user dir>node_modules\fs.ts doesn't exist
  .js
Field 'browser' doesn't contain a valid alias configuration
<user dir>node_modules\fs.js doesn't exist
as directory
<user dir>node_modules\fs doesn't exist
  [<project dir>\node_modules\dotenv\lib\node_modules]
[<project dir>\node_modules\dotenv\node_modules]
[<project dir>\node_modules\node_modules]
[<project dir>\node_modules]
[<user dir>repo\node_modules]
[<user dir>repos\node_modules]
[C:\Users\node_modules]
[C:\node_modules]
[<user dir>node_modules\package.json]
[<project dir>\node_modules\fs]
[<project dir>\node_modules\fs.ts]
[<user dir>node_modules\fs\package.json]
[<project dir>\node_modules\fs.js]
[<user dir>node_modules\fs]
[<project dir>\node_modules\fs]
[<user dir>node_modules\fs.ts]
[<user dir>node_modules\fs.js]
[<user dir>node_modules\fs]
@ ./node_modules/dotenv/lib/main.js 3:11-24
@ ./dist/specs/menu.spec.js

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

31reactions
alex-popov-techcommented, Aug 18, 2020

for someone who will be looking for working examples:

// cypress/plugins/index.js
module.exports = (on, config) => {
  const configWithDotenv = require('dotenv').config();
  if (configWithDotenv.error) {
    throw configWithDotenv.error;
  }
  const env = { ...config.env, ...configWithDotenv.parsed };
  const result = { ...config, env };
  return result;
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
}

// cypress/integration/example.spec.js
...js
Cypress.env('ENV_NAME')
...
4reactions
brian-manncommented, Feb 21, 2018

You cannot require npm modules which use native node libs like fs. Cypress serves your JS spec files to the browser - the usage of fs makes no sense there.

If you want to read in config files and then set env vars into Cypress you can use the Configuration Plugin API to do just that.

Files you write in the plugins folder are evaluated by node, not the browser - which is just the place to use them.

https://docs.cypress.io/api/plugins/configuration-api.html#

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress can't get variables from process.env file
I've got a problem with getting variables value from process.env file in my cypress test. I try to do it according to this: ......
Read more >
Environment Variables | Cypress Documentation
Difference between OS-level and Cypress environment variables In Cypress, "environment variables" are variables that are accessible via Cypress.env.
Read more >
Cypress v10 Environment Variables - Gleb Bahmutov
module.exports = defineConfig({ // our project does not use fixture files fixturesFolder: false, env: { // user values block 1 level: 1,
Read more >
Env Variables and Modes - Vite
For example, dynamic key access like import.meta.env[key] will not work. It will also replace these strings appearing in JavaScript strings and Vue templates....
Read more >
types/cypress-dotenv - NPM Package Overview - Socket.dev
Start using Socket to analyze @types/cypress-dotenv and its 1 dependencies to secure your ... Problems with the package may go unaddressed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found