How to use a Nodejs module with AMD require - node.js

I use monaco-editor in an electron app,and I want to add my own grammar support;
I tried to build a plugin like monaco-editor's grammar support library such as monaco-html or monaco-css, Monaco-editor use WebWorker to load those grammar lib,and here's the problem:
in the WebWorker,I can only access a build-in AMD require, I can't get any Node.js module with AMD require,any way I can get the nodeRequire function or require a Nodejs Module with AMD require?
any advise can be helpful,Thanks a lot.

Related

Node.js: "In Node.js, each file is treated as a separate module." What breaks that rule? [duplicate]

I have started working on an existing project based on Node.js. I was just trying to understand the flow of execution, where I encountered with some *.mjs files. I have searched the web where I found that these are module based JS-files.
I want to know how is it different from *.js files (how does it benefit)?
It indicates an ES6 module file.
Node.js's original module system is CommonJs (which uses require and module.exports).
Since Node.js was created, the ECMAScript module system (which uses import and export) has become standard and Node.js has added support for it.
Node.js will treat .cjs files as CommonJS modules and .mjs files as ECMAScript modules. It will treat .js files as whatever the default module system for the project is (which is CommonJS unless package.json says "type": "module",).
See also: Differences between ES6 module system and CommonJs
.MJS file
mjs an extension for EcmaScript modules
An MJS file is a source code file containing an ES Module (ECMAScript Module) for use with a Node.js application.
MJS files are written in JavaScript, and may also use the .JS extension outside of the Node.js context.
ES Modules allow web and application developers to organize code into smaller reusable components.
ECMAScript 6 (ES6) introduced the specification for ES Modules, providing a standard for implementing modules in JavaScript. As of 2018, all major web browsers support ES Modules.
However, the popularity of modularized JavaScript pre-dates ES6. Node.js, a JavaScript runtime environment, used CommonJS as the specification for modules. Because so many existing applications were built with CommonJS, when Node.js added support for native ES modules, it controversially introduced the MJS file extension to differentiate the two and prevent applications from breaking.
NOTE: Some developers informally refer to MJS files as "Michael Jackson Script" files.
For clarity. As for devs/humans, it's easy to distinguish between a module file(.mjs) and a normal javascript file(.js)... because it's not always easy to determine even if you examine the code in the file.
There are also performance benefits which gives you more reason to consider using it.
V8(JavaScript engine that powers Google Chrome) recommends the use of .mjs but it still depends on your situation. If you want to know more of it's advantages, check https://v8.dev/features/modules#mjs

How do you know what Node.js code will run on the browser?

I'm learning how to use Flux and I encountered the following line in the documentation: "We can use Node's EventEmitter to get started with a store."
I understand that you can use things like Browserify to do the bundling and minifying, grabbing all the dependencies that Node code has to make the bundled browser-compatible JS file. But what's bugging me right now is how you know what you can do this with. How do we know what Node code we're allowed to use in the browser?
So, first of all let's consider that when in node you have
JavaScript modules that are the 3rdParty modules written in JavaScript (ECMA5, ECMA6 and even TypeScript or CoffeScript) etc;
Node built-in module. Those are node Core modules like fs, path, util, etc.
native compiled module called Addons that are are
dynamically-linked shared objects, written in C or C++;
Then you have the packager / module bundlers
Browserify
Webpack
the transpilers i.e. source to source compilers that typicall will handle syntax tranforms like
Babel.js that shims modern JavaScript to legacy engines
and the techniques
ECMA5 Shim to support legacy JavaScript engines
HTML5 Cross-Browser Polyfills
Because you need to do polyfills if you want to transform not only syntax but even globals (like the Promise), so you combine transpiler to polyfill having like babel-polyfill
Finally we have different kind of modules design patterns (modules format) to be handled for the bundling process:
AMD modules format
CommonJS modules format
and formats that are not in those ones that must be bundled/shimmed - where possible - through custom loaders.
That said, native modules will not run in the browser: you cannot bundle a native module through Webpack. Ordinary modules will, but not all. This is due to several reasons. There are some specific methods that cannot be "browserified" or "webpacked". Let's take as example fs. Can you put this built-in module in the browser? There are some abstraction for that called brfs, that are transforms for built-in node apis fs.readFileSync() and fs.readFile(), so you will do
$ browserify -t brfs example/main.js > bundle.js
to get
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/robot.html', 'utf8');
console.log(html);
This will not work for every non built-in modules, in the npm modules jungle, so WebPack has a module.noParse option to exclude Addons modules, non supported modules, etc. - see here about that.
So you have to look at the list of the transforms that means you can apply this transform to browserify to obtain like the fs transform mentioned above.
So that said, how do you know that a certain module will run in the browser? When you design your web application and Node backend you have to do opportunistic design choises to design shared modules/library that will run in both the environment, so being shimmed/packed at some point, like object models, application logic, etc., other modules that will deal with the File System I/O or will use native addons, so that will work in the server only, packing through wrappers it's possibile, but the behavior will look differently, as we have seen in the fs example above, and web specific modules, so it's a design matter.
A note can be added about networking modules i.e. node http, https that thanks to library abstractions like node request will run everywhere or using specific transforms like http-browserify.

Node C++ Addon How to require js modules?

I have a C++ Node.js add-on. I can run use v8's RunScript to run execute javascript in the context of the native module.
Now, I also would like to execute a JS statement that contains require.
Unfortunately, the native module is setup in such a way that the context does not include require. I get the error:
ReferenceError: require is not defined
Is there a way prepare the native module's context such that it behaves like a standard node.js module ? i.e. it provides the require method?
I have met the same issue, what I have done was adding this be code below, prior loading my addon.
global.require = require;

Does TypeScript require node.js to run?

Installing TypeScript requires node.js. And I assume that TypeScript uses Node.js to compile the .ts to a .js file.
My question is, does that created .js file require node.js? The ones I've seen so far appear not to. I don't want to load node.js into my html pages if it's not used.
thanks - dave
No, TypeScript just emits regular JavaScript.
If you use the "external modules" feature of the language (import x = require('foo');) you'll need to compile for either CommonJS (node) or AMD (require.js) and have those available, but that's opt-in.

node.js module or require.js

i wonna programming web app and using express.js as webframework. As testing framework i will use mocha and chai.
Node.js support modular programming, i can load module with require like require("mocha"). On frontend i use require.js for organize my codes in module, but this can be use in node.js to. It it better to use require.js in node.js or better to use node.js standard module.
I know that require.js support AMD and testing codes with mocha it can comlicated, because the codes will be load asynchronously.
Node.js require or require.js for module loading?
node.js supports CommonJS module format.
If you have modules that you want to use both in browser and node.js, you can use require.js in node.

Resources