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' #4031

Closed
SilverCoder opened this issue Jan 23, 2020 · 12 comments
Closed

Cannot find module './drivers/node-mongodb-native/connection' #4031

SilverCoder opened this issue Jan 23, 2020 · 12 comments

Comments

@SilverCoder
Copy link

SilverCoder commented Jan 23, 2020

this a 🐛 bug report

hi,

i'm trying to bundle a nodejs app (using mongoose) with parceljs.
the bundle succeeds but when i try to run the bundled file, i get the follwing error.

Cannot find module './drivers/node-mongodb-native/connection'

🎛 Configuration (.babelrc, package.json, cli command)

.babelrc

{
  "presets": ["@babel/preset-env"]
}

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "module": "CommonJS",
    "target": "ESNext",
    "allowJs": true
  }
}

package.json

{
  "scripts": {
    "bundle": "parcel build ./src/index.js --target node --bundle-node-modules"
  },
  "dependencies": {
    "@babel/runtime": "^7.7.7",
    "@pnp/common": "^1.3.8",
    "@pnp/graph": "^1.3.8",
    "@pnp/logging": "^1.3.8",
    "@pnp/nodejs": "^1.3.8",
    "@pnp/odata": "^1.3.8",
    "@types/dotenv": "^8.2.0",
    "@types/express": "^4.17.2",
    "@types/mongoose": "^5.5.38",
    "@types/ramda": "^0.26.39",
    "@types/socket.io": "^2.1.4",
    "adal-node": "^0.2.1",
    "axios": "^0.19.0",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "i18next": "^19.0.3",
    "i18next-express-middleware": "^1.9.1",
    "i18next-node-fs-backend": "^2.1.3",
    "jsonwebtoken": "^8.5.1",
    "jwks-rsa": "^1.6.0",
    "log4js": "^6.1.0",
    "luxon": "^1.21.3",
    "memory-cache": "^0.2.0",
    "mongoose": "^5.8.9",
    "ramda": "^0.26.1",
    "socket.io": "^2.3.0",
    "uws": "github:mmdevries/uws#2.4.1"
  },
  "devDependencies": {
    "@babel/core": "^7.7.7",
    "@babel/node": "^7.7.7",
    "@babel/plugin-transform-runtime": "^7.7.6",
    "@babel/preset-env": "^7.7.7",
    "babel-eslint": "^10.0.3",
    "babel-loader": "^8.0.6",
    "babel-plugin-rewire": "^1.2.0",
    "cross-env": "^6.0.3",
    "eslint": "^6.8.0",
    "eslint-config-babel": "^9.0.0",
    "eslint-plugin-flowtype": "^4.5.3",
    "jest": "^24.9.0",
    "moxios": "^0.4.0",
    "nodemon": "^2.0.2",
    "parcel-bundler": "^1.12.4",
    "supertest": "^4.0.2"
  }
}

🤔 Expected Behavior

i expect to execute npm run bundle and get an index.js that works :)

😯 Current Behavior

/usr/src/server/dist/index.js:1
parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]=l)}if(par

Error: Cannot find module './drivers/node-mongodb-native/connection'
Require stack:
- /usr/src/server/dist/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
    at Function.Module._load (internal/modules/cjs/loader.js:690:27)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at f (/usr/src/server/dist/index.js:1:293)
    at p (/usr/src/server/dist/index.js:1:544)
    at Object.parcelRequire.iK3j../driver (/usr/src/server/dist/index.js:1792:5170)
    at f (/usr/src/server/dist/index.js:1:468)
    at p (/usr/src/server/dist/index.js:1:544)
    at Object.parcelRequire.hW9w../lib/ (/usr/src/server/dist/index.js:1794:29)
    at f (/usr/src/server/dist/index.js:1:468)
    at p (/usr/src/server/dist/index.js:1:544)
    at Object.parcelRequire.Focm.@pnp/graph (/usr/src/server/dist/index.js:3460:295)
    at f (/usr/src/server/dist/index.js:1:468)
    at parcelRequire.e4bP (/usr/src/server/dist/index.js:1:771)
    at Object.<anonymous> (/usr/src/server/dist/index.js:1:1023) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/usr/src/server/dist/index.js' ]
}

🌍 Your Environment

Software Version(s)
Parcel 1.12.4
Node 12.13.1
npm 6.12.1
Operating System Windows 10
Docker 19.03.5, build 633a0ea
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

@github-actions github-actions bot added the Stale Inactive issues label Jul 22, 2020
@github-actions github-actions bot closed this as completed Aug 5, 2020
@J3m5
Copy link

J3m5 commented Nov 24, 2020

Did you find a solution @SilverCoder ?

@github-actions github-actions bot removed the Stale Inactive issues label Nov 24, 2020
@julesmons
Copy link

julesmons commented Nov 26, 2020

Hello, I've ran into this issue while trying to compile and bundle a node app into a single .js file with parcel 2.

My current environment:

Software Version(s)
Parcel 2.0.0-nightly.460
Mongoose 5.10.15
Node 15.3.0, app is compiled to 12
npm 7.0.8
Operating System Windows 10

My parcel target is setup like this:

  "targets": {
    "main": {
      "distDir": "./dist",
      "context": "node",
      "engines": {
        "node": "12"
      },
      "includeNodeModules": true
    }
  },

The project also has a .babelrc wich looks like this:

{
  "plugins": [
    "@babel/proposal-class-properties"
  ]
}

Is this a parcel issue, or should i open one over at mongoose?

@mischnic
Copy link
Member

Can you please provide a code sample to reproduce that error with the configuration you posted? Is simply importing mongoose enough to trigger this?

@julesmons
Copy link

julesmons commented Nov 26, 2020

Just simply importing mongoose will trigger this.
I've made a really bare-bones sample project that generates the error on my side.

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.");
        }
    }
);
```

@J3m5
Copy link

J3m5 commented Nov 26, 2020

The same issue happened with esbuild, and this is due to mongoose using require with a variable and not a string.

It doesn't bundle the files that are required without a string.

@J3m5
Copy link

J3m5 commented Nov 26, 2020

Exactly! @mischnic
The same thing should happen with the Connection require.

@J3m5
Copy link

J3m5 commented Nov 26, 2020

@julesmons
Copy link

julesmons commented Nov 30, 2020

I see. Thanks a lot, both of you.
This doesn't seem like it's parcel's problem or responsibility to 'fix'. I think i'm going to open an issue over at mongoose.
In the mean time, is there a way to manually include the entire directory into the bundle like webpack apparently does?

_Edit: I'm still thinking about the require of "global.MONGOOSE_DRIVER_PATH".
How would something like this ever be picked up by parcel, or any other bundler for that matter?

I guess this problem could be approached the way webpack does it by bundeling the entire directory. It's not ideal or optimal, but seems like a good option. What are the chances of something like this making it into parcel?_

@mischnic
Copy link
Member

mischnic commented Nov 30, 2020

I guess this problem could be approached the way webpack does it by bundeling the entire directory. It's not ideal or optimal, but seems like a good option. What are the chances of something like this making it into parcel?_

We agree with esbuild's position here: evanw/esbuild#480 (comment)

I believe Webpack does this by bundling all files reachable in that directory and all nested directories, which ends up unintentionally including extra stuff in the bundle in this case. I think this is kind of an anti-pattern and I'm not currently planning on including this ability in esbuild.

Somewhat related Parcel issue: #125


is there a way to manually include the entire directory into the bundle like webpack apparently does?

I'm afraid not. One hack would be using patch-package to replace that dynamic require with a statically analyzeable one.

@julesmons
Copy link

I completely understand your standpoint, and agree with it as well.

I'm going to continue this over at mongoose.
I hope they can find a alternative way to require their modules, like logform did.

Thanks for all the help.

raphaelschwinger added a commit to raphaelschwinger/mongoose that referenced this issue Feb 15, 2021
Importing from paths generated at runtime breaks
parcel, see (parcel-bundler/parcel#4031), or
esbuild, see (evanw/esbuild#480)
By removing the calls to the deprecated global MONGOOSE_DRIVER_PATH this
issue can be resolved.
raphaelschwinger added a commit to raphaelschwinger/mongoose that referenced this issue Feb 15, 2021
Importing from paths generated at runtime breaks
parcel, see (parcel-bundler/parcel#4031), or
esbuild, see (evanw/esbuild#480)
By removing the calls to the deprecated global MONGOOSE_DRIVER_PATH this
issue can be resolved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants