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: module is not defined #2031

Open
catamphetamine opened this issue Sep 9, 2022 · 1 comment
Open

Uncaught ReferenceError: module is not defined #2031

catamphetamine opened this issue Sep 9, 2022 · 1 comment

Comments

@catamphetamine
Copy link

Throws: Uncaught ReferenceError: module is not defined

I assume that the error originates in:

module.hot&&module.hot.accept([]),module.exports={doclets:{},displayName:"PhoneNumberInput",description:"",...

Config:

module.exports = {
  components: "source/PhoneInputWithCountry.js",
  styleguideDir: "website/docs",
  usageMode: "expand",
  sortProps: props => props,
  dangerouslyUpdateWebpackConfig(webpackConfig, env) {
    webpackConfig.output.filename = 'build/bundle.js'
    webpackConfig.output.chunkFilename = 'build/[name].js'
    return webpackConfig
  },
  webpackConfig: {
    module: {
      rules: [
        // Babel loader will use your project’s babel.config.js
        {
          test: /\.jsx?$/,
          exclude: /node_modules/,
          loader: 'babel-loader'
        },
        // Other loaders that are needed for your components
        {
          test: /\.css$/,
          use: ['style-loader', 'css-loader']
        }
      ]
    }
  }
}
@catamphetamine
Copy link
Author

A thing to note is that the project uses type: "module" feature.

First, I attempted to rename styleguide.config.js to styleguide.config.cjs, otherwise it complained about module.exports not being a valid ES6 import syntax.

That didn't work (see the error above: module is undefined).

I found another workaround:

  • In the project, I've created a folder called react-styleguidist.
  • Moved styleguide.config.js file there, with some edits (shown below).
  • In that folder, I also created a simple package.json file without type: "module" setting. This means that any require()s inside that folder will resolve as ES5.
  • In that folder, I also created a package subfolder where I copied:
    • .babelrc
    • src folder with the components

That way, it worked.

styleguide.config.js:

module.exports = {
	components: "project/source/PhoneInputWithCountry.js",
	styleguideDir: "../website/docs",
	usageMode: "expand",
	sortProps: props => props,
	dangerouslyUpdateWebpackConfig(webpackConfig, env) {
		webpackConfig.output.filename = 'build/bundle.js'
		webpackConfig.output.chunkFilename = 'build/[name].js'
		return webpackConfig
	},
	webpackConfig: {
		module: {
			rules: [
				// Babel loader will use your project’s babel.config.js
				{
					test: /\.jsx?$/,
					exclude: /node_modules/,
					loader: 'babel-loader',
					options: {
						babelrcRoots: ['..']
					}
				},
				// Other loaders that are needed for your components
				{
					test: /\.css$/,
					use: ['style-loader', 'css-loader']
				}
			]
		}
	}
}

package.json:

{
  "private": true,
  "name": "react-phone-number-input/react-styleguidist",
  "main": "styleguide.config.js"
}

https://gitlab.com/catamphetamine/react-phone-number-input/-/commit/309cf471f510995c5d94b2d7a8d169fdce30a9b0

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

1 participant