Is there a way to use a non CommonJS module in Node? - javascript

Let's say someone didn't publish their module in npm and even if they did let's say they didn't have it as commonjs module. Would there be a way to still use that plugin or module inside node? I'm aware that we can use node code in browsers using browserify but I was just wondering if there's a way to do the other way around.
This question seems like the most closest that I'm looking into but there's no response yet.
What my end goal really is to be able to use libraries that are not specifically CJS module in node and still be able to test my code in node/cli.

Related

How can I add UMD so I can use my module in the browser and as an NPM module?

I'm trying to make my NPM module work in the browser, but I'm having a little trouble understanding the UMD syntax. Here are my requirements for my module, which I'll call Mod.
I need to be able to call child functions of Mod, like Mod.DoSomething(), Mod.Utils.DoSomethingElse(), etc. from other files in the browser
It needs to play nice with Webpack, Browserify, RequireJS, etc.
I need to be able to require it as a module in an NPM package, just like any other package. var mod = require('mod'); var returnedVal = Mod.DoSomething;
I don't have any dependencies, but I'd appreciate an example of how to do it both with and without dependencies. One of my main questions is how to export the child functions, so please include them in the example. Thanks!
I went through the same situation recently so even though it was an old question I would like to share my experience.
I use webpack and to make my npm module work in browser the solution I can find is to set webpack output setting libraryTarget: 'umd', check this article for details
But one mistake I made after reading that article was I misunderstand the relation between umd and es6 module, thought they were basically the same. Check my own question for details ES6 modules via script tag has the error "The requested module does not provide an export named 'default' "
About which module I should use in browser I would like to quote an answer here https://stackoverflow.com/a/55659242/301513
Today, the most likely answer is: use the UMD module. Some time in the
future it may be: use ECMAScript modules; but we don't yet (2019) have
consensus on how those will be distributed.

How to load a single AMD module in node?

I need to load a single AMD module using node.js.
Basically I need to check only the presence for a property in that AMD module.
I would like to know if node offers this possibility directly.
If no I would like to know a npm package which could help on the tasks, which does not change the node loader (I need a very simple solution for now).
Thanks
It's hard to answer such a broad question in any details.
See the resources in those answers:
Exporting Node module from promise result
javascript - Why is there a spec for sync and async modules?
and you should find all the info you need.

using requirejs with node

I'm currently working on a server side Node project. Although node has it's own module loader using CommonJS I'm evaluating whether to use RequireJS with it. Whilst there are advantages to using RequireJS with Node if the application that has some client side aspects to it, I can't find any benefits for a project which is entirely server side.
Is it commonly thought that for a 100% server side Node project, there are no real advantages to incorporating RequireJS?
There exist reasons to use RequireJS server-side but they are few. Unless you can state a reason, like:
I must use RequireJS because X
where "X" is a reason which justifies the use of RequireJS, then you should not do it.
Note that just wanting to write modules in the AMD format is not reason enough as there exist loaders (like node-amdl-loader) which allow loading AMD modules in Node. I actually use this when I want to test code which does not depend on a browser. I write the modules in AMD format and specify that if used in Node, an AMD loader like node-amd-loader should be used. This way the library works both in Node and in the browser but I test it in Node.
One reason to use RequireJS server-side is for instance, if you need to run code that needs a DOM implementation. If you use something like jsdom to provide the DOM and the code you want to load is a series of AMD modules, then using RequireJS to load the code into the DOM environment created by jsdom makes sense.
I've never found a good reason to use RequireJS in node. However, RequireJS can be run on node, and the documentation has a brief explanation of why you might want to run RequireJS on a Node server:
By using RequireJS on the server, you can use one format for all your
modules, whether they are running server side or in the browser. That
way you can preserve the speed benefits and easy debugging you get
with RequireJS in the browser, and not have to worry about extra
translation costs for moving between two formats.
Most of that is only useful in a project that also has a client element. If it were me, I'd stick with Node's internal module loader.

Make module available via AMD, bower, simple browser JS, and node

I just wrote a module ( https://github.com/mercmobily/simpleDeclare ) and made it available as a Node module via NPM. It follows CommonJS.
I would like to make it also available to people using Bower, and AMD-like loader, and a straight include using the browser.
What's the current pattern to make this happen? Or, is it even possible? I remember seeing a few modules that did it, but cannot find them and am only finding outdated and not-so-definite information (but I suspect I am looking for the wrong thing).

Testing that a JS module is CommonJS or AMD compatible

Is there a way to test automatically if a JavaScript module is compatible with CommonJS (Node) or AMD module formats? Furthermore, how could one write it as a Grunt task?
Compability is an important feature of a module and should be able to be tested automatically at least in some degree. For example a change in a building configuration could produce a compability error that cannot be noticed by unit tests that use only browser interface of the module, like is the case with QUnit. There might be also possibility that the compability-enabling code of a module (e.g. self-made UMD wrapper) is so messy that it is hard to say if a change elsewhere in the code could have an effect on the compability part of the code.
One could use tools that generate probably correct wrappers of different module formats. For example grunt-umd and uRequire are such tools. But as they too depend on the build configuration, how you can be sure that the compability really is there without any testing?
I don't think there is an out-of-the-box or obvious solution.
In general there are several approaches you could take to solve your problem:
Write your code in one module format and use transpilers to produce the other module formats. Babel (5) is pretty good at doing this, but there are probably others.
Use a parser like esprima and statically analyze the code. The "standard" wrappers like those on https://github.com/umdjs/umd are probably easy to detect patterns. Custom wrappers might be harder and you might need a few revisions before you get it all right.
Trial-and-error: Have your grunt task create files (e.g. generated from .jade templates) that include the supposed module in each of the manners you care about (AMD, browser globals), then "run" those (e.g. using nightwatch or selenium) and see if any errors occur. (This might be very difficult to get right if the module you are trying to load has dependencies)

Categories