nx
nx copied to clipboard
nx publish runs twice
Current Behavior
if I run nx publish without a publish script, it gives me > NX Cannot find configuration for task packagename:publish
if I add "publish": "npm publish" to the scripts, it attempts to publish twice and then gives me an error.
Expected Behavior
I expect it to execute once
GitHub Repo
https://github.com/dragonizedpizza/uniboard
Steps to Reproduce
- add "publish": "npm publish" to your scripts
- run nx publish package name
Nx Report
Node : 20.0.0
OS : darwin x64
pnpm : 8.4.0
Hasher : Native
nx : 16.3.2
@nrwl/tao : 16.3.2
nx-cloud : 16.0.5
typescript : 5.1.3
Failure Logs
https://cloud.nx.app/runs/tQX2LfCVel
Operating System
- [X] macOS
- [ ] Linux
- [ ] Windows
- [ ] Other (Please specify)
Additional Information
No response
This is due to how npm works.
scripts: {
"publish": "npm publish"
}
The publish script in package.json is a lifecycle for npm publish.
Nx will run nx publish package1 as npm run publish in the project's root.
Running npm run publish (even manually) in the project's root.. will do the following:
- See the
publishscript inpackage.json(npm publish) and execute it - Run
npm publishwhich will then run the life cycles - It will run the
publishscript inpackage.jsonagain... which again isnpm publishwhich will cause an infinite loop
So I don't think there is a bug with Nx as it replicates what happens via npm.
Okay, so how do you setup your publishing right?
- You could stick with pnpm workspaces (or
npmandyarn) which all have some base handling for publishing multiple packages. a. You wouldn't add thepublishnpm script inpackage.jsonbut you would just callpnpm publishin the root. - You could something like
lernawhich has better handling for publishing multiple packages. - You could continue to use Nx and rename the
publishpackage.jsonscript to something else that is not anpmlifecycle such aspublish-package. - You could write a different script such as like
tools/scripts/publish-package.jsif there's some special handling you would like to do. If you usenx g @nx/js:library lib2 --publishable --importPath lib2you will see a similar setup. - There are many other ways as well
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.