This article is about fixing Module not found Error Can't resolve 'fs' in motdotla Dotenv
  • 22-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing Module not found Error Can't resolve 'fs' in motdotla Dotenv

Module not found: Error: Can’t resolve ‘fs’ in motdotla Dotenv

Lightrun Team
Lightrun Team
22-Jan-2023

Explanation of the problem

The application was functioning properly until the start of the new year (2022), at which point Webpack began to fail and random errors began to appear. Despite attempts to fix the errors, more continue to occur. The following errors are present in the application’s terminal:

`Failed to compile.

Module not found: Error: Can’t resolve ‘fs’ in ‘/Users/shansiddiqui/Desktop/dash/node_modules/dotenv/lib’

ERROR in ./node_modules/dotenv/lib/main.js 24:11-24 Module not found: Error: Can’t resolve ‘fs’ in ‘/Users/shansiddiqui/Desktop/dash/node_modules/dotenv/lib’

ERROR in ./node_modules/dotenv/lib/main.js 26:13-28 Module not found: Error: Can’t resolve ‘path’ in ‘/Users/shansiddiqui/Desktop/dash/node_modules/dotenv/lib’

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to: – add a fallback ‘resolve.fallback: { “path”: require.resolve(“path-browserify”) }’ – install ‘path-browserify’ If you don’t want to include a polyfill, you can use an empty module like this: resolve.fallback: { “path”: false }

ERROR in ./node_modules/dotenv/lib/main.js 28:11-24 Module not found: Error…

It appears that the application is encountering issues with missing modules ‘fs’ and ‘path’ in the ‘dotenv/lib’ directory, and may require the addition of polyfills or the installation of ‘path-browserify’. The user is seeking assistance in resolving these errors.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Module not found: Error: Can’t resolve ‘fs’ in motdotla Dotenv

The original problem is related to the use of the dotenv package in a React application and the presence of missing modules ‘fs’, ‘path’, and ‘os’ in the ‘dotenv/lib’ directory. This package is used to manage environment variables and can be used to configure different settings for different environments.

When missing modules are present in the ‘dotenv/lib’ directory, it means that the application is unable to locate the required dependencies. This can happen if the modules are not installed or if they are installed in the wrong directory. In this case, the error message is pointing towards the ‘dotenv/lib’ directory, indicating that the missing modules are related to the dotenv package.

One solution proposed is to remove the require(‘dotenv’).config() call from the code and accessing environment variables without it, as react-scripts already includes it.

// remove this
require('dotenv').config()

// and use this
console.log(process.env.API_KEY)

Another solution would be to ensure that the missing modules are installed and configured properly. If you are using npm, you can install these modules by running the following command:

npm install fs path os

and then adding the following lines of code to the config file of your application:

const fs = require('fs');
const path = require('path');
const os = require('os');

In this way, the application will be able to locate the required dependencies and the error will be resolved.

Other popular problems with Dotenv

Problem: Missing modules error when using Dotenv

When using the Dotenv package to manage environment variables, the application may encounter an error related to missing modules in the ‘dotenv/lib’ directory. This can happen if the required modules are not installed or if they are installed in the wrong directory.

Solution:

To resolve this issue, the missing modules need to be installed and configured properly. If you are using npm, you can install the missing modules by running the following command:

npm install fs path os

Then, you will need to include them in your config file of your application:

const fs = require('fs');
const path = require('path');
const os = require('os');

By doing so, the application will be able to locate the required dependencies, and the error will be resolved.

Problem: Error when using dotenv.config() with Create React App

When using the Dotenv package with Create React App, the application may encounter an error when using the dotenv.config() method. This is because Create React App already includes the Dotenv package and this method is not needed.

Solution:

To resolve this issue, you will need to remove the dotenv.config() method from your code.

// remove this
require('dotenv').config()

// and use this
console.log(process.env.API_KEY)

This will ensure that the application is able to access the environment variables without any issues.

Problem: Error when loading .env file

When using the Dotenv package, the application may encounter an error when trying to load the .env file. This can happen if the path to the file is incorrect or if the file itself is not in the correct format.

Solution:

To resolve this issue, you will need to ensure that the path to the .env file is correct and that the file is in the correct format.

// correct path
require('dotenv').config({path: '/path/to/.env'});

// correct format
API_KEY=abcdefg

You can also use dotenv.config({path: __dirname + '/.env'}); instead of hardcoding the path. This will ensure that the application is able to load the .env file correctly and access the environment variables without any issues.

A brief introduction to Dotenv

Dotenv is a popular package used to manage environment variables in JavaScript applications. It allows developers to store configuration settings in a separate file, such as a .env file, which is not committed to version control. This makes it easy to switch between different configurations for different environments, such as development, staging, and production.

The package can be easily integrated into a JavaScript application by including it in the dependencies and using the dotenv.config() method to load the .env file. This method takes an optional argument for the path of the .env file, which defaults to the root of the project. Once the .env file is loaded, the variables can be accessed through the process.env object, for example, process.env.API_KEY. This package is a simple and effective way to manage environment variables, and it is widely used in many JavaScript projects.

Most popular use cases for Dotenv

  1. Storing and managing environment-specific configuration settings: Dotenv allows developers to store configuration settings in a separate .env file, which is not committed to version control. This makes it easy to switch between different configurations for different environments such as development, staging and production.
  2. Protecting sensitive information: Dotenv can be used to store sensitive information such as API keys, passwords, and other confidential data. This information can be accessed in the application through the process.env object, making it easy to use in the code.

const apiKey = process.env.API_KEY;

  1. Automating configuration for different environments: Dotenv allows developers to automate the configuration of different environments. For example, the database connection string can be set based on the environment, and the application can automatically connect to the appropriate database. This can be achieved by creating different .env files and loading the appropriate file based on the environment.
if (process.env.NODE_ENV === 'production') {
  dotenv.config({path: __dirname + '/.env.production'});
} else {
  dotenv.config({path: __dirname + '/.env.development'});
}
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.