Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module './drivers/node-mongodb-native/connection' using Parcel #9603

Closed
julesmons opened this issue Nov 30, 2020 · 9 comments · Fixed by #9937
Closed

Cannot find module './drivers/node-mongodb-native/connection' using Parcel #9603

julesmons opened this issue Nov 30, 2020 · 9 comments · Fixed by #9937
Labels
developer-experience This issue improves error messages, debugging, or reporting
Milestone

Comments

@julesmons
Copy link

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

What is the current behavior?
I'm using parcel v2 to bundle an entire node api into one JS file.
It's a relatively small api, and i need to bundle all of it because of the hosting env it will ultimately be placed on.

The API uses mongoose and when it's bundled & 'compiled' by parcel it doesn't raise any errors. However, when running the application it doesn't work because certain parts of mongoose are missing, that's where the error "Cannot find module './drivers/node-mongodb-native/connection'" comes from. The runtime environment can't find this module because it's never bundled.

The cause of this problem has been found in other issues from other libraries. See:
parcel-bundler/parcel#4031
evanw/esbuild#480

The esbuild issue is from a different library, but it boils down to the same problem.
Some parts of mongoose are required dynamically using a variable. See:

mongoose/lib/index.js

Lines 729 to 735 in 1c2978e

const driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native';
/*!
* Connection
*/
const Connection = require(driver + '/connection');

The bundlers don't pick this up and this results in those parts not being bundled, and therefore not being available at runtime.

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

To reproduce this issue, i've included a sample project below. Just use "npm run start" to see the error.

The source files
#### package.json
```json
{
    "name": "test",
    "version": "1.0.0",
    "description": "",
    "author": "Jules Mons",
    "license": "ISC",
    "main": "./dist/js/main.js",
    "source": "./src/js/main.js",
    "targets": {
        "main": {
            "distDir": "./dist",
            "context": "node",
            "engines": {
                "node": "12"
            },
            "includeNodeModules": true
        }
    },
    "scripts": {
        "start": "npm-run-all build serve",
        "serve": "node .",
        "build": "cross-env NODE_ENV=production parcel build ./src/js/main.js --target main"
    },
    "dependencies": {
        "mongoose": "5.10.15"
    },
    "devDependencies": {
        "npm-run-all": "4.1.5",
        "parcel": "^2.0.0-nightly.460"
    }
}
```

#### src/js/main.js
```javascript
import mongoose from "mongoose";

mongoose.connect(

    "mongodb://localhost:27017", 
    { useNewUrlParser: true }, 
    (err) => {

        if (!err) {

            console.log("Connection established.");
        }
    }
);
```

What is the expected behavior?
I would expect to see all modules that are needed to be bundled.

A way to achieve this would be to not use a dynamic require. I saw the global.MONGOOSE_DRIVER_PATH is being deprecated anyway. Maybe this issue could be addressed in the next release?

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

Software Version(s)
Parcel 2.0.0-nightly.460
Mongoose 5.10.15
Node 15.3.0, api is compiled to 12
npm 7.0.8
Operating System Windows 10
@vkarpov15 vkarpov15 changed the title Cannot find module './drivers/node-mongodb-native/connection' Remove MONGOOSE_DRIVER_PATH, use a setDriver() function on Mongoose instance instead Nov 30, 2020
@vkarpov15 vkarpov15 added this to the 6.0 milestone Nov 30, 2020
@vkarpov15 vkarpov15 added backwards-breaking developer-experience This issue improves error messages, debugging, or reporting labels Nov 30, 2020
@vkarpov15
Copy link
Collaborator

We definitely want to remove MONGOOSE_DRIVER_PATH in the next major release. However, removing it before then is very risky and may break other people's code because MONGOOSE_DRIVER_PATH has been supported for about 8 years at this point. We use it internally to make sure we avoid the node driver so we can build Mongoose's browser library.

Until then, you'll have to find a workaround with parcel to pull in the missing files. I don't know enough about parcel yet to suggest a workaround, but I'll look into it later this week.

