npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.49k stars 3.17k forks source link

[BUG] bundleddepencencies does not bundle dependencies in my monorepo #7137

Open elmerbulthuis opened 10 months ago

elmerbulthuis commented 10 months ago

Is there an existing issue for this?

This issue exists in the latest npm version

Current Behavior

I have a monorepo that is setup similar something like this

The package.json in the root looks something like this:

{
  "workspaces": [
    "packages/*"
  ]
}

The packages/main/package.json looks something like this

{
  "name": "main",
  "version": "0.0.0",
  "dependencies": {
     "dependency1": "*",
     "dependency2": "*"
  },
  "bundledDependencies": [
    "dependency1",
    "dependency2"
  ]
}

When I do a npm --workspace main pack then dependency1 and dependenc2 are not bundled!

Expected Behavior

I would love to see the dependencies bundled in the package. I have a workaround that does exactly what I want:

the packages/main/package.json:

{
  "name": "main",
  "version": "0.0.0",
  "scripts": {
    "prepack": "./prepack"
  }
  "dependencies": {
     "dependency1": "*",
     "dependency2": "*"
  },
  "bundledDependencies": [
    "dependency1",
    "dependency2"
  ]
}

the packages/main/prepack:

#!/bin/sh

set -e

cd $(dirname $0)

rm --recursive --force node_modules
mkdir --parents node_modules

mkdir --parents .bundle-tmp/dependency1
mkdir --parents .bundle-tmp/dependency2

npm --workspace dependency1 pack --pack-destination .bundle-tmp/dependency1
npm --workspace dependency2 pack --pack-destination .bundle-tmp/dependency2

tar --extract --file .bundle-tmp/dependency1/dependency1-0.0.0.tgz --directory .bundle-tmp/dependency1
tar --extract --file .bundle-tmp/dependency2/dependency2-0.0.0.tgz --directory .bundle-tmp/dependency2

mv .bundle-tmp/dependency1/package node_modules/dependency1
mv .bundle-tmp/dependency2/package node_modules/dependency2

rm --recursive --force .bundle-tmp

for some reason I had to clean the node_modules folder so that only the dependencies i want to be bundled are in there. If i don't it will bundle everything in the node_modules folder. This is kind of weird as i specify what i want in the bundledDependencies key

Steps To Reproduce

Create a monorepo setup with two packages in it. One of the packages is a depencendy of the other. The dependency should be in the bundledDependencies list of the main package. Also, in main package and in the root package include a dependency that has a differnt major value. This will make the package appear in the node modules folder of the main package when installing.

Then npm pack the main packages using the --workspace parameter and voila! the dependent package will nog be in the produces .tgz file.

Environment

//registry.npmjs.org/:_authToken = (protected)

; "project" config from /home/elmerbulthuis/workspace/JsonSchema42/.npmrc

commit-hooks = false git-tag-version = false

; node bin location = /usr/bin/node ; node version = v21.5.0 ; npm local prefix = /home/elmerbulthuis/workspace/JsonSchema42 ; npm version = 10.3.0 ; cwd = /home/elmerbulthuis/workspace/JsonSchema42 ; HOME = /home/elmerbulthuis ; Run npm config ls -l to show all defaults.

mukundthakare commented 9 months ago

@elmerbulthuis

I am also facing the same issue with latest version of node.js, the buldeled dependencies are being ignored/ not considered while building the node package. however I don't face same issue with the lower version of node.js like 16.18.1.

I am using npm pack command to have locally installble tarball file. npm pack release-folder

the package.json look's like this release-folder look's like this - "peerDependencies": { "@angular/common": "^13.3.0", "@angular/core": "^13.3.0", "@angular/platform-browser": "~13.3.0", "@angular/forms": "~13.3.0", "@core-3d": "1.0.2", "@loader": "1.0.2", "@viewer": "1.0.2", "@shared-ui": "0.0.1" }, "dependencies": { "tslib": "^2.3.0" }, "bundledDependencies": [ "@core-3d", "@loader", "@viewer", "@shared-ui" ]

Let me know if you were able to fix the problem at your end.

Thank you !!