设置 ReactJs 时出错

Posted

技术标签:

【中文标题】设置 ReactJs 时出错【英文标题】:Error in setting up ReactJs 【发布时间】:2017-12-19 12:43:12 【问题描述】:

我是 reactJs 和为 reactJs 应用程序设置环境的新手。我有这样的包,json文件


 "name": "reactjs-basics",
  "version": "1.0.0",
 "description": "Some basic ReactJS",
 "main": "index.js",
 "scripts": 
 "start": "npm run build",
 "build": "webpack -d && copy src/index.html dist/index.html && webpack-dev-
  server --content-base src/ --inline --hot",
"build:prod": "webpack -p && copy src/index.html dist/index.html"
        ,
 "keywords": [
 "reactjs"
      ],
 "author": "Maximilian Schwarzmueller",
 "license": "MIT",
 "dependencies": 
 "react": "^15.2.1",
"react-dom": "^15.2.1"
     ,
"devDependencies": 
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-2": "^6.11.0",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
    
 

和这样的 web.config.js 文件

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

var DIST_DIR = path.resolve(__dirname, "dist");
var SRC_DIR = path.resolve(__dirname, "src");

var config = 
entry: SRC_DIR + "/app/index.js",
output: 
    path: DIST_DIR + "/app",
    filename: "bundle.js",
    publicPath: "/app/"
,
module: 
    loaders: [
        
            test: /\.js?/,
            include: SRC_DIR,
            loader: "babel-loader",
            query: 
                presets: ["react", "es2015", "stage-2"]
            
        
    ]
  
 ;

module.exports = 配置;

当我运行这个命令“npm start”然后我得到这个错误

"ERROR in Cannot find module 'babel-core'
 The syntax of the command is incorrect.

 npm ERR! Windows_NT 10.0.14393
 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program  
 Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
 npm ERR! node v6.10.0
 npm ERR! npm  v3.10.10
 npm ERR! code ELIFECYCLE
 npm ERR! reactjs-basics@1.0.0 build: `webpack -d && copy src/index.html 
 dist/index.html && webpack-dev-server --content-base src/ --inline --hot`
 npm ERR! Exit status 1
 npm ERR!
 npm ERR! Failed at the reactjs-basics@1.0.0 build script 'webpack -d && 
 copy src/index.html dist/index.html && webpack-dev-server --content-base 
 src/ --inline --hot'.
 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-basics 
 package,
 npm ERR! not with npm itself.
 npm ERR! Tell the author that this fails on your system:
 npm ERR!     webpack -d && copy src/index.html dist/index.html && webpack- 
 dev-server --content-base src/ --inline --hot
 npm ERR! You can get information on how to open an issue for this project 
 with:
 npm ERR!     npm bugs reactjs-basics
 npm ERR! Or if that isn't available, you can get their info via:
 npm ERR!     npm owner ls reactjs-basics
 npm ERR! There is likely additional logging output above.

 npm ERR! Please include the following file with any support request:
 npm ERR!     C:\Users\fahad\Downloads\reactjs-basics-master\reactjs-basics- 
 master\npm-debug.log

 npm ERR! Windows_NT 10.0.14393
 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program 
 Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
 npm ERR! node v6.10.0
 npm ERR! npm  v3.10.10
 npm ERR! code ELIFECYCLE
 npm ERR! reactjs-basics@1.0.0 start: `npm run build`
 npm ERR! Exit status 1
 npm ERR!
 npm ERR! Failed at the reactjs-basics@1.0.0 start script 'npm run build'.
 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-basics 
 package,
 npm ERR! not with npm itself.
 npm ERR! Tell the author that this fails on your system:
 npm ERR!     npm run build
 npm ERR! You can get information on how to open an issue for this project 
 with:
 npm ERR!     npm bugs reactjs-basics
 npm ERR! Or if that isn't available, you can get their info via:
 npm ERR!     npm owner ls reactjs-basics
 npm ERR! There is likely additional logging output above.

 npm ERR! Please include the following file with any support request:
 npm ERR!     C:\Users\fahad\Downloads\reactjs-basics-master\reactjs-basics- 
 master\npm-debug.log"

我该如何解决这个问题?我不知道为什么这会引发错误,请查看此内容并让我知道究竟是什么错误。

【问题讨论】:

先试试npm install 我试过 npm install,npm install babel-core --save-dev。但仍然出现我提到的错误 ERROR in Cannot find module 'babel-core'. using react.js, webpack, and express server的可能重复 @MayankShukla 没有适合我的解决方案。 如果您解决了它,您应该提及您的解决方案,以便其他可能遇到类似问题的人受益。 【参考方案1】:

webpack.config.js 中使用“\”代替“/”。

它使用这个工作:

"build": "webpack -d && copy src\\index.html dist\\index.html && webpack-dev-
server --content-base src/ --inline --hot"

【讨论】:

以上是关于设置 ReactJs 时出错的主要内容,如果未能解决你的问题,请参考以下文章

安装reactjs时出错:解析附近时JSON输入意外结束

在 ReactJS 应用程序中将字符串转换为 JSON 对象时出错

当reducer在我的功能组件reactjs中出错时,我想清除或初始状态

如果响应在模态中出错,则 ReactJS 引导警报

ReactJs:在使用动态 Taglist 时使用道具设置初始状态

Reactjs-createStore在使用打字稿时无法识别减速器