无法使用自定义PUBLIC_URL构建create-react-app项目 [英] Can't build create-react-app project with custom PUBLIC_URL

查看:1739
本文介绍了无法使用自定义PUBLIC_URL构建create-react-app项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试

PUBLIC_URL=http://example.com npm run build

使用最新的create-react-script构建的项目。

with a project built using the latest create-react-script.

但是,在 public / index.html 中出现的%PUBLIC_URL%替换为空字符串,而不是预期的字符串值 PUBLIC_URL

However, the occurrences of %PUBLIC_URL% in public/index.html are replaced with an empty string, not the expected value PUBLIC_URL.

public / index.html 包含像这样的代码

<script src="%PUBLIC_URL%/static/js/jarvis.widget.min.js"></script>

搜索互联网和堆栈溢出的时间表明,关于的文章很少PUBLIC_URL 。我从GitHub克隆了create-react-app,一直在浏览代码,但尚未获得启发。

Hours of searching the internet and stack overflow show that very little is written about PUBLIC_URL. I cloned create-react-app from GitHub and have been browsing the code but have not yet been enlightened.

有人对我做错了什么吗? ?

Does anyone have any suggestions as to what I'm doing wrong?

推荐答案

如果其他答案不适合您,则还有一个主页 package.json 中的c $ c>字段。在运行 npm run build 之后,您应该收到以下消息:

If the other answers aren't working for you, there's also a homepage field in package.json. After running npm run build you should get a message like the following:

The project was built assuming it is hosted at the server root.
To override this, specify the homepage in your package.json.
For example, add this to build it for GitHub Pages:

  "homepage" : "http://myname.github.io/myapp",

您只需将其添加为 package.json 的根字段之一,例如

You would just add it as one of the root fields in package.json, e.g.

{
  // ...
  "scripts": {
    // ...
  },
  "homepage": "https://example.com"
}

成功设置后,可以通过主页 PUBLIC_URL 进行设置得到这样的消息:

When it's successfully set, either via homepage or PUBLIC_URL, you should instead get a message like this:

The project was built assuming it is hosted at https://example.com.
You can control this with the homepage field in your package.json.

这篇关于无法使用自定义PUBLIC_URL构建create-react-app项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