@vkarpov15 vkarpov15 modified the milestones: 6.0, 5.10.19 Nov 30, 2020
@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed backwards-breaking developer-experience This issue improves error messages, debugging, or reporting labels Nov 30, 2020
@vkarpov15 vkarpov15 changed the title Remove MONGOOSE_DRIVER_PATH, use a setDriver() function on Mongoose instance instead Cannot find module './drivers/node-mongodb-native/connection' using Parcel Nov 30, 2020
@julesmons
Copy link
Author

Thank you! Parcel isn't able to manually require the missing files. Nonetheless, thanks for picking this up!

@vkarpov15 vkarpov15 modified the milestones: 5.11.6, 5.11.7, 5.11.x Dec 9, 2020
@stolinski
Copy link

stolinski commented Dec 11, 2020

Hitting this with https://esbuild.github.io as well. @julescubtree did you find a work around for this ever?

@hugdru
Copy link

hugdru commented Feb 11, 2021

I am using esbuild through aws-lambda-nodejs. Noticed some warnings due to dynamic requires which resulted in runtime errors. For logform I followed evanw/esbuild#480 (comment) but for mongoose I used patch-package to modify:

/lib/index.js

mongoose/lib/index.js

Lines 7 to 16 in 1c2978e

if (global.MONGOOSE_DRIVER_PATH) {
const deprecationWarning = 'The `MONGOOSE_DRIVER_PATH` global property is ' +
'deprecated. Use `mongoose.driver.set()` instead.';
const setDriver = require('util').deprecate(function() {
require('./driver').set(require(global.MONGOOSE_DRIVER_PATH));
}, deprecationWarning);
setDriver();
} else {
require('./driver').set(require('./drivers/node-mongodb-native'));
}

mongoose/lib/index.js

Lines 725 to 741 in 1c2978e

/*!
* Driver dependent APIs
*/
const driver = global.MONGOOSE_DRIVER_PATH || './drivers/node-mongodb-native';
/*!
* Connection
*/
const Connection = require(driver + '/connection');
/*!
* Collection
*/
const Collection = require(driver + '/collection');

It is not ideal but works. I also extended NodejsFunction to avoid having to apply these workarounds for each lambda that needs them. Used hooks, fs, and spawnsync in the implementation.

@CryogenicPlanet
Copy link

I am using esbuild through aws-lambda-nodejs. Noticed some warnings due to dynamic requires which resulted in runtime errors. For logform I followed evanw/esbuild#480 (comment) but for mongoose I used patch-package to modify:
/lib/index.js

It is not ideal but works. I also extended NodejsFunction to avoid having to apply these workarounds for each lambda that needs them. Used hooks, fs, and spawnsync in the implementation.

@hugdru Could you share your patched code? I am having the same problem with esbuild and just blocked on it

@hugdru
Copy link

hugdru commented Feb 12, 2021

@CryogenicPlanet Yeah sure. I followed the steps in patch-package which resulted in patches/mongoose+5.10.19.patch in the root of the project.

https://gist.github.com/hugdru/982a31d929a030e29f852c4bfafd9b16

@raphaelschwinger
Copy link
Contributor

raphaelschwinger commented Feb 15, 2021

thx @hugdru for your suggested changes! We could use those in our project. Since we are also effected by typegoose/typegoose#480 and therefore need mongoose version 5.10.18 I forked this repo and import a branch with your patch applied and import it with yarn.

    "mongoose": "raphaelschwinger/mongoose#fix-import"

@vkarpov15
Copy link
Collaborator

I took a closer look at this and we may be able to work around this in Mongoose 5.x without breaking the API completely. We'll review a bit more but otherwise we should be able to release this fix with Mongoose 5.12.8 👍

@vkarpov15 vkarpov15 modified the milestones: 5.12.x, 5.12.8 Apr 29, 2021
@vkarpov15 vkarpov15 added developer-experience This issue improves error messages, debugging, or reporting and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Apr 29, 2021
vkarpov15 added a commit that referenced this issue May 8, 2021
@vkarpov15
Copy link
Collaborator

Fix will be in 5.12.8 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants