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.
Related
I'm trying to use the serialization API from the node.js v8 module in my react app (created with create-react-app) but it doesn't seem to work.
According to the documentation it should just be a case of importing/requiring the module. When I try this, it all appears to be working as expected - no errors. I can even access methods like .serialize() and .deserialize() on the v8 object too - great. But when I try to actually run my project (using react-scripts start) I get a compilation error:
Module not found: Can't resolve 'v8' in '...'
Is it looking for a file called "v8.js" to import rather than using the node module for some reason? How do I get around this?
node_modules is only a concept when working within the node ecosystem. So it is only possible to import "v8" when within a node process.
Since you ask about a "react app" that seems to imply that you are writing something for the browser. Which now has modules which use import/export (similar to require/module.exports from node), however, that still doesn't mean that the "v8" package will be present.
Many of node's packages are C++ backed (or to use the technical term, they are "native packages") instead of being purely written in JS. Also, it should be noted that unlike dependencies listed in your "package.json" file, none of the node packages are actually downloaded when you run npm install since they are all bundled with your installation of node.
Currently I am developing an NPM module which focuses on the use with Node.js. It requires the types of an external package (#types/package), but not the package itself. The types are currently located in the devDependencies, like every other type files. But when I install the library from git and use it with typescript, it tells me that exactly the external packages's types are not available, which seems logically to me. Is there a way to bundle these types within the NPM package itself?
Adding the types to dependencies would solve this, I think, but I do not want to store it as a default dependency.
The other way is to copy the type definitions to the src folder first and use them directly. This seems to be complicated and not intended.
Do you have any suggestions? Thanks and cheers!
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.
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
I have a question around the dependency injection based on AngularJS and NodeJS.
There's any difference between $injector from AngularJS and the require module from NodeJS?
Would be nice use require module with a MEAN STACK architecture instead $injector for the Angular app? And for whar propose?
They're quite different.
Angular's $injector is a classic example of Inversion of contorl. Instead of each module fetching their dependencies, you have an $injector whose job it is to provide dependencies to the modules that are asking for them at run-time. This makes it really to easy switch out the dependencies in tests for example, since nothing is forcing you to pass in the expected dependency -- you could pass in a mock version.
NodeJS's require methods just allow you to require other javascript files and have access to any properties they set on module.exports.
They're not mutually exclusive. You could use browserify (nodejs like require for the front-end) to load the different Angular modules if they are in separate files. It would essentially be equivalent to concatenating them, however. If you wanted to dynamically load the angular modules as needed, you would have to use something like RequireJs.
Conversely, you can use inversion of control in node by passing stuff into a module rather than trying to fetch it from the module. It's actually good practice in many cases.