Home > Net >  How to export all constants at once?
How to export all constants at once?

Time:10-25

I have a file where I export constants.

export const PASSWORD_MISMATCH_ERROR = 'Passwords not match!';
export const USER_NOT_FOUND_ERROR = 'User not found';
export const WRONG_PASSWORD_ERROR = 'The password is incorrect';
export const UNAUTHORIZED = 'User unauthorized';

How do I make it so that I don't have to export each constant separately?

CodePudding user response:

The ECMAScript 262 specification allows this trivially. Even though I don't expect the module syntax introduced by Common-JS, to be ever removed from Node.js, new Node applications shouldn't use it as it's a remnant from the time before modules were standardized by ECMA, borne out of sheer necessity (which is understandable).

Below are some solutions with ECMAScript modules instead, something that your application evidently uses:

const PASSWORD_MISMATCH_ERROR = 'Passwords not match!';
const USER_NOT_FOUND_ERROR = 'User not found';
const WRONG_PASSWORD_ERROR = 'The password is incorrect';
const UNAUTHORIZED = 'User unauthorized';

export { PASSWORD_MISMATCH_ERROR, USER_NOT_FOUND_ERROR, WRONG_PASSWORD_ERROR, UNAUTHORIZED };

It may seem your problem is elegance of expression, so you can use one const keyword for multiple definitions:

const
    PASSWORD_MISMATCH_ERROR = 'Passwords not match!',
    USER_NOT_FOUND_ERROR = 'User not found',
    WRONG_PASSWORD_ERROR = 'The password is incorrect',
    UNAUTHORIZED = 'User unauthorized';

But then you still end up repeating every identifier twice -- once during declaration in the module and the second time for exporting it.

Instead, you can define and export a dictionary, utilizing object destructuring on the importing side:

export const errors = {
    PASSWORD_MISMATCH_ERROR: 'Passwords not match!',
    USER_NOT_FOUND_ERROR: 'User not found',
    WRONG_PASSWORD_ERROR: 'The password is incorrect',
    UNAUTHORIZED: 'User unauthorized'
};
import { errors } from "the-above-module";
const { PASSWORD_MISMATCH_ERROR, USER_NOT_FOUND_ERROR, WRONG_PASSWORD_ERROR, UNAUTHORIZED } = errors;

CodePudding user response:

You can use module.exports

const PASSWORD_MISMATCH_ERROR = 'Passwords not match!';
const USER_NOT_FOUND_ERROR = 'User not found';
const WRONG_PASSWORD_ERROR = 'The password is incorrect';
const UNAUTHORIZED = 'User unauthorized';
module.exports = {
 PASSWORD_MISMATCH_ERROR,
 USER_NOT_FOUND_ERROR, 
 WRONG_PASSWORD_ERROR,
 UNAUTHORIZED
}

and then in the file where you want to import, you can either using normal way or by object destructuring.

const constants = require('path to the file');

and access it like constants.UNAUTHORIZED

OR

const {PASSWORD_MISMATCH_ERROR, USER_NOT_FOUND_ERROR, WRONG_PASSWORD_ERROR, UNAUTHORIZED } = require('path to the file');

CodePudding user response:

You can export them all as a config together:

export const ERRORS = {
  PASSWORD_MISMATCH_ERROR: 'Passwords not match!',
  USER_NOT_FOUND_ERROR: 'User not found',
  WRONG_PASSWORD_ERROR: 'The password is incorrect',
  UNAUTHORIZED: 'User unauthorized',
}

Now, when you add some new variable, you don't have to explicitly export it.

  • Related