I am creating a React component and want to publish it.
I've put in the package.json
:
"peerDependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1"
},
And expect that when building my module with webpack, my component would not yell this:
Module not found: Error: Can't resolve 'react'
How can I compile a production component without including React?
I checked some famous ones :
react-select react-toggle
And they list react only in peerDependecies (and in devDependecies, for testing and development purposes I guess)
If I add react to my devDeps and run webpack -p
it compiles successfully but then I have react
in my component bundle.
Here my simple webpack config
What am I missing?
UPDATE I added "external" option in webpack config (the gist is updated), with this is compiles correctly, it does not include React in the bundle, but when I import it in project with React to use it it gives me:
Cannot read property 'Component' of undefined
soon as it finds React.Component
of my component...
You can set it as a peerDependency
but also add it as a devDependency
so you can install it locally. make sure it's as external
in webpack config. That's about all you need
see https://webpack.js.org/configuration/externals/#object
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
},
'prop-types': {
root: 'PropTypes',
commonjs2: 'prop-types',
commonjs: 'prop-types',
amd: 'prop-types'
}
},
output: {
publicPath: '/',
filename: '[name]',
library: "foobar",
libraryTarget: 'umd',
}
when consuming:
"dependencies": {
"foobar": "1.0.0",
"react": "~16.0.0",
"react-dom": "~16.0.0",
"prop-types": "^15.6.0"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With