Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: prepare script is needed for installing dependencies from git using npm #3

Closed
wants to merge 1 commit into from

Conversation

trusktr
Copy link

@trusktr trusktr commented Mar 27, 2021

Hello!

The prepare script is needed in a library in order to make the library installable with npm from a git URL in a consuming project's package.json.

F.e. A consuming project would have the following to install rosbag from git:

"dependencies": {
  "rosbag": "foxglove/rosbag.js",
}

However, there's an issue with having both prepack and prepare scripts when using npm: npm will run both of them, but when npm runs prepack, devDependencies of the library being installed (f.e. devDependencies of rosbag while it is being installed from git) will not be available yet, so the build will fail.

The solution to make this work in npm is to delete the prepack script, and just use prepare.

But here's the bad part!

Last I checked, Yarn intentionally does not run prepare scripts. Instead, Yarn only runs prepack scripts, and devDependencies are available during prepack for yarn users, so installing from git with yarn works.

Basically Yarn deviated quite significantly from npm here, which makes the two totally incompatible with each other when install from git.

If yarn must be supported, then adding a prepare script and not deleting the prepack script won't work. In this case we would need to be sure to change the name of the package and publish it so npm users can install from the npm registry.

If npm users would like to install the latest from git though (while not breaking this for yarn users), they would need to clone the project, publish it under their own name, and use a package.json alias:

"dependencies": {
    "rosbag": "npm:@my-scope/rosbag@^1.6.1"
}

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@defunctzombie
Copy link
Contributor

@trusktr Sorry for not commenting on this PR sooner - I didn't get any notifications in my usual channels about it. We currently use yarn for all of our work which includes installing directly from sources in these forks so want to make sure we keep that working.

One option here might be to have prepare and prepack invoke a script that detects yarn vs npm and does the right thing depending on which was invoked.

@amacneil
Copy link
Contributor

We are gearing up to publish this package on NPM, so this will become a non-issue for anyone simply wanting to consume this library.

Without doing my own testing, but based on the PR description and also reading yarn + npm docs, it sounds like there is no simple solution that would allow both NPM and yarn users to install the package from git sources and have it automatically built (with devDependencies available during build). It might be possible to add the build step to both prepare and prepack, but wrap prepack in a script which detects and exits under npm. I assume someone on the internet has encountered and solved this issue before so if we are going to fix it, it would be nice to reuse prior art, and use the same pattern across all our libraries and our template repo.

However, given that we will be publishing the package to npm shortly, I'm not convinced that all the above work is necessary. If someone is working from a git fork then they are of course free to edit package.json to rename prepack to prepare. All other users will just use our public npm module. I therefore suggest closing this PR unless there is another obvious solution I have missed.

@amacneil
Copy link
Contributor

amacneil commented Aug 6, 2021

This package has now been published to npm as @foxglove/rosbag.

If there is a better way to use prepack & prepare to allow install from git source on both npm and yarn, we're open to suggestions.

@amacneil amacneil closed this Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants