![]() Npm, which is bundled with Node.js, made this even easier by giving us quick and easy access to tools that others have created, which we install on our machines to access from wherever we are in our system. At first, many of us simply saw this as a way to write our application servers without needing to learn another language, but we all quickly caught on to the fact that we could also write tools for the command line that automate a lot of things in our development cycles. 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.Quick summary ↬ Node.js brought about a great revolution for JavaScript developers by allowing us to write code that runs directly on our machines our skills were no longer limited to browsers alone. CLIĬommand 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. If you want to add it as a dependency use npm install -save examplePackage or as a devDependency with npm install -save-dev examplePackage. ![]() Note: npm install examplePackage does not add examplePackage to your package.json, it simply installs it. After that you can correctly import those modules. Instead you need to run npm install, this will install all listed dependencies in package.json. As you've noticed you can specify the dependencies in the package.json, but node itself does not do anything with that information. It only looks on your hard drive, specifically in the node_modules directories as mentioned above. How does node know to search thru either your project for a package or your hard drive? 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). Although it would be possible to just install all dependencies in /, there are many advantages of having project specific dependencies. ![]() That indeed means that you can import a package, that has been installed in /, from anywhere on your file system. And so on until the module was found or the root of the file system is reached. ![]() If the module is not found in that directory, it moves to the parent directory and checks its node_modules directory. If it isn't a core module it starts looking 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. Npm install examplePackage will install the package in node_modules of the current directory. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2023
Categories |