Search code examples
node.jsreactjsnpmnext.jsdotenv

Cannot find module 'dotenv/types'


Im using next.js and trying to set an environment variable - I have installed dotenv ofcourse by im being told that the module cannot be found and have no earthly idea as to why. Why might something like this be happening?

The dotenv is in my dependencies, But when i run dotenv --version i get told 'bash command not found'

I should say that the below code is probably not relevant to the problem.

in my .env file at the root of the doc i have

API_URL = http://localhost:1337

Then in my config file i have

const path = require('path');
require('dotenv').config();

module.exports = {
  env: {
    API_URL: process.env.API_URL,
  },
  webpack: (config) => {
    config.resolve.alias['components'] = path.join(_dirname, 'components');
    config.resolve.alias['public'] = path.join(_dirname, 'public');

    return config;
  },
};

Im getting this on the the console:

 Loaded env from C:\Users\sidtu\Documents\Websites\next-example\.env
 Cannot find module 'dotenv/types'
 code: 'MODULE_NOT_FOUND',

Solution

  • "The dotenv is in my dependencies, But when i run dotenv --version i get told 'bash command not found'" - That is typical behaviour for a npm package, only packages that are installed globally should be accessible from the command line.

    If you are just using dotenv in weback, use dotenv-webpack instead.

    If you are having trouble getting .env files to work with React see this blog