4

I am using npm link to point to a local clone of the npm package. In the package.json file of the npm package I have set the entrypoint to dist/index.js. When I develop on the local version I don't want to rebuild the project every time. How can I solve this problem?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
otto
  • 1,815
  • 7
  • 37
  • 63
  • You can't. If you don't rebuild it, the build outputs won't get updated. – jonrsharpe Jan 05 '22 at 08:19
  • is there a best practice solution? – otto Jan 05 '22 at 08:28
  • Either write packages that don't require a build step, or rebuild them when changed. – jonrsharpe Jan 05 '22 at 08:29
  • Maybe using two different branches? One for production and one for development? – otto Jan 05 '22 at 09:19
  • I'm not sure how you think that would help, either the code is source is on both branches or you'd still need to build the source on the development branch to create the outputs for the production branch. – jonrsharpe Jan 05 '22 at 09:21
  • On the dev branch I point in package.json with the main entry to "src/index.jsx" and on the producition branch I point to "dist/index.js" – otto Jan 05 '22 at 09:37
  • That's unlikely to help because the project you're linking into will generally not be set up to try to transpile all of its dependencies (may not be _able_ to, even - it won't necessarily have the right tools and plugins to do so). Also it exposes you to the risk that it won't actually work with the build. Just **do the build**. – jonrsharpe Jan 05 '22 at 09:39
  • I am using a UI Library and I want to use live reloading in development. For now I have a subdirectory with the UI Library and relative imports. So then I think there is no accetable solution for me – otto Jan 05 '22 at 09:52
  • When you are developing, shouldn't you use [webpack dev server](https://webpack.js.org/configuration/dev-server/)? Why do you need to rebuild every time? – ikhvjs Jan 13 '22 at 16:15

1 Answers1

4

Leave the package.json file entry point set to the dist/index.js file.

Then set a new scripts option in the package.json file to run tsc <filename> -w. This will run the TypeScript Compile command with the watch flag. This will recompile when files change in the directory.

If you have a different flow that doesn't use tsc directly or doesn't support a watch flag then see the link below that usesnodemon.

https://stackoverflow.com/a/48255950/5779200


You can't avoid the build process so it's best to embrace it and understand the advantages of not changing the entry point between the deployment and the development.

  • Simple flow, just press start, and your startup script should run it on its own
  • Same output in Prod and local development means less things to break
  • Auto rebuilding on changes allow for faster development of not having to re-execute commands
Noah
  • 859
  • 7
  • 17