Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export constants defined using object destructuring

Guided by eslint's prefer-destructuring rule, I defined some constants like this:

const {
    NODE_ENV,
    API_URL,
} = process.env;

Is it possible to export these constants by prefixing the statement by export?

export const {
    NODE_ENV,
    API_URL,
} = process.env;

This would seem natural, but eslint-plugin-import complains about a violation of the import/named rule: API_URL not found in '../constants'. In fact, this usage of export is also not described on the relevant MDN page.

Do we then have to repeat all constants in a separate export statement?

const {
    NODE_ENV,
    API_URL,
} = process.env;

export {
    NODE_ENV,
    API_URL,
};
like image 498
Claudio Avatar asked Sep 06 '25 19:09

Claudio


1 Answers

Is it possible to export these constants by prefixing the statement by export?

export const {
    NODE_ENV,
    API_URL,
} = process.env;

Yes, this is totally valid according to the spec. You can use destructuring patterns in the declarations of exported consts.

This would seem natural, but eslint-plugin-import complains about a violation of the import/named rule: API_URL not found in '../constants'.

Sounds like that plugin is broken. In fact, your exact use case was reported as working before.

like image 84
Bergi Avatar answered Sep 08 '25 08:09

Bergi