How can I publish library from dist directory saving node_modules?

28 Views Asked by At

I want to publish npm package and have next type of import: import { Component } from "my-lybrary/Component"

I found out that I have to copy my package.json to dist directory and run npm publish ./dist. Well, it works, but final package doesn't have directory node_modules, that I have in dist after building. How can I save node_modules directory?

  1. I tried to add !node_modules/ at .npmignore
  2. I tried to publish from root directory (my package.json has files: ["dist"], but in this case I have incorrect structure:
  • my-lybrary --dist ----component

instead of

  • my-lybrary --component
1

There are 1 best solutions below

0
Victoria Hill On

Publishing node_modules within your package isn't typical or recommended due to potential bloat and dependency conflicts. Instead, list your dependencies in package.json. Consumers installing your package will have these dependencies resolved automatically. If you need specific modules within your package, consider bundling them into your distribution files using tools like Webpack or Rollup. This way, you maintain a clean package structure and prevent issues related to directly including node_modules.