How to pack a snapshot of a local dependency in a published package

Viewed 589

In a JavaScript monorepo managed with Rush, I have three projects:

  • backend/
  • frontend/
  • shared/

In backend/package.json and frontend/package.json, the shared project is declared as a dependency:

  "dependencies": {
    "shared": "0.0.0",

In the local environment, everything works fine, thanks to Rush, which runs npm link for me.

Now I need to publish.

On the frontend project, the command rushx build (like npm run build) creates JS and CSS bundles with the help of webpack, and puts them in the backend/ directory.

I would like to publish the backend project as a real package on npmjs. I don't want to publish shared as a distinct package because in the published version it is not shared anymore. The shared content is already bundled in the frontend. I would like to embed it into the backend too.

I tried to pack it:

cd backend/
npm pack ../shared
# A new file is created: 'shared-0.0.0.tgz'

Then I edited backend/package.json:

  "dependencies": {
    "shared": "shared-0.0.0.tgz",

But after that the generated package doesn't work. When I execute npm install my-published-package, it tries to find shared-0.0.0.tgz in the current directory instead of the installed package directory.

Is there an elegant solution?

1 Answers

Try specifying the full relative path to shared-0.0.0.tgz, as documeted here:

  "dependencies": {
    "shared": : "file:~/some/relative/path/shared-0.0.0.tgz"

The valid relative paths can be anything like these (use whatever is appropriate for your structure):

../foo/bar
~/foo/bar
./foo/bar
/foo/bar

Then do rush install or rush update to process the change.

If that doesn't work, try saving the shared package with npm install --save first.

If that doesn't work, bundledDependencies may be the answer. I'll build up a test project tonight to try it out and update later, but this may get you on your way sooner.

Related