![]() ![]() For libraries, you are encouraged to install the libraries locally, within each project you develop. Nagesh, the node.js philosophy (which I don't necessarily agree with) is that global install is only for modules that add commands to your system, not for modules that act as libraries for your own projects. I'm serious - I almost lost it a few times trying to get to the bottom of this.ĭeep thoughts by - Where Does Node.js And Require() Look For Modules? Ok - now I can start to breathe a bit easier. There, you can find a much more in-depth explanation of the search algorithm including module path caching and package descriptions. If you want to know more about Node.js's module search algorithm, check out the documentation on Modules. I write all of this in an attempt to hammer this Node.js module search overview into my head. If I log my default require.paths value to the console, here is what I get: NOTE: From what I have read, programmatically updating the "require.paths" value is considered bad practice. The paths values in this array default to the paths defined, in part, by the environmental variable, NODE_PATH but, they can be updated programmatically within a Node.js application. ![]() If it still can't find the file in this directory-spidering manner, Node.js will then proceed to look at the directory paths outlined in the "require.paths" array. Node.js will perform a hierarchical directory search for "node_modules" and "utils" in the following ways: So, for example, if you make a request to load the module, "utils": And, if it finds the named sub-directory, it will then attempt to load the file in various ways. Once Node.js finds the node_modules folder, it will then attempt to load the given module either as a (.js) Javascript file or as a named sub-directory. It will start in the current directory (relative to the currently-executing Javascript file in Node) and then work its way up the folder hierarchy, checking each level for a node_modules folder. If the given module is not a core module, Node.js will then begin to search for a directory named, "node_modules". These core modules will always take precedence in the loading algorithm. This is where I really started to lose my sanity this morning! When you refer to a Node.js module by name, Node.js uses a complex searching algorithm to locate the requested source code.įirst, Node.js looks to see if the given module is a core module - Node.js comes with many modules compiled directly into the executable binary (ex.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |