node.js NPM 启动未启动本地服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40478608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:48:50  来源:igfitidea点击:

NPM Start not starting local server

node.jsreactjsnpmwebpack

提问by Dheeraj Agrawal

I am trying to make an react app using webpack and when I try to run npm start it should load http://localhost:3333but it says site cannot be reached, here is my webpack config:

我正在尝试使用 webpack 制作一个 React 应用程序,当我尝试运行 npm start 时,它应该加载http://localhost:3333但它说无法访问站点,这是我的 webpack 配置:

module.exports = {
    entry: './main.js',
    output: {
        path: '/',
        filename: 'index.js'
    },
    devServer: {
        inline: true,
        port: 3333
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel',
                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    }
}

And here is my script object from package.json: "start": "webpack-dev-server". I have already installed webpack & webpack-dev-server globally. Check below image which I am getting: enter image description here

这里是我的脚本对象来自package.json"start": "webpack-dev-server"。我已经在全球范围内安装了 webpack 和 webpack-dev-server。检查下面我得到的图像: 在此处输入图片说明

Edit: My package.json:

编辑:我的 package.json:

{
  "name": "react-app",
  "version": "1.0.0",
  "description": "sample",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/dheeraja00/react-app.git"
  },
  "author": "Dheeraj Agrawal",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/dheeraja00/react-app/issues"
  },
  "homepage": "https://github.comdheeraja00/react-app#readme",
  "dependencies": {
    "material-design-lite": "^1.2.1",
    "react": "^15.3.2"
  },
  "devDependencies": {
    "babel-core": "^6.18.0",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "css-loader": "^0.25.0",
    "file-loader": "^0.9.0",
    "node-sass": "^3.10.1",
    "raw-loader": "^0.5.1",
    "sass-loader": "^4.0.2",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.16.2"
  }
}

回答by AJS

npm startwill only works when you have start script.

npm start只有当你有启动脚本时才有效。

For the below example webpack-dev-serverand webpackpackages are required. For adding these packages you should install webpack-dev-serverand webpackglobally.

对于下面的示例webpack-dev-serverwebpack软件包是必需的。添加这些软件包,你应该安装webpack-dev-serverwebpack全球。

npm install webpack-dev-server webpack -g

For Example:

例如:

"scripts": {
    "start": "webpack-dev-server"
 }

in you package.json, basically when you run npm start it searches your package.jsonfor what to do.

package.json 中,基本上当您运行 npm start 时,它会搜索您的package.json以了解要执行的操作。

回答by Deepak Kumrawat

You can run any one of the below mentioned commands to start the node server for your reactJs application:

您可以运行以下任一命令来为您的 reactJs 应用程序启动节点服务器:

 npm run-script start
 npm run start
 npm start

回答by Wigglemaster

Answering as this happened to me with a new installation of Ubuntu 18.04 where localhostdid not exist in my hosts file and had to be added.

localhost在我的主机文件中不存在并且必须添加的 Ubuntu 18.04 的新安装中回答这发生在我身上。

You can check if this is your issue by navigating the browser to 127.0.0.1:3333instead. If it is available you need to add the following line to your /etc/hostsfile

您可以通过将浏览器导航到127.0.0.1:3333来检查这是否是您的问题。如果可用,您需要将以下行添加到您的/etc/hosts文件中

127.0.0.1 localhost