由于Yarn和npm lockfile冲突,Heroku构建失败

22

我正在尝试使用Heroku CLI在Heroku上部署一个React Web应用程序。但是,当我在项目文件夹中运行

git push heroku master

时,它会抛出以下错误:


Translated text:

我正在尝试使用Heroku CLI在Heroku上部署一个React Web应用程序。但是,当我在项目文件夹中运行

git push heroku master

时,它会抛出以下错误:

Counting objects: 213, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (212/212), done.
Writing objects: 100% (213/213), 515.89 KiB | 0 bytes/s, done.
Total 213 (delta 40), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Node.js app detected
remote: 
remote: -----> Build failed
remote:  !     Two different lockfiles found: package-lock.json and 
yarn.lock
remote: 
remote:        Both npm and yarn have created lockfiles for this 
application,
remote:        but only one can be used to install dependencies. 
Installing
remote:        dependencies using the wrong package manager can 
result in missing
remote:        packages or subtle bugs in production.
remote: 
remote:        - To use npm to install your application's 
dependencies please delete
remote:          the yarn.lock file.
remote: 
remote:          $ git rm yarn.lock
remote: 
remote:        - To use yarn to install your application's 
dependences please delete
remote:          the package-lock.json file.
remote: 
remote:          $ git rm package-lock.json
remote:     
remote:        https://kb.heroku.com/why-is-my-node-js-build-
failing-because-of-conflicting-lock-files
remote: 
remote:  !     Push rejected, failed to compile Node.js app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to MyAPP.
 remote: 
 To https://git.heroku.com/MyAPP.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 
https://git.heroku.com/MyAPP.git'

我执行了rm命令并移除了yarn.lock文件,因为我使用npm。但是依然出现了同样的错误。现在当我实际上执行 rm yarn.lock 命令时,终端会显示没有找到条目。我不知道为什么Heroku CLI坚持认为我在目录中仍有yarn lock文件。

9个回答

37

如果您使用npm

git rm yarn.lock
git commit -m "Remove yarn lock file"
git push heroku master
如果您使用 yarn
git rm package-lock.json
git commit -m "Remove npm lock file"
git push heroku master

2
根据原始问题,这是最佳答案! - Delorme Grant
这必须被标记为问题的正确答案。 - Ali Seivani

30

在将代码推送到Heroku之前,您是否已经将其提交到主分支?

对我而言,这样的问题通常出现在我做了代码更改后,然后执行'git push heroku master',但是我的主分支尚未更新,因为我还没有提交本地更改。

尝试:

git commit -m 'some changes'

然后

git push heroku master

1
为什么git-push要关心未提交的更改?我认为它应该在有或没有“一些更改”的情况下都可以。 - user3451822
这有所帮助,谢谢。 - Syam Sankar

5

删除yarn.lock和package-lock.json,然后执行以下操作:

git add --all 
git commit -a -m "Delete yarn lock and package lock"
git push 
git push heroku master

对我来说可行!


1
如果您使用npm,请遵循以下步骤:
rm yarn.lock
git add .
git commit -m 'remove yarn.lock file'
git push origin
git push heroku origin

1
我遇到了同样的问题,但在我的情况下,我正在推送包含 package-lock.json 和 yarn.lock 的主分支。我通过使用命令 git push heroku branch-name:master 来解决这个问题。

1

我遇到了同样的问题,但是上面的建议没有帮助。我所做的是删除yarn.lock(git rm yarn.lock),错误就消失了。


0
在我的情况下,我必须检出主分支并运行git push heroku main。这解决了我的问题。

0

我遇到了同样的问题。不确定在部署到Heroku时如何重新创建package-lock,但似乎是这样发生的。尝试创建一个.npmrc文件并添加package-lock=false,这对我解决了问题。


0

同时请检查是否需要构建包以便部署到Heroku。

例如,对于Meteor,您需要添加一个构建包。

heroku buildpacks:set https://github.com/AdmitHub/meteor-buildpack-horse.git

如果您有多个应用程序使用git,请确保在heroku命令中添加--app foobar作为您的应用程序名称。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接