3

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?

Paleo
  • 21,831
  • 4
  • 65
  • 76

1 Answers1

0

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.

Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • 1
    Interesting. I didn't know `bundleddependencies`. This may be the way to go indeed. – Paleo Jul 08 '20 at 21:25
  • I tested with `bundledDependencies`. It works but there is a huge issue: `shared` is not correctly packed. It is just bundled _as it is_, with source files, `.rush/` and `node_modules/` directories etc. The script `prepublish` of `shared/package.json` is ignored. The file `.npmignore` and the `"files"` section in `package.json` as well. Under these conditions, I can't use this feature. – Paleo Jul 11 '20 at 12:41
  • Try moving the `prepublish` script to `prepack`. – Pedreiro Jul 15 '20 at 12:49