Why have to install webpack twice
I first install webpack global and I think it should work, but when to run webpack command at the project folder it throw error which said can't find module webpack.
I read the guide of webpack it recommend to setup webpack as the project dependence. I can't understand why has this design.
so many known issue I didn't find a similar one so create this.
You don't have to install it globally.
Local installation is better because every project could have a different webpack version.
sorry response late. for the issue I tried again today that if I only install webpack locally and run the webpack command at the project root it said webpack command is not found, it was searching the command from global package. if I only install it global it throw the error above. so I have to install webpack in two places.
Is that right, anything I missing? my platform is windows 10 with node 0.10.32.
If you installed it globally then webpack ./entry.js bundle.js should work.
Maybe you uninstalled it, and forgot? You can check like this:
npm list --depth 0 --global webpack | grep webpack
If you only have it installed locally, then you should execute:
node_modules/.bin/webpack ./entry.js bundle.js from you project root. Or you could add it to your package.json scripts like this:
"scripts" : {
"build" : "webpack ./entry.js bundle.js"
}
and execute: npm run build. This will still run the local version because npm will first look in ./node_modules/.bin/
This is not so much a webpack issue as an npm feature. It's just the way npm works.
I hope I understood your question and answered it correctly.
thanks for your feekback @devboell, it is clearly from your answer, with either global or local copy I need to run command in specify format, and seems it only compile one file each time not full compile, that may not good for big project which contain many export files. What I expected is if I install webpack globally, I can run 'webpack' command at the project root folder, then it will read webpack-config.js file and execute full compile logic, in that way I can share the same webpack copy across different projects and make the compile much easy. With local copy it just a javascript lib, and need npm command to run each compile, also bring more work I think.
sure, I just used the simplest example from webpack docs. But even in this example it will not just compile entry.js, it will compile every file that is required/imported by entry.js, ie. the entire dependency tree. Of course in a real project you would use webpack.config,js. The reason why people recommend using the local copy, has to do with managing versions. And as I said, this is not webpack specific, it is how npm has been designed. It works really well.
I suggest you read some more on npm and webpack and experiment with it. Good luck.
well, node_modules/.bin/webpack ./entry.js bundle.js that stuff is needed in the documentation where it shows how to use webpack globally but says we should only install it locally.
and seems it only compile one file each time not full compile, that may not good for big project which contain many export files.
That's not entirely true. webpack is a powerful tool but highly depends on how you use it. Lot's of projects are using it to generate a complex website/web app, not just compile one file at a time.
What I expected is if I install webpack globally, I can run 'webpack' command at the project root folder, then it will read webpack-config.js file and execute full compile logic, in that way I can share the same webpack copy across different projects and make the compile much easy.
You probably had it wrong regarding how node.js works. Installing global package is not recommended if you have multiple projects to run. And certainly lots of CLI tools are working as the same way webpack does, reading each individual configuration files per project, not globally. Because each projects are different. If you have similar projects, you can either copy them around or create a git branch to achieve the result.
With local copy it just a javascript lib, and need npm command to run each compile, also bring more work I think.
webpack IS a npm package after all. If that's not what you expect, you probably got it wrong from the beginning.