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

TypeError: t.versions.node is undefined #12392

Closed
2 tasks done
alexredder opened this issue Sep 4, 2022 · 17 comments
Closed
2 tasks done

TypeError: t.versions.node is undefined #12392

alexredder opened this issue Sep 4, 2022 · 17 comments
Assignees
Milestone

Comments

@alexredder
Copy link

alexredder commented Sep 4, 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.6.0

Node.js version

17.6.0

MongoDB server version

where do i find that?

Description

Hey guys,

one day i open my application and suddenly only the following error message pops up:

grafik

I have not changed the code except for updating mongoose.. but i could find out what that error meant or how i can solve it..

this is what it says in chrome:
grafik

I am a newbie so i dont know how to solve this issue.. hope you guys can help me out :)

cheers

Steps to Reproduce

the error message points to
node_modules\mongoose\dist\browser.umd.js (220:268)
line 220 intails the following code:
e.noop=function(){},e.errorToPOJO=function(t){if(!(t instanceof Error))throw new Error("errormust beinstanceof Error.");var e,r={},o=n(Object.getOwnPropertyNames(t));try{for(o.s();!(e=o.n()).done;){var i=e.value;r[i]=t[i]}}catch(t){o.e(t)}finally{o.f()}return r},e.nodeMajorVersion=parseInt(t.versions.node.split(".")[0],10)}).call(this,r(10))},function(t,e,r){"use strict";(function(t){
My Mongo_URI is coming from my .env file

This is my connect code:

`import * as mongoose from 'mongoose';

const connection = {};
const testvar = process.versions.node;

async function connect() {
if (connection.isConnected) {
console.log('already connected');
return;
}
if (mongoose.connections.length > 0) {
connection.isConnected = mongoose.connections[0].readyState;
if (connection.isConnected === 1) {
console.log('use previous connection');
return;
}
await mongoose.disconnect();
}
const db = await mongoose.connect(process.env.MONGODB_URI);
console.log('new connection');
connection.isConnected = db.connections[0].readyState;
}
console.log(testvar);

async function disconnect() {
if (connection.isConnected) {
if (process.env.NODE_ENV === 'production') {
await mongoose.disconnect();
connection.isConnected = false;
} else {
console.log('not disconnected');
}
}
}

function convertDocToObj(doc) {
doc._id = doc._id.toString();
doc.createdAt = doc.createdAt.toString();
doc.updatedAt = doc.updatedAt.toString();
return doc;
}

const db = { connect, disconnect, convertDocToObj };

export default db;
`

UPDATE
i have reset the application to a former state.. now it works in dev-environment but not in my vercel deployment..

Expected Behavior

run smoothly like it did before.. aswell in deployment as development

@alexredder
Copy link
Author

@vkarpov15 can you help me here? i have updated to mongoose 6.6.0 but nothing changed so far..

@lapoguidi
Copy link

lapoguidi commented Sep 11, 2022

Hello,
I have the same problem using mongoose with Sveltekit. I tried also to create an empty Sveltekit skeleton application and after mongoose is added and imported in the browser just to read a Schema, the same error appears.

@alexredder
Copy link
Author

Hello, I have the same problem using mongoose with Sveltekit. I tried also to create an empty Sveltekit skeleton application and after mongoose is added and imported in the browser just to read a Schema, the same error appears.

have you managed something? - i can revert my code but as soon as i update my packages - its all garbage..

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 13, 2022

Can you provide a simple replication repository?

@lapoguidi
Copy link

lapoguidi commented Sep 13, 2022

Sure: https://github.com/lapoguidi/sveltekit-mongoose-http-500

I followed just the instructions I reported on README.md and after the start is running, the console.log is executed, but just after an HTTP 500 is received and inside the console I can read: TypeError: Cannot read properties of undefined (reading 'split').

This is the code I've got in node_modules
code-example

Maybe is useful to know that I tried with:
node v16.17.0 and v17.9.1
npm v8.19.1 and pnpm v7.11.0

Sveltekit is the latest v1.0.0-next.481 with vite v3.1.0 (rollup v2.78.1)

Thank you for the interest.

@nathanbabcock
Copy link

Same issue here, using vite@3.1.2. My only import is Schema, and removing it resolves the error.

@KonghaYao
Copy link

image
You can just write a number to it! And it will all right 😀

@alexredder
Copy link
Author

alexredder commented Sep 19, 2022

@KonghaYao Hi there, i did that in my node_modules/mongoose/dist/browser....js and it solves the issue locally - but when i push it to vercel it displays the same error.. please checkout www.sanivio.de to see in production.

@KonghaYao
Copy link

@alexredder Oh, You need to move the dist/browser.umd.js file to your project folder, because your project upload and bundle your source code with mongoose which has the same bug! You can write an alias redirect your import to the new file path. 😀

@alexredder
Copy link
Author

@KonghaYao sorry.. i dont understand can you please explain in more detail where to put it? ..

@KonghaYao
Copy link

@alexredder 1. You can put browser.js to any path in your project ,like /src/lib/browser.js,
2. if you are using bundler, the bundler like vite will recognize "mongoose" package and find node_module/mongoose always, so we can add an alias like 'mongoose': "/src/lib/browser.js"; vite will automatically redirect mongoose project to your browser.js.
3. But it is an umd file, I think you will use script to load it , just src = the browser.js path, browser will load it.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 19, 2022

I dont know. I played around and it seems that svelte has no global window object, so we can not detect if we have a browser or not.

@lapoguidi
Copy link

lapoguidi commented Sep 20, 2022

Editing browser.umd.js is not a so useful solution, because everytime that the dependencies are rebuilded, it will be overwritten.
I manually edit everytime the code, but a permanent solution would be better.
Untill now I was not able to solve the code generated during build. I'm hoping that somebody more expert than me can understand and solve this problem.

@lapoguidi
Copy link

lapoguidi commented Sep 28, 2022

Just an update.

I manually downloaded from npmjs registry the mongoose packages starting from latest v6.6.2 untill v6.5.2 using an url like this: https://registry.npmjs.org/mongoose/-/mongoose-6.6.2.tgz.
After download I opened one tarball at a time and searched inside dist/browser.umd.js for nodeMajorVersion string.
Well, I found an occurrence in the file starting from v6.5.3 and this is the first version that give that issue; v6.5.2 doesn't contain that string and seems to be immune from this issue.

Then I simply cloned the v6.6.2 repository and tried to build the browser.umd.js.
As I mentioned some post ago, I tried with node v17.9.0, npm v8.19.1 v and webpack v5.74.0.
The resulting browser.umd.js doesn't contain the nodeMajorVersion variable and it seems to work fine.

git clone https://github.com/Automattic/mongoose.git
npm i
npm run build-browser

Beside that, just a couple of questions (for curiosity):

  • why the browser.umd.js provided inside the package contains that variable assignment and in the browser.umd.js built with npm run buil-browser doesn't?
  • why the resulting browser.umd.js just built is really different from the provided one? (export comments and licenses outside in a separate txt file, for example)

Thanks for helping me.

@alexredder
Copy link
Author

Update: installing Mongoose version 6.5.2 fixed it for now..

#11943 (comment)

@vkarpov15 vkarpov15 added this to the 6.6.8 milestone Oct 17, 2022
@maciasello
Copy link
Contributor

Some parts of the issue seem to be related to gh-12576. The dist/browser.umd.js file in 6.6.6 is binary equal to the one found in 5.13.15 (and which contains the offending process.version related code).

@lapoguidi it may be the root cause of the file content differences you observe, but I did not verify on the versions you mention.

@vkarpov15
Copy link
Collaborator

Confirmed fixed by #12577

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants