Skip to content

[BUG] npm publish <package-spec> in a workspace is not taking package-spec into account #7726

@Badisi

Description

@Badisi

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

Considering the following mono-repo workspace:

my-project
├── dist
│   └── a
│       └── src
│       │    ├── file.d.ts
│       │    └── file.js
│       └── package.json # { "name: "a", "version": "1.0.0" }
├── projects
│   └── a
│       └── src
│       │    └── file.ts
│       └── package.json # { "name: "a", "version": "1.0.0" }
└── package.json # { "name: "my-project", "workspaces": [ "projects/a" ] }

When releasing a new project the idea is to:

  1. Set the working-directory to the desired project (ex: cd projects/a)
  2. Build the project into the root dist folder (ex: npm run build)
  3. Publish the project (ex: npm run release which runs npm publish ../../dist/a --access=public)

Issue

When specifying workspaces in the root package.json, the step 3 does not work as expected.

It ignores the <package-spec> parameter completely and publish the files in the current directory instead:

$ npm run release

> [email protected] publish
> npm publish ../../dist/a --dry-run

npm notice
npm notice 📦  [email protected]
npm notice Tarball Contents
npm notice 172B package.json
npm notice 0B src/file.ts
npm notice Tarball Details
npm notice name: a
npm notice version: 1.0.0
npm notice filename: a-1.0.0.tgz
...

The <package-spec> parameter has no effect at all. Even a wrong path will not display an error and publish the current directory instead (ex: npm publish ./unknown-dir --dry-run).

Note

The pack command on the contrary is working as expected and ignores the workspace:

$ npm pack ../../dist/a --dry-run

npm warn Ignoring workspaces for specified package(s)
npm notice
npm notice 📦  [email protected]
npm notice Tarball Contents
npm notice 44B package.json
npm notice 0B src/file.d.ts
npm notice 0B src/file.js
npm notice Tarball Details
npm notice name: a
npm notice version: 1.0.0
npm notice filename: a-1.0.0.tgz
...

Related

I had opened this issue, 2 years ago, that sounded similar at first, because npm publish <package-spec> was also not working as expected. But after looking at it again, it is the exact same problem, because I'm using cwd to navigate to a project and run the publish from there.

Expected Behavior

I would expect the command npm publish <package-spec> to publish what's located at the given path argument and not what's in the current directory.

Steps To Reproduce

  1. git clone https://github.com/Badisi/issue-npm-publish-with-workspaces-2
  2. npm install
  3. cd projects/a
  4. npm run release

Environment

  • platform: macOS Monterey 12.7.5
  • node: v20.15.1
  • npm: 10.8.2

Metadata

Metadata

Assignees

Labels

Bugthing that needs fixingPriority 2secondary priority issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions