cookpete / react-player

A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion
https://cookpete.github.io/react-player
MIT License
9.35k stars 1.15k forks source link

TypeError: Cannot assign to read only property 'exports' of object '#<Object>' #1862

Open angela318 opened 3 months ago

angela318 commented 3 months ago

Current Behavior

I tried to import ReactPlayer to my react rails app. I added the line below: import ReactPlayer from 'react-player' and the page cannot be loaded. I saw the error below in console:

TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    at Module.<anonymous> (utils.js:126:1)
    at ./node_modules/react-player/lib/utils.js (utils.js:368:1)
    at __webpack_require__ (index-07d9361752105ff10b68.js:64:30)
    at ./node_modules/react-player/lib/players/index.js (index.js:81:1)
    at __webpack_require__ (index-07d9361752105ff10b68.js:64:30)
    at ./node_modules/react-player/lib/index.js (index.js:81:1)
    at __webpack_require__ (index-07d9361752105ff10b68.js:64:30)

It fails at this line: module.exports = __toCommonJS(utils_exports);

Expected Behavior

I want to use ReactPlayer to play a video but it fails at importing

Steps to Reproduce

I just add the line below into my app and the page error out: import ReactPlayer from 'react-player'

Environment

Other Information

node version: v14.21.3 yarn version: v1.22.19

babel.config.js:

module.exports = function(api) {
  var validEnv = ['development', 'test', 'production']
  var currentEnv = api.env()
  var isDevelopmentEnv = api.env('development')
  var isProductionEnv = api.env('production')
  var isTestEnv = api.env('test')

  if (!validEnv.includes(currentEnv)) {
    throw new Error(
      'Please specify a valid `NODE_ENV` or ' +
        '`BABEL_ENV` environment variables. Valid values are "development", ' +
        '"test", and "production". Instead, received: ' +
        JSON.stringify(currentEnv) +
        '.'
    )
  }

  return {
    presets: [
      isTestEnv && [
        '@babel/preset-env',
        {
          targets: {
            node: 'current'
          },
          modules: 'commonjs'
        },
        '@babel/preset-react'
      ],
      (isProductionEnv || isDevelopmentEnv) && [
        '@babel/preset-env',
        {
          forceAllTransforms: true,
          useBuiltIns: 'entry',
          corejs: 3,
          modules: false,
          exclude: ['transform-typeof-symbol']
        }
      ],
      [
        '@babel/preset-react',
        {
          development: isDevelopmentEnv || isTestEnv,
          useBuiltIns: true
        }
      ]
    ].filter(Boolean),
    plugins: [
      'babel-plugin-macros',
      '@babel/plugin-syntax-dynamic-import',
      isTestEnv && 'babel-plugin-dynamic-import-node',
      '@babel/plugin-transform-destructuring',
      [
        '@babel/plugin-proposal-class-properties',
        {
          loose: true
        },
      ],
      [
        '@babel/plugin-proposal-private-methods',
        {
          loose: true
        }
      ],
      [
        '@babel/plugin-proposal-object-rest-spread',
        {
          useBuiltIns: true
        }
      ],
      [
        '@babel/plugin-transform-runtime',
        {
          helpers: false,
          regenerator: true,
          corejs: false
        }
      ],
      [
        '@babel/plugin-transform-regenerator',
        {
          async: false
        }
      ],
      isProductionEnv && [
        'babel-plugin-transform-react-remove-prop-types',
        {
          removeImport: true
        }
      ]
    ].filter(Boolean)
  }
}

webpack.config

const path = require('path');
module.exports = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.js$/i,
        include: path.resolve(__dirname, 'src'),
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          },
        },
      },
      {
        test: /\.css$/i,
        include: path.resolve(__dirname, 'src'),
        use: ['style-loader', 'css-loader', 'postcss-loader'],
      },
      {
        test: /\.mp4$/,
        use: {
          loader: 'file-loader'
        }
  }
    ],
  },
  devServer: {
    contentBase: path.resolve(__dirname, 'dist'),
    watchContentBase: true,
  },
};

I don't see anyone facing the same issue, I guess it might not be related to the package. But I don't know how to debug and proceed. Please help, thanks

briardg commented 3 months ago

I get something similar:

Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

And when trying to fetcher the player/file like the example in the readme with youtube

I need to do

import ReactPlayer from 'react-player/file.js'

@angela318 if in the last two weeks you found a work around could you please keep me in touch? šŸ™

thanks in advance

angela318 commented 3 months ago

I get something similar:

Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

And when trying to fetcher the player/file like the example in the readme with youtube

I need to do

import ReactPlayer from 'react-player/file.js'

@angela318 if in the last two weeks you found a work around could you please keep me in touch? šŸ™

thanks in advance

I haven't found any workaround and am currently using the video tag directly Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video