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

Closed
2 tasks done
AlexRMU opened this issue Aug 26, 2022 · 12 comments
Closed
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

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