Can a npm dependency require a module from its parent package - node.js

I am building a server app as a "core" framework and (potentially) multiple specialisations, starting with "spec_a".
I would like "core" to be an npm package, and "spec_a" to be an npm package which requires "core".
While I could require spec_a's modules and pass them to core in function calls, I would prefer core to read a config file and try to require modules (which are specified in the config file) from it's parent package. (The core shouldn't need to know the name of its parent package, as it will be different from each specialisation.)
What I am looking for is a require_from_parent(module_name_from_config) call.
Is there a way of making such a thing?
I was thinking of using require('../'+module_name_from_config), but this is fragile and will probably break on the latest npm.

I think I found the answer here on a nodebb blog.
var Topics = module.parent.require('./topics'),
User = module.parent.require('./user'),
Notifications = module.parent.require('./notifications');

There is no explicit "requireFromParent" call. Whenever you call require, it will first look in its own node_modules folder and if the module is not found, it will traverse up the directory tree and check if its parents have the module, which should work for your purposes.

Related

Archived node_module libraries

I am wondering what is the best way to keep archives/older versions of a specific module I am developing for nodeJS.
Say I have SampleModule v1.0.5 then later on I will be releasing SampleModule v2.0.0. I want to retain both versions in my node_modules folder since another app will only need SampleModule v1.0.5.
What is the best way to do this? Should I just have 2 diff folders: samplemodule105 and samplemodule200 then call something like require('samplemodule200') or require('samplemodule105')?
Also, so basically require() checks the folder name rather than the name and version inside a module's package.json right? This folder and name are only used to fetch the library via npm install?

How to structure your NodeJS application in different modules?

so far i've learned a bit about NodeJS. But now i want to write a huge enterprise app with it and i'm wondering how to setup the structure correctly? Coming from other languages like PHP and Java, i imagine, i would split my project in different NPM modules. For example #mybigproject/customer, #mybigproject/cart and #mybigproject/checkout and so on.
But those submodules would be installed in the node_modules folder of the application skeleton. How would i tell for example Express, that the template files are in the different module directories? Or for example i use TypeORM for data access. So each module would have it's own set of models. How do those models know the database configuration data, as it's only in the main application skeleton, or the other way around, how does the application skeleton should know where to find the models?
Don't use npm modules for different parts of your project.
This components is integral part of your project and usually depend on your global config / schema / routing / etc
Just put it in different files and require it where you need it.
You can get an idea for folders structure from projects like Sail.JS
Use npm modules if you writing some utility that going to serve you for different apps and you want an easy way to upgrade the utility code once for all your apps (or in case you want to share that utility as open source for all of us)
NPM can install your local folder as a dependency. (ref)
npm install <folder>:
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
Your module keeps its original location after installed and a symlink is created as the same name of your module folder in the top level node_modules folder.
In these custom sub-modules, you can use __dirname and relative paths to locate you configuration files to feed to database or other data consumers.
But remember that, sub-modules often serve as utility functions for the main module. They should be independent from the project context.

use npm module of dependency [duplicate]

This question already has answers here:
How To Require Module of a Node Submodule
(2 answers)
Closed 7 years ago.
The context:
my app depends on a module 'my-module'
'my-module' depends on 'express'
I want to require('express') form my app
without need of installing 'express' dependecy in my app directly but instead I want it to be taken from 'my-module'.
Is it possible to configure npm/package.json work this why? Maybe npm link should be used, I just not sure if it is a standard scenario.
I can see several possible solutions for your problem.
Explicit export
If you're the one who maintains my-module and this is one of its common use cases, then you should explicitly export any important dependency to be used in parent application.
This solution is preferable for handy utility modules, but it doesn't look like a good solution for modules like express.
Relying on npm
If you just want to eliminate duplicate dependencies, then you could add express to your parent application. If any of your application dependencies will also depend on express, npm will use already installed one (provided that there is no version mismatch).
This solution is preferable if my-module is a third-party module.
Using peer dependencies
Again, if you're the one who maintains my-module you could specify express an a peerDependency. By doing so you'll tell npm to always use express module from the parent application.
This solution is preferable for framework modules like express and mongoose.
Using the most direct approach
If none of those solution fits you for some reason, you could require any dependency of another dependency using the following code:
require('my-module/node_modules/express')
Just try to avoid this approach if you can.

Node/NPM: Can one npm package expose more than one file?

I have made a JS library for web development, it consists of several modules, which build up multiple distribution builds. It's fine when it is distributed over cdn or for example using Bower.
But now I'm trying to publish it with NPM so that it can be consumed using Browserify. My question is how can I expose more than one main file statically so that they can be consumable by Browserify.
you don't need to do anything.
require('my-library') // will require the `main` entry point
require('my-library/some-other-file') // will require a different file

Node.js and npm which word to choose "package" or "module"?

I am confused npm is a package manager, but Node.js has modules. So when you install or create your own... erhmm, module, package? Which word to choose over another and when?
Have a look at docs http://nodejs.org/docs/latest/api/modules.html :
In Node, files and modules are in one-to-one correspondence.
On the other hand package is a folder with a special package.json file in it. That file tells the interpreter how to load modules when you do require(folder);. In other words package is a collection of modules.
In Node, a single file is a module. It is a somewhat isolated entity from other parts of the program and by convention is usually written in a way where it can be run as an independent program (even if it doesn't do anything). On the other hand, a package in node is a larger entity. It is basically a complete program/library that serves some purpose. A package may consist of a single module file or hundreds of files.
The package is traditionally defined by "package.json" in the root directory of a package and it describes the creator's purpose and other things about the program.
If it has package.json/package.yaml at the top, and it is installable from npm registry, it is definitely a package.
If it's one simple javascript file (maybe a part of your package) written in CommonJS standard, it's a module.

Resources