nx icon indicating copy to clipboard operation
nx copied to clipboard

nx publish runs twice

Open Dragonizedpizza opened this issue 1 year ago • 1 comments

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

  1. add "publish": "npm publish" to your scripts
  2. 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

Dragonizedpizza avatar Jun 13 '23 08:06 Dragonizedpizza

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:

  1. See the publish script in package.json (npm publish) and execute it
  2. Run npm publish which will then run the life cycles
  3. It will run the publish script in package.json again... which again is npm publish which 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?

  1. You could stick with pnpm workspaces (or npm and yarn) which all have some base handling for publishing multiple packages. a. You wouldn't add the publish npm script in package.json but you would just call pnpm publish in the root.
  2. You could something like lerna which has better handling for publishing multiple packages.
  3. You could continue to use Nx and rename the publish package.json script to something else that is not a npm lifecycle such as publish-package.
  4. You could write a different script such as like tools/scripts/publish-package.js if there's some special handling you would like to do. If you use nx g @nx/js:library lib2 --publishable --importPath lib2 you will see a similar setup.
  5. There are many other ways as well

FrozenPandaz avatar Jun 15 '23 22:06 FrozenPandaz

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! 🙏

github-actions[bot] avatar Jul 31 '23 00:07 github-actions[bot]

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.

github-actions[bot] avatar Sep 14 '23 00:09 github-actions[bot]