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

ts-invariant 'cant resolve imported dependency process/browser' -- conflict with webpack 5 typical polyfill #287

Open
disarticulate opened this issue May 5, 2022 · 8 comments

Comments

@disarticulate
Copy link

disarticulate commented May 5, 2022

this package appears to conflict with a seemingly standard polyfill in webpack5:

        cfg.resolve.alias = {
          ...cfg.resolve.alias,
          process: 'process/browser',
        }
        cfg.plugins = [
          ...cfg.plugins,
          new webpack.ProvidePlugin({
            Buffer: ['buffer', 'Buffer'],
            process: 'process/browser',
          }),
        ]

resulting in this error:

/node_modules/@apollo/client/node_modules/ts-invariant/process/index.js
Module not found: Can't resolve imported dependency "process/browser"
Did you forget to install it? You can run: yarn add process/browser

eg, https://stackoverflow.com/questions/65018431/webpack-5-uncaught-referenceerror-process-is-not-defined

Not sure how process is working in this instance, but a likely fix is just to rename the folder for import.

It compiles without the plugin/alias, but then throws in other packages because process is not available.

@luckyrobot
Copy link

same issue

Uniswap/web3-react#423

@luckyrobot
Copy link

found solution, adding fullySpecified to false

/* config-overrides.js */
const webpack = require('webpack')

module.exports = function override(config) {
  const fallback = config.resolve.fallback || {}
  Object.assign(fallback, {
    util: require.resolve('util/'),
    crypto: require.resolve('crypto-browserify'),
    stream: require.resolve('stream-browserify'),
    buffer: require.resolve('buffer'),
    assert: require.resolve('assert'),
    http: require.resolve('stream-http'),
    https: require.resolve('https-browserify'),
    os: require.resolve('os-browserify'),
    url: require.resolve('url'),
  })
  config.plugins.push(
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
      process: 'process/browser',
    }),
  )
  config.module.rules = [
    ...config.module.rules,
    {
      test: /\.m?js/,
      resolve: {
        fullySpecified: false,
      },
    },
  ]
  config.resolve.fallback = fallback
  config.ignoreWarnings = [/Failed to parse source map/]
  return config
}

@disarticulate
Copy link
Author

@luckyrobot Thanks. This appears to work but it seems like such a monkey patch for this library.

@benjamn
Copy link
Member

benjamn commented May 5, 2022

@disarticulate What version of ts-invariant are you using?

It seems like webpack is injecting an import for process/browser that does not exist in the ts-invariant code.

@disarticulate
Copy link
Author

looks like @apollo-client @ 3.6.2 installs ts-variant @ 0.10.2

@benjamn

@ravenflores
Copy link

found solution, adding fullySpecified to false

/* config-overrides.js */
const webpack = require('webpack')

module.exports = function override(config) {
  const fallback = config.resolve.fallback || {}
  Object.assign(fallback, {
    util: require.resolve('util/'),
    crypto: require.resolve('crypto-browserify'),
    stream: require.resolve('stream-browserify'),
    buffer: require.resolve('buffer'),
    assert: require.resolve('assert'),
    http: require.resolve('stream-http'),
    https: require.resolve('https-browserify'),
    os: require.resolve('os-browserify'),
    url: require.resolve('url'),
  })
  config.plugins.push(
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
      process: 'process/browser',
    }),
  )
  config.module.rules = [
    ...config.module.rules,
    {
      test: /\.m?js/,
      resolve: {
        fullySpecified: false,
      },
    },
  ]
  config.resolve.fallback = fallback
  config.ignoreWarnings = [/Failed to parse source map/]
  return config
}

thanks bro

@AumyF
Copy link

AumyF commented Sep 26, 2022

I avoided the problem by replacing process/browser with process: 'process/browser.js'

@jheavejimenez
Copy link

thanks bro

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

6 participants