Why is process.env undefined in module?

11,697

Solution 1

I'm idiot. I named function process, which is the name of system variable :D

Sorry for bothering you guys, thanks for help.

Solution 2

Add require('dotenv').config() in global. Also make sure .env file is in root directory of the project. I have created a sample on github https://github.com/GMaker01/basic-dotenv-example

For reference, you can look into the dotenv documentation

Share:
11,697
oLDo
Author by

oLDo

Updated on June 25, 2022

Comments

  • oLDo
    oLDo almost 2 years

    I'm beginner with nodejs and I want environment variables shared through modules. I read those variables with dotenv package. But in next required module process.env is undefined.

    app.js

    console.log(require('dotenv').config())
    console.log(process.env.NODE_ENV);
    require('./task')
    

    task.js

    console.log(process.env);
    console.log(process.env.NODE_ENV);
    

    .env

    NODE_ENV=development
    PORT=8080
    

    console log

    { parsed: { NODE_ENV: 'development', PORT: '8080' } }
    development
    undefined
    E:\msf\nodejs_prj\compositor\task.js:2
    console.log(process.env.NODE_ENV);
                            ^
    TypeError: Cannot read property 'NODE_ENV' of undefined
        at Object.<anonymous> ...
    

    I created new clean project with provided code and it works also for me. That means it's related to something else. This node.js is weard about errors.

    This is my whole code from task.js

    const fs = require('fs')
    const path = require('path')
    const decompress = require('decompress')
    
    const dir = './upload'
    
    console.log(process, process.env)
    
    function process() {
        console.log('cron - process data');
    
        fs.readdir(dir, (err, files) => {
            if (err) return
    
            files.forEach(file => {
                if (path.extname(file) != '.zip') return
    
                let target = path.join(dir, path.basename(file).replace(path.extname(file), ''))
                unlinkDirSync(target)
    
                decompress(path.join(dir, file), target).then(files => {
                    console.log(files);
                    console.log('done!');
    
                    //todo process unzipped files
    
                    //todo delete unzipped directory and zip file
                })
            })
        })
    }
    
    function unlinkDirSync(dir_path) {
        if (fs.existsSync(dir_path)) {
            fs.readdirSync(dir_path).forEach(function (entry) {
                var entry_path = path.join(dir_path, entry);
                if (fs.lstatSync(entry_path).isDirectory()) {
                    unlinkDirSync(entry_path);
                } else {
                    fs.unlinkSync(entry_path);
                }
            });
            fs.rmdirSync(dir_path);
        }
    }
    
    if (process.env === undefined || process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'production') {
        console.log('starting on production')
        setInterval(process, 1000 * 60)
    } else {
        console.log('starting on development')
        setTimeout(process, 1000)
    }
    

    If I comment out the rest after console.log it works.