nodejs 反应:package.json 文件中的构建脚本在命令 yarn 构建时不起作用

nodejs react : build script in package.json file not working when commanding yarn build

在 linux 和 windows 上,当我在 package.json 上进行以下编辑后 运行 纱线构建命令时:

{  
"name": "chatastrophe",  
"version": "1.0.0", 
 "main": "index.js",
  "license": "MIT", 
 "scripts": {  
  "build": "node_modules/.bin/webpack",
'  }, 
 "dependencies": {   
 "react": "15.6.1",  
  "react-dom": "15.6.1", 
   "webpack": "3.5.4",
  } 
}

我不断收到此错误:

error An unexpected error occurred: "C:\Users\Richard\Desktop\chatastrophe\package.json: Unexpected token } in JSON at position 175". info If you think this is a bug, please open a bug report with the information provided in "C:\Users\Richard\Desktop\chatastrophe\yarn-error.log".

此错误的更详细版本如下(当我使用 npm 而不是 yarn 时)

npm ERR! file C:\Users\Richard\Desktop\chatastrophe\package.json
npm ERR! code EJSONPARSE
npm ERR! Failed to parse json
npm ERR! Unexpected token } in JSON at position 175 while parsing near '...webpack",
npm ERR!           },
npm ERR!         "dependen...'
npm ERR! File: C:\Users\Richard\Desktop\chatastrophe\package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! Tell the package author to fix their package.json file. JSON.parse

谁能帮帮我。提前致谢。

您的 package.json 构建脚本无效 JSON,逗号后有一个单引号。试试这个:

 {
  "name": "chatastrophe",
  "version": "1.0.0", "main": "index.js", "license": "MIT", 
  "scripts": {
    "build": "node_modules/.bin/webpack" 
  }, 
  "dependencies": {
    "react": "15.6.1",
    "react-dom": "15.6.1", "webpack": "3.5.4", 
   } 
} 

"scripts": {
        "dev": "npm run development",
        "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
        "watch-poll": "npm run watch -- --watch-poll",
        "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
        "prod": "npm run production",
        "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
    },

解决方案是更新我的 package.json 文件以包含以下脚本和依赖项:

    "build": "node scripts/copy_assets.js && node_modules/.bin/webpack --config webpack.config.prod.js",
    "start": "node_modules/.bin/webpack-dev-server",
    "deploy": "npm run build && firebase deploy"
  },
  "dependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.4",
    "file-loader": "^0.11.2",
    "fs-extra": "^4.0.1",
    "html-webpack-plugin": "^2.30.1",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-hot-loader": "3.0.0",
    "react-router-dom": "^4.2.2",
    "style-loader": "^0.18.2",
    "webpack": "^3.5.4",
    "webpack-dev-server": "^2.7.1",
    "webpack-manifest-plugin": "^1.3.1"
  },
  "devDependencies": {
    "babel-preset-stage-2": "^6.24.1"
  }
}