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

ReferenceError: process is not defined (Docusaurus) #66

Closed
pomber opened this issue Jun 10, 2021 · 9 comments
Closed

ReferenceError: process is not defined (Docusaurus) #66

pomber opened this issue Jun 10, 2021 · 9 comments
Labels
bug Something isn't working

Comments

@pomber
Copy link

pomber commented Jun 10, 2021

Hi. I was testing sandpack-react with Docusaurus, but there's a fatal error as soon as I import a sandpack component:

client.js?e6b8:10 Uncaught (in promise) ReferenceError: process is not defined
    at eval (webpack-internal:///./node_modules/@codesandbox/sandpack-client/esm/client.js:21)
    at Module../node_modules/@codesandbox/sandpack-client/esm/client.js (:3000/vendors-node_modules_codesandbox_sandpack-react_dist_esm_presets_SandpackRunner_js.js:23)
    at __webpack_require__ (:3000/runtime~main.js:36)
    at fn (:3000/runtime~main.js:339)
    at eval (webpack-internal:///./node_modules/@codesandbox/sandpack-react/dist/esm/contexts/sandpackContext.js:8)
    at Module../node_modules/@codesandbox/sandpack-react/dist/esm/contexts/sandpackContext.js (:3000/vendors-node_modules_codesandbox_sandpack-react_dist_esm_presets_SandpackRunner_js.js:143)
    at __webpack_require__ (:3000/runtime~main.js:36)
    at fn (:3000/runtime~main.js:339)
    at eval (webpack-internal:///./node_modules/@codesandbox/sandpack-react/dist/esm/hooks/useSandpack.js:6)
    at Module../node_modules/@codesandbox/sandpack-react/dist/esm/hooks/useSandpack.js (:3000/vendors-node_modules_codesandbox_sandpack-react_dist_esm_presets_SandpackRunner_js.js:183)
    at __webpack_require__ (:3000/runtime~main.js:36)
    at fn (:3000/runtime~main.js:339)
    at eval (webpack-internal:///./node_modules/@codesandbox/sandpack-react/dist/esm/common/Layout.js:8)
    at Module../node_modules/@codesandbox/sandpack-react/dist/esm/common/Layout.js (:3000/vendors-node_modules_codesandbox_sandpack-react_dist_esm_presets_SandpackRunner_js.js:79)
    at __webpack_require__ (:3000/runtime~main.js:36)
    at fn (:3000/runtime~main.js:339)
    at eval (webpack-internal:///./node_modules/@codesandbox/sandpack-react/dist/esm/presets/SandpackRunner.js:7)
    at Module../node_modules/@codesandbox/sandpack-react/dist/esm/presets/SandpackRunner.js (:3000/vendors-node_modules_codesandbox_sandpack-react_dist_esm_presets_SandpackRunner_js.js:215)
    at __webpack_require__ (:3000/runtime~main.js:36)
    at fn (:3000/runtime~main.js:339)
    at eval (webpack-internal:///./src/pages/index.js:12)
    at Module../src/pages/index.js (:3000/component---site-src-pages-index-jsc-4-f-f99.js:47)
    at __webpack_require__ (:3000/runtime~main.js:36)
    at Function.fn (:3000/runtime~main.js:339)

Made a repro repo here, but it's basically the default docusaurus template with this added to pages/index.js:

import { SandpackRunner } from "@codesandbox/sandpack-react";

const CustomSandpack = () => <SandpackRunner />;

Using version 0.0.6.

@alexnm
Copy link
Collaborator

alexnm commented Jun 11, 2021

this is strange, feels like the error is coming from this line: https://github.com/codesandbox/sandpack/blob/main/sandpack-client/src/client.ts#L91 but that should be solved by the bundler?! Not familiar with docusaurus, this happens at build time?

@JayaKrishnaNamburu
Copy link
Contributor

JayaKrishnaNamburu commented Jun 11, 2021

So, i spent some time around this. And here are some of the observations. This should be handled in many ways, but the first solution is the best way to do it imo.

Solution

First @codesandbox/codesandbox-client is using process and it's a pure node thing. Which is being polyfilled by bundlers all these years, but from webpack~5 the node built-ins are not polyfilled anymore. So,

  • Any packages that target browser shouldn't use process inside them. If they still do so, it's good to remove it in build or anything shipped to npm. And as i see currently sandpack is just transpiling and not doing a build.
  • If the usage is mandatory, then the process needs to be polyfilled by the library and so people who consumes it don't need to do it again on their side. (But if every package stat's doing it, then we have node-polyfiils duplicated everywhere in the end build). So, the best way is to remove process from the end build.

I tried making the existing 0.0.6 work. But here the hurdles.

So, currently these are all the things, if process/browser resolves in lezer esm. Then docusaurus-node-polyfills unblocks the webpack error.

Here is the fork of the example above
https://github.com/JayaKrishnaNamburu/sandpack-docusaurus-test/blob/trail/node-polyfill/docusaurus.config.js
https://github.com/JayaKrishnaNamburu/sandpack-docusaurus-test/blob/trail/node-polyfill/package.json#L20

Docusaurus plugin for polyfills
https://github.com/JayaKrishnaNamburu/docusaurus-node-polyfills

@alexnm
Copy link
Collaborator

alexnm commented Jun 14, 2021

wow, nice research @JayaKrishnaNamburu! thanks for taking the time!

On our side we can remove the usage of process in sandpack-client and rely on props when setting up storybook or anything else to run with a local bundler, but we would still have the issue from codemirror I guess.

@JayaKrishnaNamburu
Copy link
Contributor

Yes, the issue still exists with codemirror. Unfortunately, we will run into such issues unless the browser targeted libraries stop using process in end build 😅. So, now only two things left

  • Either this PR get's merged and this plugin upgrades it.
  • Or just write the fallback in the plugin directly since it's a small change. I will see today if i can write the fallback in the docusaurus plugin itself :)

@alexnm
Copy link
Collaborator

alexnm commented Jun 14, 2021

Unfortunately, we will run into such issues unless the browser targeted libraries stop using process in end build 😅

I feel like this is almost impossible, since a lot of libraries rely on env variables, even just for checking NODE_ENV

@JayaKrishnaNamburu
Copy link
Contributor

Yes, yes. So, for the moment polyfilling is the right way to solve those problems. Unless web standards come up with something for in browsers 😄

@JayaKrishnaNamburu
Copy link
Contributor

JayaKrishnaNamburu commented Jun 14, 2021

Here is a update, I have the working repo here https://github.com/JayaKrishnaNamburu/sandpack-docusaurus-test/tree/trail/node-polyfill

Just added a new package docusaurus-node-polyfills which i just released by wrapping up the changes https://github.com/JayaKrishnaNamburu/docusaurus-node-polyfills

@danilowoz danilowoz added bug Something isn't working known issue labels Jun 29, 2021
@pomber
Copy link
Author

pomber commented Aug 16, 2021

None of my use cases use the editor, I use sandpack for running the preview, so I don't need codemirror. Not sure how often other people use/will use sandpack without the editor, but have you considered having the preview runner in its own separated package?

@danilowoz
Copy link
Member

Hey, we believe that this issue has been solved in the latest release (v0.1.13); please let me know if this bug still persists.

Thanks for reporting it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants