简体   繁体   English

npm开始不适用于webpack React

[英]npm start not working with webpack React

Currently I am self teaching myself React and trying to set-up a beginning framework. 目前,我正在自学React,并尝试建立一个入门框架。 I'm having the following error after running npm start in my command line: 在命令行中运行npm start后出现以下错误:

~/projects/reactApp » webpack --display-error-details         Dustin@Dustins-MacBook-Pro
Hash: 94c3df302d8dd2990ab8
Version: webpack 2.4.1
Time: 37ms

ERROR in Entry module not found: Error: Can't resolve './main.js' in '/Users/Dustin/projects/reactApp'
resolve './main.js' in '/Users/Dustin/projects/reactApp'
  using description file: /Users/Dustin/projects/reactApp/package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: /Users/Dustin/projects/reactApp/package.json (relative path: .)
    using description file: /Users/Dustin/projects/reactApp/package.json (relative path: ./main.js)
      as directory
        /Users/Dustin/projects/reactApp/main.js doesn't exist
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        /Users/Dustin/projects/reactApp/main.js doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        /Users/Dustin/projects/reactApp/main.js.js doesn't exist
      .json
        Field 'browser' doesn't contain a valid alias configuration
        /Users/Dustin/projects/reactApp/main.js.json doesn't exist
------------------------------------------------------------
~/projects/reactApp » 

Coincidentally I get the same error when trying to load localhost:7777. 巧合的是,当尝试加载localhost:7777时出现相同的错误。 I'm not to familiar with webpack and curious what I am doing wrong. 我对Webpack不熟悉,也不知道我在做什么错。 Webpack isn't great at displaying errors, but I've managed to fix other errors up to this point. Webpack不能很好地显示错误,但是到目前为止,我已经设法修复了其他错误。

webpack.config.js webpack.config.js

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

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

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

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

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

}

module.exports = config;

package.json 的package.json

{
  "name": "reactapp",
  "version": "1.0.0",
  "description": "first React app following tutorial",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --hot"
  },
  "repository": {
    "type": "git",
    "url": "reactApp"
  },
  "keywords": [
    "react"
  ],
  "author": "Dustin Waggoner",
  "license": "ISC",
  "dependencies": {
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  },
  "devDependencies": {
    "babel-plugin-transform-react-jsx": "^6.24.1"
  }
}

I think it is a path problem, but attempted several different options to correct this. 我认为这是一个路径问题,但是尝试了几种不同的方法来纠正此问题。 Thanks for any help. 谢谢你的帮助。

Try this. 尝试这个。 resolve is mandatory while you are using none-default settings. 使用非默认设置时,必须执行resolve

Here is the documents for webpack . 这是webpack的文档

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

  output: {
    path: '/',
    filename: 'index.js'
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    inline: true,
    port: 7777
  },

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

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

}

module.exports = config;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM