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

Can't used in browser? #134

Open
cx690 opened this issue Nov 25, 2021 · 3 comments
Open

Can't used in browser? #134

cx690 opened this issue Nov 25, 2021 · 3 comments

Comments

@cx690
Copy link

cx690 commented Nov 25, 2021

I want to use serialize-javascript in vite project.
There are two problem,At first buffer is undefined,I install buffer,and then there is an error at randombytes,the error code is here:

//randombytes/browser.js
var crypto = global.crypto || global.msCrypto

if (crypto && crypto.getRandomValues) {
  module.exports = randomBytes
} else {
  module.exports = oldBrowser
}
.........

In browser,global is undefined!
I try to change it as this:

const globalThis = typeof global === 'object' ? global : window;

var crypto = globalThis.crypto || globalThis.msCrypto

if (crypto && crypto.getRandomValues) {
  module.exports = randomBytes
} else {
  module.exports = oldBrowser
}

And then,serialize-javascript wrok!

@pk111and222
Copy link

I want to use serialize-javascript in vite project. There are two problem,At first buffer is undefined,I install buffer,and then there is an error at randombytes,the error code is here:

//randombytes/browser.js
var crypto = global.crypto || global.msCrypto

if (crypto && crypto.getRandomValues) {
  module.exports = randomBytes
} else {
  module.exports = oldBrowser
}
.........

In browser,global is undefined! I try to change it as this:

const globalThis = typeof global === 'object' ? global : window;

var crypto = globalThis.crypto || globalThis.msCrypto

if (crypto && crypto.getRandomValues) {
  module.exports = randomBytes
} else {
  module.exports = oldBrowser
}

And then,serialize-javascript wrok!

Finally, how did you solve it? I also have this problem at present

@cx690
Copy link
Author

cx690 commented Dec 24, 2021

If buffer is undefined,you can install it.
And then set this before your project!

window.global = { crypto:window.crypto };

@NoHara42
Copy link

NoHara42 commented Aug 26, 2022

I am also getting this issue when using Vite in a react project, but the above suggestions don't seem to help.
Any other pointers on what this might be?

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

3 participants