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

Issues building with Vite/Rollup: "TypeError: Cannot read property 'Decimal128' of null" #12335

Open
2 tasks done
AlexRMU opened this issue Aug 26, 2022 · 12 comments
Open
2 tasks done
Labels
interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency
Milestone

Comments

@AlexRMU
Copy link

AlexRMU commented Aug 26, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.5.3

Node.js version

16.15.0

MongoDB server version

4.4

Description

I'm trying to create a bundle for the prod.
I use esbuild, vite, rollup, sveltekit.

An error appears during building:

var decimal128$3 = driver$1.get().Decimal128;
                                 ^
TypeError: Cannot read properties of null (reading 'Decimal128')

Possibly related to #7866

Steps to Reproduce

It is difficult

Expected Behavior

No response

@IslandRhythms IslandRhythms added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Aug 29, 2022
@IslandRhythms
Copy link
Collaborator

Could you please provide a code sample or perhaps the code leading to that point where the error is thrown?

@AlexRMU
Copy link
Author

AlexRMU commented Aug 30, 2022

Somewhere someone has already fixed it. I think the problem was in sveltekit or the adapter.

@AlexRMU AlexRMU closed this as completed Aug 30, 2022
@AlexRMU
Copy link
Author

AlexRMU commented Sep 22, 2022

Reproduction: https://stackblitz.com/edit/sveltejs-kit-template-default-jysxco?file=src/hooks.server.js
The error appears here:

...

var mongoose$1 = {
    exports: {}
};

var lib$5 = {
    exports: {}
};

var driver$1 = {};

/*!
 * ignore
 */

let driver = null;

driver$1.get = function () {
    return driver;
};

driver$1.set = function (v) {
    driver = v;
};

...

/**
 * Decimal128 type constructor
 *
 * #### Example:
 *
 *     const id = new mongoose.Types.Decimal128('3.1415');
 *
 * @constructor Decimal128
 */

var decimal128$3 = driver$1.get().Decimal128; //here

var symbols$3 = {};

symbols$3.arrayAtomicsBackupSymbol = Symbol('mongoose#Array#atomicsBackup');
...

@AlexRMU AlexRMU reopened this Sep 22, 2022
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Sep 30, 2022
@vkarpov15 vkarpov15 added this to the 6.6.5 milestone Sep 30, 2022
@vkarpov15
Copy link
Collaborator

@AlexRMU I don't see any errors? Maybe I just don't understand how to use StackBlitz

image

@vkarpov15 vkarpov15 removed this from the 6.6.5 milestone Oct 4, 2022
@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Oct 4, 2022
@AlexRMU
Copy link
Author

AlexRMU commented Oct 5, 2022

The error appears after the build is runned.
Run

npm i && npm run build && node build/index.js

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Oct 19, 2022
@vkarpov15 vkarpov15 added this to the 6.6.8 milestone Oct 19, 2022
@vkarpov15
Copy link
Collaborator

This looks like a bug with how Vite handles bundling. Like you mentioned, the general structure of the file Vite outputs looks like this:

var driver$1 = {};

/*!
 * ignore
 */

let driver = null;

driver$1.get = function () {
    return driver;
};

driver$1.set = function (v) {
    driver = v;
};

// ...

(function (module, exports) {

  /*!
   * Module dependencies.
   */

  console.log('set driver to', nodeMongodbNative);
  driver$1.set(nodeMongodbNative);
  // ...
})(lib$4);

The problem is that the driver.set() call runs after driver.get().Decimal128 in Vite/Rollup, but before in Node. We tried to make a couple of tweaks, but no luck in getting Rollup to produce correct output.

The following doesn't work:

/** @type {import('vite').UserConfig} */
const config = {
  plugins: [sveltekit()],
  build: {
    commonjsOptions: {
      strictRequires: false
    }
  }
};

Nor does moving the require('./driver').set(require('./drivers/node-mongodb-native')); call to a separate file outside of lib/index.js. Rollup's output is unfortunately wrong, and we unfortunately don't have the expertise in Rollup internals to make this an easy fix. We'll take a look at this issue later, and open up a related issue that will hopefully help.

@vkarpov15 vkarpov15 modified the milestones: 6.7.1, 6.x Unprioritized Nov 2, 2022
@vkarpov15 vkarpov15 added interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Nov 2, 2022
@vkarpov15 vkarpov15 changed the title TypeError: Cannot read property 'Decimal128' of null Issues building with Vite/Rollup: "TypeError: Cannot read property 'Decimal128' of null" Nov 2, 2022
@birkskyum
Copy link

Here is a Reproduction of this issue with a blank SolidStart app:

https://github.com/birkskyum/solid-mongoose-issue

@birkskyum
Copy link

There is a lot of circular dependencies going on in mongoose, but in my case this error could be avoided by configuring the @rollup/plugin-commonjs with the configuration: { strictRequires: true } (see more here)

@vkarpov15
Copy link
Collaborator

I took a look and this is still an issue. New error message is the following:

/gh-12335/dist/index.js:134476
	  const Connection = _mongoose.__driver.Connection;
	                                        ^

TypeError: Cannot read properties of null (reading 'Connection')
    at Mongoose.createConnection (/gh-12335/dist/index.js:134476:42)

Here's the logic that sets __driver when compiled by rollup:

driver$2.set(nodeMongodbNative);

That logic is after the logic that initializes the Mongoose singleton:

const mongoose = module.exports = new Mongoose({
          [defaultMongooseSymbol]: true
        });

Not sure why this is happening, I don't see any circular imports related to driver imports in particular. But we'll take a look and see if we can resolve some of the circular imports.

@AlexRMU
Copy link
Author

AlexRMU commented Mar 22, 2024

Maybe it's a rollup bug
Maybe you need to change the rollup settings
Maybe you need to switch to es modules

@vkarpov15 vkarpov15 modified the milestones: 8.3, 8.4 Mar 22, 2024
@vkarpov15
Copy link
Collaborator

Yeah all of those are valid explanations, but unfortunately not necessarily viable courses of action at this time. Apparently you can fix this with strictRequires option in Vite/rollup, but I'd love to see if there's a way we can fix this without requiring that option

@Coiggahou2002
Copy link

Yeah all of those are valid explanations, but unfortunately not necessarily viable courses of action at this time. Apparently you can fix this with strictRequires option in Vite/rollup, but I'd love to see if there's a way we can fix this without requiring that option

I've encountered the same problem, setting the options of @rollup/plugin-commonjs to { strictRequires: true } does works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency
Projects
None yet
Development

No branches or pull requests

5 participants