Can I include npm (or similar) modules inside Couch views? - couchdb

I want to run an npm module as part of my map function. I realise it's possible to include CommonJS-style modules as strings within the view document. But including a whole npm module in this way along with its dependencies is a little impractical. Is there a way to include npm modules as external files?

Not at the moment, no. You would have to use the CommonJS method you are suggesting. Just be warned that you might have to compile the module and its dependencies into one body of JS to be most effective, and not all Node behaviors will be found in CouchDB since it uses a different engine.
As Dominic mentions, there has been a lot of discussion on this topic for the last year or so.

Related

Best way to distribute modules used the same framework

I am creating my first open source project, and I am making some plugins for it. These plugins will be published as npm packages, and they will have identical dependencies.
My question is, what I the best way to deliver them and avoid code repetition? I know I can use something like Rollup.js to pack all dependencies used by that module in the final distribution js file, but if the user is using multiple modules, the inlined dependencies will be repeated and make the file bloat.
I know end user can use a bundler to remove those repeated codes, but is there anything more I can do to reduce the size of my distribution js files?

How to write an NPM module that uses multiple files?

I have a simple local command-line module that I want to use in 2 different ways (basically different defaults), but it uses the same core logic, so I want to extract that logic into a third entity and use that from the two entry points.
I have everything working with two bin command scripts, but each file has its own copy of the logic to run, and I am not sure how to pull this duplicated code out into a third file within the same module. I figure I could do it by creating an entire separate module and loading it with require(), but I would rather just keep it together since it's tightly coupled.
The structure is like this:
bin\
cmdone.js
cmdtwo.js
core.js
package.json
I would like to move the logic, which currently exists in both cmdone.js and cmdtwo.js, into core.js and reference it from the two files in bin. Is this possible?
If i understand your question correct, then what you need is require function of nodejs
Well, after some more poking around, I discovered that this works:
const test = require('../core.js');
I suppose I misunderstood the distinction between Node modules and NPM packages. I was basically equating the two, but it seems that you can create and use modules entirely within packages, they don't have to be one-to-one.

Node installs loads of modules

I am trying to install some node modules for my application.
Now after entering this command: npm install laravel-elixir it creates a folder node_modulesand installes over a hundred modules!! this cannot be right.
How would I solve this problem?
How would I solve this problem?
Write your own code from scratch.
Really, there's very little that can be done. Large dependency trees are very common in Node.js. A lot of modules are built on the backs of other modules. The module in question is an especially large piece of software, trying to do what seems like a lot of different things, and relying on many other modules to do so.
You can try
$ npm install laravel-elixir --no-optional
to see if you can trim some optional dependencies from the tree. Another methood is to add optional=false to your .npmrc.
In my brief, and unscientific testing this seems to drop about six dependencies from the tree. Not much.
You should also make sure you've updated to npm 3.0 (3.8.6 being the latest), as it does a better job of flattening dependencies.
Sometimes there are needless dependencies in the middle of a tree, and in that event there is not much you can do other than reach out to the maintainers, and see if these dependencies can be removed, but then all the downstream packages will need to update.
This is generally called depedency hell, and it is an unfortunate symptom of certain modules that rely on too many submodules.
In reality though, if this module does what you need it to do, and there are no ill effects of having many dependencies installed, does it really matter? Other than the install time, when using the module, can you tell that it is pulling in a lot of other modules?

Creating node module half-native half-js

I have made a module for Node.js, which is part native, a C++ compiled library and half-JavaScript - about 10 *.js files. How should I distribute that? As single module or separately?
Depends on the use case I suppose, but as one module is probably fine. Unless you'd like to maintain them in separate repositories, you've got other modules that might prefer depending on them separately, or other otherwise and then it is just a matter of adding one to the other's package.json dependency list.

Any library for visualizing module dependencies in Node.js?

As part of a major refactoring of my Node.js app (going DDD), I'm looking for a library that through inspecting code is able to visualize module dependencies (by means of 'requiring' them) between different node-modules.
Visualizing in Table-format is fine, I don't need fancy graphs.
Any Node libraries out there?
If you may accept also some fancy graphs: http://hughsk.github.com/colony/
I do not know if this exists, but I found the following by quick search:
http://toolbox.no.de/packages/subdeps
http://toolbox.no.de/packages/fast-detective
Maybe subdeps is not exactly what you want right now, but I think you could use these projects to make that project yourself?
See also https://github.com/pahen/madge
Create graphs from your CommonJS, AMD or ES6 module dependencies. Could also be useful for finding circular dependencies in your code. Tested on Node.js and RequireJS projects. Dependencies are calculated using static code analysis.
I just published my node-dependency-visualizer, which is a small module, that creates a digraph from your node dependencies. Paired with graphviz/dot you can create a dependency graph as svg (or other image format) which you can include with your documentation, embed in your Readme.md, ...
However, it does not check, whether the dependencies are actually needed in code - not sure, whether the OP meant that with "requiring". Of course this question is old, but this tool might be helpful for others, too.
Sample image (Angluar cli):

Resources