Requiring a folder - Deepstash

Requiring a folder

Create a Find-Me folder under Node_Modules and place an Index.Js file in there. The same require ('Find-Me') line will use that folder’s Index.Js file. We can control what file name to start with under the folder using the main property in Package.Json.

4 STASHED

MORE IDEAS FROM THEARTICLE

Learning React or Node? Checkout my books: 

  • Learn React.js by Building Games  
  • Node.js Beyond the Basics  

4 STASHED

We want to use this file in two ways: 

  • From the command line directly like this: 

4 STASHED

Node wraps the Module code in a function, which we can inspect using the wrapper property of the Module Module. Exports is defined as a reference to Module.Exports prior to that. Require and Module are both specific to the function to be executed, and __Filename /_Dirname variables will contain the wrapped Module’s absolute Filename and directory path. This wrapper function has 5 arguments: exports, require, Module, __Filename,

4 STASHED

If a file extension was not specified, the first thing Node will try to resolve is A.Js file. If it can’t find A.Json file, it will try A.Json file and it will Parse The.Json file if found as a Json text file. After that, it will try to find a Binary.Node file. To remove ambiguity,

4 STASHED

Module1 requires Module2, and Module2 requires Module1 while it wasn’t fully loaded. A property was reported because both B and C were exported after Module2 required and printed Module1. Node keeps this really simple: during the loading of a Module, it builds the exports object.

4 STASHED

If you’ve noticed above, every time we’ve printed a Module object, it had an exports property which has been an empty object so far. So far, every time we printed a Module object, we saw a loaded attribute on that object with a value of false. The Module Module uses the loaded attribute to track which modules have been loaded (true value) and which modules are still being loaded (false value) we can, for example, see the Index.Js

4 STASHED

Create a Lib/Util.Js file and add a Console.Log line there to identify it. Also, Console.Log the Module object itself. Do the same for an Index.Js file, which is what we’ll be executing with Node: NOTE how the main index Module (Id:' .') is now listed as the parent for the Lib/Util Module.

4 STASHED

Besides resolving modules from within the node_modules directories, we can also place the module anywhere we want and require it with either relative paths (./ and ../ ) or with absolute paths starting with /

4 STASHED

If you want to only resolve the module and not execute it, you can use the require.resolve function. This behaves exactly the same as the main require function, but does not load the file. It will still throw an error if the file does not exist and it will return the full path to the file when found. 

4 STASHED

Every Module object gets an Id property to identify it. This Id is usually the full path to the file, but in a Repl session it’s simply Repl>. Node allows many ways to require a file (for example, with a relative path or a Pre-Configured path) before we can load the content of a file into the memory we need to find the absolute location of that file. If you create a local Node_Modules

4 STASHED

Deepstash helps you become inspired, wiser and productive, through bite-sized ideas from the best articles, books and videos out there.

GET THE APP:

RELATED IDEAS

Some of the main disadvantages of the current structure
  • There are two realms: JS and Native which are not really aware of each other and do not share the same memory
  • The communication is async through the bridge between the two realms
  • It is slow to transfer large chunks of data. Since the memory is not shared, all the data passed between js and native is a new copy
  • No way to update the UI in a sync way.
  • The library repo is HUGE. It makes the library heavier and also making it slower to contribute code from outside or release new fixes

1 STASHED

4 LIKES

Bad at delegating

There is a situation where your manager will assign you a project but not give enough instruction, hoping you will just figure it out. When the project is mostly completed, your manager may finally turn attention to the project and express disappointment because you did not do it in a way she expected.

You may feel like you are continually failing at your job, but that would be wrong. It is more likely that your manager is bad at delegating.

105 STASHED

3 LIKES

Happiness and emotional diversity

While most of us believe that happiness is the key to a long and healthy life, meaning first and foremost a life without stress, recent studies have shown that actually, it is emotional diversity that enables us to live longer and healthier rather than happiness itself.

This means that it is better to experience a wider range of positive emotions rather than just happiness alone.

383 STASHED

6 LIKES