-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarification on using dependencies or devDependencies #520
Comments
I think, this answer can be useful in your case. This question doesn't relate to webpack, but for any npm package. |
Sorry, I don't see how this question relates to webpack. |
I agree with @dschissler that everybody is doing this wrong. A browser app built by webpack has no runtime node dependencies, and thus all frontend dependencies should be listed as devDependencies. The The way I ran into this was which of course failed because none of the devDependencies were downloaded by npm install, as described in the npm install docs. I am exposing NODE_ENV=production to webpack, because React inspects that environment variable, perhaps this is an issue with react, I don't fully understand. |
@dustingetz I'm a bit confused by your response. You say "all frontend dependencies should be listed as devDependencies" but then you said "...none of the devDependencies were downloaded by npm install" when you set |
@dkreft I agree. Everything should just go into |
@dkreft @pke You guys are right that @dustingetz doesn't address that big issue but you're solution doesn't address an issue that I will have. So I have a PHP framework and I'm using Webpack to build front end resources as explained above. Then eventually I will use a production build to run some Nodejs websocket tech and I'd rather not download all of the webpack stuff for this purely Nodejs production server. Its possible that it should be a separate package.json for that part of the code but that would complicate other matters. So I'm considering to export |
it's harmless to have them as devDeps, but it's technically incorrect. see: webpack/webpack#520
This conversation states why it works, but leaves one unsure of the reasoning behind it. Webpack projects seem to add a layer of confusion, but we are simply not asking ourselves the right questions: "How will this project be used in production?", and "Will we run webpack during production?" The argument to put most dependencies under Scenario 1: If your project needs to run webpack in the production environment before serving: Packages required to build the webpack project are under In this scenario, our project is no longer considered the "front end app", but instead "the project that builds the front end app". Assuming statement above to be reasonable, Scenario 2: If our project does not need to run webpack in the production environment, and has a pre-built copy to serve (or require by other projects): This scenario is if we do not need to run webpack in production and we only serve the pre-built files in a node server. This means our project will need to ship with our build folder. The webpack and webpack app dependencies can be placed in This scenario is also used if we are creating library packages, instead servers. Others will install the project as a package, and require the pre-built files in their project. If one wishes to help develop our package, then they can clone and install with devDependencies to enable webpack building. A caveat to this scenario is that for production, one must have already built using webpack in a development environment. Summary: Webpack projects should be considered build-type projects first, and not production-type projects, unless webpack is used in production. Packages needed during production goes in Of course, one can always put everything in That's it! That's all you need to know! Whichever scenario you find yourself in, I would love to know if there are any other pitfalls and caveats! Supplementary information:
This quote is true and untrue. A browser app built by webpack has no node dependencies. However, many projects not only serve but also build during production. It is true that this is an unconventional use of the word "build" for node developers (in scenario 1), as building typically refers to doing it before production instead of during. Instead of build and run in separate commands, it's more like "build-to-run" in one. Sorry for the long text, probably should have been a blog post, but I think this deserves an explanation somewhere on the internet! |
@pbrn46 In what circumstance does one have to run webpack in production before serving? I have a build server that runs webpack and create the bundles, then I copy these distibution files to the server. So at this point in time I do not need to run webpack yet again in prod. Who would run webpack in prod at runtime before serving, isn't that really ineffcient? And in my case should everything go under devDependencies? Of course for development I run locally. Is there any impact on the bundles based on what is in dependencies vs devDependencies? |
@jrmcdona For your current situation, and as a general recommendation for everyone, move everything into Since you are copying your files to a production server, and the project itself doesn't serve in production, then it is recommended to use Supplemental informationWebpack in production Running webpack in production should actually be a special use case. This is where your package is actually meant for the end user to webpack (implicitly) with your package. The best example would be if you are creating something like the Efficiency In terms of efficiency for running webpack in production (as a server), it would only be the first build that takes time, as the outputs of webpack are static files after the build. The files are not rebuilt every time a client sends a request (although this can actually be a way webpack is used for the super-advanced-evil-masterminds). For the lazy people who have their server and build in the same project, the usual setup is on every change they would crash the server, run webpack, and restart the server (for example using Redistributing your package for others to program or build This conversation is actually only applicable if you want to redistribute the project. Interestingly enough, if you redistribute your project just for people to build, then you need your webpack and build dependencies (like loaders, babel, sass, etc) should be in Now, if you distribute your project with the pre-built files and the only use is for others to import/include your pre-builts, then everything should be in |
@pbrn46 can you state that in like two sentences so i can understand what you're talking about, thanks |
@dustingetz Haha, sorry for the long text. Prone to being unspecific to use case, the simple answer is I agree webpack should belong in |
@pbrn46 maybe I'm late to the party, but IMHO in So e.g. yarn you can use with So from that point of view |
Eh, then |
@at-daonguyen you got it right, |
I think everything that is needed to create the bundle.js belongs in |
At the same time, in my opinion, everything that is bundled into the distribution should not be in dependencies in most scenarios. https://github.com/vuejs/vue/blob/dev/package.json ..they have 0(!) dependencies, only devDependencies. The dev dependencies are a lot of front-end libraries that people tend to reference as ordinary dependencies. However since the distribution is based around a pre-built bundle vue.js/react.js are doing it exactly the right way in my opinion. was this thread conclusive?
|
Yes, I think in general the best practice is to add |
You said it yourself, npm start is for development - and starts the webpack-dev-server that's not intended to be used for production; so why should it be available when installing production dependencies only? |
@naeramarth7 Because But yeah, anyway, one could argue that its up to the developer to decide which script should be used in which scenario, because its not clearly defined. |
* According to webpack/webpack#520 (comment), all deps should reside in `dependencies` * This also improves compatibility with the default behavior of `npm install x`
Based on the following comment webpack/webpack#520 (comment)
@pbrn46 You didn't mention the Following your explanation for Case 1, which consists in acknowledging the project as "a project that builds an app" rather than "an app", and furthermore having the unit testing packages under Said scenario is building my app, specifically into a docker image but I wouldn't limit it to just that, for I don't care about testing dependencies when I'm building static assets for my app and bundling it into a docker image. Great example btw, @pbrn46 |
@chris-fran That approach is perfectly valid. The only drawback of that approach is that you require your dependents to always build at least the optimized --prod build themselves. It would not make sense to include those generated assets in the distribution of that package, because then they would depend on all your build dependencies. |
As @dustingetz suggests, it's very common to need to set
or
https://packtracker.io/blog/devdependencies-are-not-installing-with-npm/ |
- Use package-lock.json through command: npm ci - Fixes regarding Docker Compose setup - Multistage build to get only what is needed for PRD image Useful links - https://git.io/JebrQ - webpack/webpack#520 (comment)
- Use package-lock.json through command: npm ci - Fixes regarding Docker Compose setup - Multistage build to get only what is needed for PRD image Useful links - https://git.io/JebrQ - webpack/webpack#520 (comment)
@pbrn46 hello, thanks for your clear answers, one cuestion. I i'm working with react app with a mock db generated in start script and a jason-server. I'm playing a little with heroku, trying to connect my GH repo with cd/ci heroku workflow, so every time that I push to master heroku re-build project, so should I have webpack, json-server and babel as prod dependencies right? |
Haven't read everything but to me the question is more relevant when you dev a library rather than an application. Library - the library is reused. People don't need your dev deps (ie testing library, etc...) |
I've read through the comments here, but it seems like no one mentioned something that I thought makes it very useful to separate deps into Scenario
Use CaseWhen building your app pre-production, you use |
For us this was a problem because our client/server code shared one package.json. This was making our server host a bunch of client side dependencies that were already taken care of at build time. Basically we only needed 5/25 dependencies on our server. The solution to this was to separate our client and server into separate directories and have their own package.json. Then actual client side dependencies could still be listed as |
I'm looking for clarification on using dependencies or devDependencies. For example, I'm using backbone through npm and I have it under devDependencies. My final build does not require any node environment so I'm wondering what the best convention is for this. It practically doesn't seem to make much of a difference but it would be great to understand the best case convention.
The text was updated successfully, but these errors were encountered: