简体   繁体   English

努力使用Webpack设置reactjs和babel

[英]Struggling setting up reactjs, babel using webpack

I am new to webpack and will try to learn reactJS but I find it hard to setup. 我是webpack的新手,将尝试学习reactJS,但是我发现它很难设置。 I don't have any experience in working with webpack. 我没有使用webpack的经验。

Here's what I did. 这就是我所做的。

I install dependencies which is 我安装的依赖项是

babel-loader babel-core babel-preset-es2015 babel-preset-react webpack react react-dom

After installing all the dependencies. 安装完所有依赖项之后。 I edit the package.json 我编辑package.json

Here's my package.json 这是我的package.json

{
  "name": "reactjs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "react": "^15.3.2",
    "react-dom": "^15.3.2"
  },
  "devDependencies": {
    "babel-cli": "^6.18.0",
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.7",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0",
    "webpack": "^1.13.3"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "babel": "babel", // I put babel and webpack
    "webpack": "webpack" 
  },
  "author": "",
  "license": "ISC"
}

webpack.config.js webpack.config.js

var path    = require('path');
var webpack = require('webpack');

module.exports = {
    entry: './js/app.js',
    output: {
        path: __dirname,
        filename: './js/app-dist.js'
    },
    watch: true,
    module: {
        loaders: [
            {
                test: /.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['es2015', 'react']
                }
            }
        ]
    }
}

Here's the error thrown. 这是引发的错误。

npm ERR! Windows_NT 6.1.7600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "webpack"
npm ERR! node v6.7.0
npm ERR! npm  v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! reactjs@1.0.0 webpack: `webpack`
npm ERR! Exit status 3221225501
npm ERR!
npm ERR! Failed at the reactjs@1.0.0 webpack script 'webpack'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the reactjs package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     webpack
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs reactjs
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls reactjs
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     E:\reactjs\npm-debug.log

The script compiled well but it always throw an error. 该脚本编译良好,但始终会引发错误。

Any help would be appreciated. 任何帮助,将不胜感激。 Salamat (Thanks) 萨拉玛特(谢谢)

You need not specify babel and webpack as a script command. 您无需将babelwebpack指定为脚本命令。 What you need to do is to run webpack to build your JSX code. 您需要做的是运行webpack来构建JSX代码。 Change your script to 将脚本更改为

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack -p --config webpack.config.js",
  },

and run the command npm run build and it should work for you. 并运行命令npm run build ,它应该可以为您工作。

This error is not from your side. 此错误并非来自您的身边。 Looks like, it happens only on Windows. 看起来,它仅在Windows上发生。 This is a bug with npm or node . 这是npmnode的错误。

You can check this issue https://github.com/npm/npm/issues/11024 您可以检查此问题https://github.com/npm/npm/issues/11024

It's OK, the Warriors' offensive schemes involve so much motion that it makes sense that it takes some time for you guys to get comfortable with each other. 没关系,勇士的进攻计划涉及太多动作,这很有意义,以至于你们需要一些时间才能彼此适应。 I wouldn't sweat your losses, especially since the second one was on a back-to-back. 我不会为您的损失感到汗水,特别是因为第二个是背靠背的。 Moving forward, if you're just trying to learn React, it might be useful to start with create-react-app to avoid all the scaffolding. 展望未来,如果您只是想学习React,那么从create-react-app开始可能会很有用,以避免所有的脚手架。

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

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