首页 > Web开发 > 详细

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

时间:2019-09-11 16:22:01      阅读:535      评论:0      收藏:0      [点我收藏+]

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,
        "pathRewrite": {
            "/": "/"
        }
    }))
    
    //app.use(proxy(...)) //可以配置多个代理
}

 

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

原文:https://www.cnblogs.com/Netsharp/p/11506893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!