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'
.babelrc
{
"presets": ["@babel/preset-env"]
}
````
tsconfig.json
```json
{
"compilerOptions": {
"baseUrl": ".",
"module": "CommonJS",
"target": "ESNext",
"allowJs": true
}
}
````
package.json
```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 Error: Cannot find module './drivers/node-mongodb-native/connection' | Software | Version(s) |
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
Require stack:
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.
code: 'MODULE_NOT_FOUND',
requireStack: [ '/usr/src/server/dist/index.js' ]
}
```馃實 Your Environment
| ---------------- | ---------- |
| Parcel | 1.12.4
| Node | 12.13.1
| npm | 6.12.1
| Operating System | Windows 10
| Docker | 19.03.5, build 633a0ea
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.
Did you find a solution @SilverCoder ?
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?
Can you please provide a code sample to reproduce that error with the configuration you posted? Is simply importing mongoose enough to trigger this?
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.");
}
}
);
```
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.
Exactly! @mischnic
The same thing should happen with the Connection require.
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?_
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: https://github.com/evanw/esbuild/issues/480#issuecomment-714350999
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: https://github.com/parcel-bundler/parcel/issues/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.
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.