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

Uncaught ReferenceError: process is not defined #339

Closed
ericsoco opened this issue Sep 11, 2019 · 12 comments
Closed

Uncaught ReferenceError: process is not defined #339

ericsoco opened this issue Sep 11, 2019 · 12 comments
Labels
👀 no/external This makes more sense somewhere else

Comments

@ericsoco
Copy link

I recently updated my app to 4.2.2 (from an old, 2.x version) and am seeing an NPE on process when attempting to render a <ReactMarkdown>.

It appears that ReactMarkdown calls unified.parse(), which in turn instantiates a VFile, which accesses process, but process is not available in a browser.

It is easy enough to reproduce (for me) that I'm surprised others haven't reported it, so I'm guessing there's something unique in my setup that is triggering this. Still debugging, but am I correct to assume that this codepath shouldn't be running in the browser?

Will add more information as I dig it up.

@ericsoco
Copy link
Author

It turns out that process is mocked by default by Webpack. Browserify does something similar.

The bug here is really with unified/vfile, so I've also filed it there:
vfile/vfile#38

but note that this issue renders react-markdown effectively unusable in environments that don't mock/polyfill process.

@Colkadome
Copy link

I have a similar issue. Requiring unified/vfile is a bit tricky here, as they both use node.js functions so things like process need to be explicitly shimmed in for some build tools. If this project is to be browser compatible, you could fork unified/vfile and replace the node.js-dependant functions (like is-plain-obj, path.dirname) with custom ones (there aren't many of them).

@AmirTugi
Copy link

Having the same results :(
Can't use this lib because of this.
Bummer

@flybayer
Copy link

The solution is to set this in your webpack config:

webpackConfig.node.process = true;

@shengliangli
Copy link

webpack v5,same thing happend.

@Falci
Copy link

Falci commented Jun 26, 2020

Workaround:

window.process = { cwd: () => '' };

@greybax
Copy link

greybax commented Jul 3, 2020

@flybayer solution doesn't work for me

@Falci where to put this code?
I've tried something like:

plugins: [
    new webpack.DefinePlugin({
      'windows.process': { cwd: () => '' },
    })
  ],

but it doesn't work for me

P.S. downgraded to version 2.5.1. This version works for me without any additional tweaks in webpack config

@Falci
Copy link

Falci commented Jul 3, 2020

@greybax I'm using like this:

import React from 'react';
import ReactMarkdown from 'react-markdown/with-html';

window.process = { cwd: () => '' };

const MyComponent = ({data}) => (
    <LayoutWrapper>
        <ReactMarkdown source={data} escapeHtml={false} />
    </LayoutWrapper>
)

@govizlora
Copy link

A temporary solution for me: vfile/vfile#38 (comment)

@ChristianMurphy ChristianMurphy added the 👀 no/external This makes more sense somewhere else label Oct 8, 2020
@ChristianMurphy
Copy link
Member

This is in vfile, not in react-markdown itself.
See the suggestion @govizlora linked

chrishavekost added a commit to Availity/availity-workflow that referenced this issue Nov 19, 2020
Resolves issues with @availity/spaces compatibility with webpack 5. Spaces depends on react-markdown, which depends on vfile, which needs process.cwd() at runtime. remarkjs/react-markdown#339 (comment)
@zjjjjjjjjjjd
Copy link

module.exports = {
  module: {
    rules: [
      {
        test: /node_modules(\/|\\)vfile(\/|\\)core\.js/,
        use: [{
          loader: 'imports-loader',
          options: {
            type: 'commonjs',
            imports: ['single process/browser process'],
          },
        }],
      },
    ],
  },
};

@guguji5
Copy link

guguji5 commented Sep 9, 2021

if you use vite, add variable in vite.config.js to fix it

define: {
    'process.env': {},
  },

karrui added a commit to opengovsg/design-system that referenced this issue Jan 20, 2022
so when users install it will pull react-markdown from npm instead of using bundled dependency, which may fail if they have not polyfilled process
See remarkjs/react-markdown#339

When pulling from npm, the browser flag is respected and such a problem will not occur
kodiakhq bot pushed a commit to recipeyak/recipeyak that referenced this issue Aug 1, 2022
Basically:
- Replace web pack setup with vite and upgrade various dependencies and cull unnecessary ones.
- Also remove TSLint. Will replace with typescript-eslint rules eventually.
- Got rid of the hacky landing page static generation.

Various road bumps along the way:

- https://javascript.plainenglish.io/how-to-set-up-path-resolving-in-vite-ad284e0d9eae
- fix sass imports vitejs/vite#5764 (comment)
- Then needed to rewrite the alias for typescript again to match the types
- Replace `process`. With the `import.meta.env` thing
- https://stackoverflow.com/questions/64677212/how-to-configure-proxy-in-vite
- Fix imports for static files from `requires()`
- Had to fix proxying for `/avatar` and `/api` in dev
- Ran into remarkjs/react-markdown#339 (comment)
    - Upgrade markdown react to fix
- Migrate from `react-helmet` to fix some deprecation warnings
- Vite has a different jsx config, so no need to import React
    - microsoft/TypeScript#41882
- Vitest issue:
    - https://github.com/vitest-dev/vitest/blob/57c2367196b3fd978a04fa38ebdac7a5b6ef9b16/packages/vite-node/src/client.ts#L178-L179
- Couldn’t import react-dnd, upgraded
    - react-dnd/react-dnd#3368
- `import { isUndefined } from "util"` didn’t work
- Favicon via https://github.com/darkobits/vite-plugin-favicons to replace web pack equivalent 
- Issue with React router browser history in vitest, it exploded until jsdom was setup
- Question: https://stackoverflow.com/questions/71703933/what-is-the-difference-between-vite-and-vite-preview
- Vitest vscode integration broken :/
    - vitest-dev/vscode#44
- Tried happy-dom but it doesn’t work, lots of issues, supposed to be faster
- Took a while to get MSW in a good place, had to write some stuff so missing endpoint mocks fail the test
    - I think there's room for improvement here in terms of developer experience
    - Test with react testing library and API calls
        - https://www.npmjs.com/package/nock
            - Doesn’t fail test on unknown mock
            - https://stackoverflow.com/questions/69430232/jest-nock-how-to-fail-a-test-if-a-non-mocked-request-is-made
        - MSW
            - Doesn’t fail test on unknown mock
            - mswjs/msw#946
        - Relay’s mockEnvironment
          - couldn't easily find thorough examples
        - Apollo mock provider
            - Doesn’t fail test on unknown mock
- Added a visualize plugin similar to a web pack analyzer thing, but it’s slightly off with the numbers it gives for sizes:
    - btd/rollup-plugin-visualizer#96
- TODO:
    - ESLINT rules, replace what tslint provided, eslint-cake anyone?
        - https://typescript-eslint.io/rules/no-floating-promises/
        - And more!!!
    - Replace lodash with lodash-es
        - Or maybe: https://github.com/onebay/vite-plugin-imp
        - Although lodash-es seems cleaner
    - SSR  for landing page?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👀 no/external This makes more sense somewhere else
Development

No branches or pull requests