Because a NestJS Module is itself a module for an express app, what you could do to make that module usable inside another app that is not using currently NestJS, is to mount it as a sub app.
I've build a NestJS module and mounted it as a subapp of a vanilla express app, you could borrow from my example test here: https://github.com/tzkmx/nestjs-graphql-as-express-subapp
The key is exporting your Module as a sub app already initialized, you could not mount the modue directly inside another express app without using the NestJS framework itself in it.
// src/sub-app/boot.js
import { NestFactory } from '@nestjs/core'
import { SubAppModule } from './module'
export default async function bootstrap () {
const app = await NestFactory.create(SubAppModule)
return app
}
// src/app.js
import bootstrapSubApp from './sub-app/boot'
import express from 'express'
const app = express()
app.get('/', (req, res) => res.send('hello express\n'))
async function mountSubApp (app, mountPath, subAppBoot) {
const subApp = await subAppBoot()
await subApp.init()
app.use(mountPath, subApp.getHttpAdapter().getInstance())
return app
}
mountSubApp(app, '/sub', bootstrapSubApp)
.then(app => app.listen(4000))
As you can see, it is needed first call subApp.init()
and then getting the express instance with subApp.getHttpAdapter().getInstance()
to mount it in the vanilla js express app.