5

I am setting up a new mean project from mean.io clone. after installing the npm packages and starting the mongod. I run npm start it gives me this eror.

Error: Config validation error: child "JWT_SECRET" fails because ["JWT_SECRET" is required]

this is my config.js

const Joi = require('joi');

// require and configure dotenv, will load vars in .env in PROCESS.ENV
require('dotenv').config();

// define validation for all the env vars
const envVarsSchema = Joi.object({
  NODE_ENV: Joi.string()
    .allow(['development', 'production', 'test', 'provision'])
    .default('development'),
  SERVER_PORT: Joi.number()
    .default(4040),
  MONGOOSE_DEBUG: Joi.boolean()
    .when('NODE_ENV', {
      is: Joi.string().equal('development'),
      then: Joi.boolean().default(true),
      otherwise: Joi.boolean().default(false)
    }),
  JWT_SECRET: Joi.string().required()
    .description('JWT Secret required to sign'),
  MONGO_HOST: Joi.string().required()
    .description('Mongo DB host url'),
  MONGO_PORT: Joi.number()
    .default(27017)
}).unknown()
  .required();

const { error, value: envVars } = Joi.validate(process.env, envVarsSchema);
if (error) {
  throw new Error(`Config validation error: ${error.message}`);
}

const config = {
  env: envVars.NODE_ENV,
  port: envVars.SERVER_PORT,
  mongooseDebug: envVars.MONGOOSE_DEBUG,
  jwtSecret: envVars.JWT_SECRET,
  frontend: envVars.MEAN_FRONTEND || 'angular',
  mongo: {
    host: envVars.MONGO_HOST,
    port: envVars.MONGO_PORT
  }
};

module.exports = config;

Not sure where is the problem. I haven't changed a single thing I just took a clone from the official mean.io site. installed npm packages and started the mongodb.

Sami
  • 1,041
  • 3
  • 16
  • 32

1 Answers1

5

Add an .env file to the root folder with the following contents:

NODE_ENV=development
SERVER_PORT=4040
JWT_SECRET=0a6b944d-d2fb-46fc-a85e-0295c986cd9f
MONGO_HOST=mongodb://localhost/mean
MEAN_FRONTEND=angular

You may find an .env.example file in your root directory, which you can simply rename to .env. An example .env.example file can be found here.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
IftekharDani
  • 3,619
  • 1
  • 16
  • 21