1

I have a React app that is built using npm.

To build the app I can run...

npm run build

... and npm successfully bundles the javascript and assets into a ./build folder.

I can run...

npm start

...from the root of the app and npm will fire up a web server and I can run the app.

As an aside, the app is deployed as as a browser extension at the moment but I don't think this is relevant.

I want to be able to package the react app as an npm module and include it in another application (so that the same code is run in our browser extension and our native web application). This seems like an extremely simple idea but in practise I'm struggling to make it work.

I have found the running..

npm pack

in the root folder produces a tgz file. I can then call npm install <path_to_root>\the-tgz-file.tgz and that seems to successfully install the module in our native web app. This handles the node dependencies but it installs the full root directory of our React App. I only want to package the bundled, minified contents of our build directory (not the raw source files) but I do want npm to handle dependencies for me.

Surely I'm missing something really simple! Help!

  • You may also want to add a `prepack` script that runs the build script, so you always pack up-to-date outputs. – jonrsharpe Jan 03 '20 at 14:03
  • @jonrsharpe - I think it might answer my question. The answer you've indicated suggests that I can control what goes into the package by creating a .npmignore file. I'll give this a go and see if I can get it to work. Do you know if this is a recommended approach for npm packages (or is there an expectation that packages *should* include src files)? – Matt Austin Jan 03 '20 at 14:15
  • Apologies if this is a silly question, but does "npm pack" produce the same output as "npm publish" but without publishing the package? This was my understanding, in which case I'm surprised that so much of the folder structure is included in the output. Am I really misunderstanding something fundamental? – Matt Austin Jan 03 '20 at 14:24
  • [*"All files in the package directory are included if no local `.gitignore` or `.npmignore` file exists."*](https://docs.npmjs.com/cli/publish) I'd recommend the `files` approach, as that's a safe-list. – jonrsharpe Jan 03 '20 at 14:25

0 Answers0