简体   繁体   English

如何在项目中使用fork版本的npm包

[英]How to use fork version of npm package in a project

Let's say there is an npm package called abcd . 假设有一个名为abcd的npm包。 Normally in package.json , we specified the dependencies as "abcd": "^1.0.0", 通常在package.json ,我们将依赖项指定为"abcd": "^1.0.0",

But this abcd does not work as expected, so I forked (and modified) it inside https://github.com/mygithubid/abcd 但是这个abcd没有按预期工作,所以我在https://github.com/mygithubid/abcd中分叉(并修改)它

Then I run npm install git+https://git@github.com/mygithubid/abcd.git and in package.json , the definition is changed to "abcd": "git+https://github.com/mygithubid/abcd.git", 然后我运行npm install git+https://git@github.com/mygithubid/abcd.git ,在package.json ,定义更改为"abcd": "git+https://github.com/mygithubid/abcd.git",

After restarting the project that using this abcd , now it throws error Module not found: Can't resolve 'abcd' ... even though I saw the abcd folder is added inside node_modules 重新启动使用此abcd的项目后,现在它抛出错误Module not found: Can't resolve 'abcd' ...即使我看到在node_modules添加了abcd文件夹

Could you advise the mistake I made in above? 你能告诉我上面的错误吗? Thanks! 谢谢!

One fairly clean option is to use patch-package : https://www.npmjs.com/package/patch-package 一个相当干净的选择是使用patch-packagehttps//www.npmjs.com/package/patch-package

If the people using your project might use either npm or yarn, then remeber to make the patch available for both. 如果使用您的项目的人可能使用npm或纱线,那么请记住使补丁可用于两者。 More info under patch-package --use-yarn . patch-package --use-yarn下的更多信息patch-package --use-yarn

Patch or fork? 补丁或叉子? See https://www.npmjs.com/package/patch-package#benefits-of-patching-over-forking 请参阅https://www.npmjs.com/package/patch-package#benefits-of-patching-over-forking

Be sure that the github repository contains compiled files, at most cases you need build the package for npm first, the compiled files push only to NPM. 确保github存储库包含已编译的文件,在大多数情况下,您需要首先为npm构建包,编译的文件仅推送到NPM。 To find out how to build the package check the package.json file 要了解如何构建包,请检查package.json文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM