Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.9k stars 3.83k forks source link

[ Dependencies ] MongoDb: Include some of the optionnalPeerDependencies #8602

Closed PierreCookie closed 4 years ago

PierreCookie commented 4 years ago

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

I want to bundle the package. But when i execute the script i have:

Error: Cannot find module 'mongodb-client-encryption'

If the current behavior is a bug, please provide the steps to reproduce.

Create a file index.js :

const mongoose = require('mongoose')
const express = require('express')

const app = express();
mongoose.connect('mongodb://localhost:27017/', {useUnifiedTopology: true})

app.listen(3000, () => { console.log("succesfully launched server on 3000"})

Try to bundle it with rollup:

rollup.config.js

import commjs from 'rollup-plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';

export default {
  input: 'index.js',
  plugins: [
    resolve();
    json(),
    commonjs()
  ],
  output: [{ file: 'dist/index.js', format: 'cjs', exports: 'named'}]
}

Install the packages:

npm i --save-dev rollup-plugin-commonjs @rollup/plugin-node-resolve @rollup/plugin-json

npm i mongoose express

And insert the script in you package.json:

scripts: {
  "build": "rollup -c"
}

Run build

npm run build

You should have a list of multiple error and the one that "need" to be resolved is mongodb-client-encryption.

Run the bundle

node dist/index.js

What is the expected behavior?

I want to be able to bundle my server, and to launch it with a commande like node .. I don't want to have an error due to unresolved dependencies.

Maybe i should have the module in my package.json but as a user of your librairy i shouldn't do it.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

node: v10.16.0 mongoose: 5.9.1 mongo: latest (3 month ago)

renanpvaz commented 4 years ago

Hey @PierreCookie I'm having the same issue here, were you able to work around it or fix it somehow?

PierreCookie commented 4 years ago

I added the module inside my lib/apps in dependencies, like so it is downloaded, and switch to webpack.

vkarpov15 commented 4 years ago

Here's how you work around this:

import commonjs from 'rollup-plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';

export default {
  input: 'index.js',
  plugins: [
    resolve(),
    json(),
    commonjs({ ignore: ['mongodb-client-encryption'] })
  ],
  output: [{ file: 'dist/index.js', format: 'cjs', exports: 'named'}]
}

No way for Mongoose to work around this while supporting mongodb-client-encryption as an optional peer dependency using require(). The only reason why rollup-plugin-commonjs doesn't choke on kerberos and other optional peer dependencies is because those use require_optional, which we may move away from.