Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global webpack install vs. project specific webpack install

I'm somewhat confused with how webpack works. If you run npm install examplePackage in your root director, does that install the package to your hard drive? Then does that mean you can import that package anytime in any file? How is this different than running the npm install examplePackage inside your project instead? Is it because then when you push your project to Github, there will be a package.json for other people to follow and make sure they have the necessary libraries to run your app as well? How does node know to search thru either your project for a package or your hard drive? Does it just check your library first and then if it doesn't find it, it'll look in your hard drive?

like image 854
stackjlei Avatar asked Feb 14 '17 07:02

stackjlei


1 Answers

npm install examplePackage will install the package in node_modules of the current directory. When you import a module in Node.js with require('examplePackage') it first checks if it's a core module (e.g. fs). If it isn't a core module it starts looking in node_modules of the current directory. If the module is not found in that directory, it moves to the parent directory and checks its node_modules directory. And so on until the module was found or the root of the file system is reached. See https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

That indeed means that you can import a package, that has been installed in /, from anywhere on your file system. Although it would be possible to just install all dependencies in /, there are many advantages of having project specific dependencies. Probably the biggest advantage is that you can easily share your project with others and you can be sure that it will work for them (assuming a compatible Node.js version).

How does node know to search thru either your project for a package or your hard drive?

It only looks on your hard drive, specifically in the node_modules directories as mentioned above. As you've noticed you can specify the dependencies in the package.json, but node itself does not do anything with that information. Instead you need to run npm install, this will install all listed dependencies in package.json. After that you can correctly import those modules.

Note: npm install examplePackage does not add examplePackage to your package.json, it simply installs it. If you want to add it as a dependency use npm install --save examplePackage or as a devDependency with npm install --save-dev examplePackage. https://docs.npmjs.com/cli/install

CLI

Command line tools or generally packages with executables, such as webpack, are available in node_modules/.bin and in order to execute them, they have to be either in your shell's PATH or called directly. Because it's very common that a project calls locally installed executables, you can use them in the scripts section of the package.json without prefixing them with node_modules/.bin. For instance to run webpack you could define a script:

"scripts": {
  "build": "webpack"
}

And then run npm run build instead of having to run ./node_modules/.bin/webpack. More information on scripts: https://docs.npmjs.com/misc/scripts

like image 169
Michael Jungo Avatar answered Sep 21 '22 16:09

Michael Jungo