How to load a single AMD module in node? - javascript

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.

Related

difference between library , package , module in js

I have started to learn react and I am very confused with the concept of packages.
and why we can't just use a simple link as cdn and there is a module which i don't understand it and what's npm and why i have to use it with react
Not trying to give the definite answer here, but trying to explain the 3 terms as simple as I can:
A module is just a file containing lines of JavaScript code.
A library uses one or many modules to provide a set of features.
A package is a downloadable, versioned library. Think of someone putting it in a box and shipping it to you, so you can import it and use it in combination with your own code.
so I came with conclusion and hope you tell me if I get it right or not .
-Module : it is justba javascript file but it's different from normal script that it has its own scope so you have to use import or export to exchange information between modules.
-Library : it is a group of modules or scripts that it is responsible for the function you want .
-package : can be one or more libraries but it is also contain files that don't deal with the functionality but it's only role to make sure the libraries and functional file work properly .
like react package it is come with react library deals with the functionality and also has babel compiler to make browser read and understand react library.
It is very much possible to use a simple link such as a CDN. Many packages also have links available, such as material UI. However, it quickly becomes unmanageable to use CDN links when your project grows, and it can affect performance and load times of your site.
Npm stands for Node package manager. It handles packaging for Node, where it would not be suitable to use a simple link.
It turns out that it is possible to also use npm for web applications, by combining it with a bundler. The bundler (such as webpack) takes all of your modules (JavaScript files and npm packages) and bundles it together so that you get a single script which you can run in the browser.

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.

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).

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

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.

Categories