Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the package.json for deployment without using create-react-app and only using my own Webpack?

I am doing an app assessment which I finished if not for a problem for which I cannot find a solution. Some of the requirements are to not use CRA (create-react-app) and to configure my own Webpack, which I did, then to push the final react app to Github Pages, which is where I am getting errors.

After I installed gh-pages and ran npm run deploy I am, obviously, getting the error: npm ERR! missing script: build, because I don't have it. Since I was used just to run CRA and have it worked, I wrongly never wondered what each script from react-scripts does.

So I am confused about what I should do now with the build script. Any help would be great.

Here is the package.json:

{
  "name": "assessment",
  "homepage": "https://myUsername.github.io/Github_followers_front-end/",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "webpack serve --config ./webpack.config.js --mode development",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
  },
  "keywords": [],
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.12.10",
    "@babel/preset-env": "^7.12.11",
    "@babel/preset-react": "^7.12.10",
    "antd": "^4.9.4",
    "axios": "^0.21.0",
    "babel-loader": "^8.2.2",
    "css-loader": "^5.0.1",
    "file-loader": "^6.2.0",
    "gh-pages": "^3.1.0",
    "lodash": "^4.17.10",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-hot-loader": "^4.13.0",
    "style-loader": "^2.0.0",
    "svg-url-loader": "^7.1.1",
    "webpack": "^5.11.0",
    "webpack-cli": "^4.2.0",
    "webpack-dev-server": "^3.11.0"
  },
  "dependencies": {
    "dotenv": "^8.2.0"
  }
}

Here is the webpack.config.js:

const webpack = require('webpack');
const path = require('path');
module.exports = {
  entry: path.resolve(__dirname, './src/index.js'),
  module: {
    rules: [
      // React loader
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      // css loader
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      // Images loader
      {
        test: /\.(png|jpe?g|gif)$/i,
        use: [
          {
            loader: 'file-loader',
          },
        ],
      },
      // SVG loader
      {
        test: /\.svg$/,
        use: [
          {
            loader: 'svg-url-loader',
            options: {
              limit: 10000,
            },
          },
        ],
      },
    ]
  },
  resolve: {
    extensions: ['*', '.js', '.jsx'],
  },
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: 'bundle.js',
  },
  plugins: [new webpack.HotModuleReplacementPlugin()],
  devServer: {
    contentBase: path.resolve(__dirname, './dist'),
    port: 3000,
    hot: true
  },
};

And the .babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ]
}
like image 709
Andrea D_ Avatar asked Oct 16 '25 22:10

Andrea D_


1 Answers

Add this to the scripts object in your package.json file

"build": "webpack --mode production"

So your scripts looks like this

"scripts": {
    "start": "webpack serve --config ./webpack.config.js --mode development",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "build": "webpack --mode production"
  },
like image 163
Elijah Enuem-Udogu Avatar answered Oct 18 '25 13:10

Elijah Enuem-Udogu