When specified, "proxy" in package.json must be a string.


react項目在package.json中配置proxy之后,報錯

$ npm run start

> img@0.1.0 start D:\xx\src\img
> react-scripts start

When specified, "proxy" in package.json must be a string.
Instead, the type of "proxy" was "object".
Either remove "proxy" from package.json, or make it a string.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! img@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the img@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\xx\AppData\Roaming\npm-cache\_logs\2019-09-11T08_04_08_822Z-debug.log

原因是react-scripts模塊中從2開始改變了proxy的配置方式,解決的方法有兩種

第一種方式是,指定安裝react-scripts的老版本

1.刪除node_modules/react-scripts

2.重新安裝 npm i react-scripts@1.1.1 --save

 

第二種方式是,使用新版本proxy的配置方法

1.安裝http-proxy-middleware

$ npm install http-proxy-middleware --save
$ # or
$ yarn add http-proxy-middleware --save

2.src目錄下新建文件:setupProxy.js,配置代碼為:

const proxy = require('http-proxy-middleware')
 
module.exports = function(app) {

  app.use(proxy('/api', 
    {
        "target": "https://localhost:5000/",
        "changeOrigin": true
    }))
    
    //app.use(proxy(...)) //可以配置多個代理
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM