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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs