DEV Community

Marcel Scherf
Marcel Scherf

Posted on

1

A REPL for your node project

Having a REPL / console to play with your application code is always handy. So here is a little piece of code that enables me to use a mongoose User model inside a node repl:

// load environment variables from .env file
require('dotenv').config();
// load mongoose and connect to db using env variable
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true });
// start the node repl
const repl = require('repl');
const context = repl.start().context;
// load mongoose User model
const { User } = require('./lib/models/user');
// add mongoose User model to repl context:
context.User = User;
context.mongoose = mongoose;
// start with: node --experimental-repl-await repl.js
// use the User model: await User.find({});
view raw repl.js hosted with ❤ by GitHub

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay