0

I made my own react component npm package and published in npm, now when import and use it in other CRA apps, i get this error when npm start in run in command line .Browser Error

and in console:

Console Error

My webpack.config.js file:

var webpack = require('webpack');
var path = require('path');
var nodeExternals = require('webpack-node-externals');

module.exports = {
    target: 'web',
    entry: './src/index.js',
    output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'index.js',
    libraryTarget: 'commonjs2'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                include: path.resolve(__dirname, 'src'),
                exclude: /(node_modules|bower_components|build)/,
                use: {
                loader: 'babel-loader',
                options: {
                    presets: ['env']
                }
                }
            },
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.(png|jpg|woff|woff2|eot|ttf|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'url?limit=512&&name=[path][name].[ext]?[hash]'
            }
        ]
    },
    mode: 'development',
    externals: {
        'react': 'commonjs react'
    }
};

My package.json file:

{
  "name": "primetable1",
  "version": "1.0.0",
  "description": "A Datatable for react apps based on Primereact",
  "main": "build/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack --watch",
    "build": "webpack"
  },
  "keywords": [
    "primereact",
    "primeicons"
  ],
  "author": "Anish Arya",
  "license": "ISC",
  "peerDependencies": {
    "react": "^16.4.0",
    "react-dom": "^16.4.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-env": "^1.7.0",
    "babel-runtime": "^6.26.0",
    "css-loader": "^3.4.2",
    "file-loader": "^5.0.2",
    "react": "^16.4.0",
    "react-dom": "^16.4.0",
    "react-hot-loader": "^4.12.19",
    "react-scripts": "3.4.0",
    "style-loader": "^1.1.3",
    "webpack-cli": "^3.3.11",
    "webpack-node-externals": "^1.7.2"
  },
  "dependencies": {
    "classnames": "^2.2.6",
    "primeicons": "^2.0.0",
    "primereact": "^3.4.0",
    "react-transition-group": "^4.3.0"
  }
}

My .babelrc file:

{
    "presets": ["env"],
    "plugins": [
      "transform-object-rest-spread",
      "transform-react-jsx",
      "react-hot-loader/babel"
    ]
  }

I followed this article to create and publish npm package: https://codeburst.io/extracting-a-react-js-component-and-publishing-it-on-npm-2a49096757f5

How to solve this error?

4
  • 1
    I think the externals configuration is bogus here. Try removing it. Commented Feb 20, 2020 at 10:51
  • require('webpack'), where is webpack in your package.json? It's not there. Commented Feb 20, 2020 at 10:51
  • @JMadelaine That's a concern, but it doesn't really matter since webpack-cli etc. pull them in anyway. Commented Feb 20, 2020 at 10:52
  • FWIW, here's a webpack.config.js for a React component that Works For Me (tm): github.com/akx/react-wheely/blob/… Commented Feb 20, 2020 at 10:53

1 Answer 1

1

The externals configuration seems wrong – you probably only need externals: [nodeExternals()] (from const nodeExternals = require("webpack-node-externals");).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.