Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird variable initialization issue #45961

Closed
yunylz opened this issue Dec 24, 2022 · 2 comments
Closed

Weird variable initialization issue #45961

yunylz opened this issue Dec 24, 2022 · 2 comments

Comments

@yunylz
Copy link

yunylz commented Dec 24, 2022

Version

v16.18.1

Platform

Linux vmi700946 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

module.exports = () => {

    let envConfig = require("dotenv").config().parsed || {};
    let localConfig = require("../config");

    let config = {
        ...localConfig,
        ...envConfig
    }
    
    // Check for required keys
    ["ENV", "ENVS"].forEach(k => {
        if (!config[k]) throw new Error(`${k} is missing from config and it is required.`)
    });

    // Check if enviroment is a valid one
    if (!config.ENVS.includes(config.ENV)) 
        throw new Error(`${config.ENV} is not an allowed enviroment.\nEnv must be one of these: ${config.ENVS.join(", ")}`)

    global.config = config;
}

On "Check for required keys" part, when the array is set straight on forEach, it will throw Cannot access 'config' before initialization in the loop.

If the array is set as a variable like this, it runs completely fine.

let arr = ["ENV", "ENVS"]
arr.forEach(k => {
        if (!config[k]) throw new Error(`${k} is missing from config and it is required.`)
});

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

No response

What do you see instead?

image

Additional information

No response

@mscdex
Copy link
Contributor

mscdex commented Dec 24, 2022

Try adding a semicolon after config's declaration/assignment.

@aduh95
Copy link
Contributor

aduh95 commented Dec 24, 2022

You need a semicolon between your assignment of config and the array just after it – as is it’s interpreted as one instruction.

@bnoordhuis bnoordhuis closed this as not planned Won't fix, can't repro, duplicate, stale Dec 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants