-2

I'm new to react js and I'm trying to set up the environment for it and I followed the steps mentioned in https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm.

But after doing all the things mentioned there I'm getting this error:

'webpack-dev-server' is not recognized as an internal or external command, operable program or batch file

enter image description here

JAD
  • 2,035
  • 4
  • 21
  • 35
vindy
  • 1
  • Possible duplicate of [webpack is not recognized as a internal or external command,operable program or batch file](https://stackoverflow.com/questions/35810172/webpack-is-not-recognized-as-a-internal-or-external-command-operable-program-or) – George Aug 01 '17 at 16:46

3 Answers3

1

If you want to develop an application using babel, webpack, etc. You need to follow following steps. No doubt there are much better tutorial available over the internet but it will give you some idea.

1.Webpack:

In browsers you can not require or import modules as you usually do while writing node.js code. With the help of a module bundler, maybe Webpack, you can write code that uses require/import in the same way that you would use it in node environment. I am assuming you will use webpack considering its popularity.

2. Install dependencies (es6)

These are minimal dependencies you need in your project (package.json) to get it working. You can directly copy paste the following text into a new file named "package.json". run the following set of commands in you EMPTY project directory:

  1. install the node package manager

    npm init [follow the command prompt to fill in meta data of your project like name, author,etc.]

  2. install global packages

    npm install -g babel babel-cli [this will install transpiler(babel) into your global environment]

  3. install module bundler

    npm install webpack webpack-dev-server --save

  4. install babel plugins

    npm install babel-core babel-loader babel-preset-react babel-preset-es2015

After this command set, your package.json will start looking like as following:

  {
  "name": "reactjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "No Command Written Yet"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "webpack": "^3.4.1",
    "webpack-dev-server": "^2.6.1"
  },
  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1"
  }
}

3.Write your webpack-config.js file

A sample webpack config file should like this. Don't ask me about each bit of it but rather have a look on webpack tutorial because I can not explain everything here. Just remember the fact that Webpack is a module bundler that bundles javascript and other assets for the browser.

    var config = {
   entry: './main.js',

   output: {
      path:'/',
      filename: 'index.js',
   },

   devServer: {
      inline: true,
      port: 8080
   },

   module: {
      loaders: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',

            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;

4.Set up entry point for your application

src->index.js

index.js

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
    <App />
  , document.querySelector('.init')
);

5.Setup index.html in your project root

<!DOCTYPE html>
<html>
  <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>Welcome to ReactJs</title>
  </head>
  <body>
    <div class="init"></div>
  </body>
  <script src="./public/bundle.js"></script>
</html>

6.Running

A slight change is needed in your package.json replace:

"scripts": {
    "test": "No Command Written Yet"
  },

with

"scripts": {
        "dev": "webpack-dev-server --hot"
      },

[this will change the script you will run to execute the app bundled by webpack]

Now, whenever you want to run the project, just be in the project root directory and call:

npm run dev

DONE, Have Fun!

Haider Ali Anjum
  • 831
  • 1
  • 9
  • 20
0

Run:

npm install webpack-dev-server --save-dev

And try again. You got the error because webpack-dev-server couldn't be found in your devDependencies inside of your package.json file

Jose Lopez Garcia
  • 972
  • 1
  • 9
  • 21
0

This is happening because you don't have webpack-dev-server installed as a global package, that's why you can execute directly.

The recommended way is installing it locally, in this way you'll avoid this problem.

Here you can find the steps to make it run.

Good luck

Arnold Gandarillas
  • 3,896
  • 1
  • 30
  • 36