Node.js source code of require() function - javascript

According to the document (https://nodejs.org/api/globals.html), require() function is one of the global objects/functions. But I'm having a hard time finding its source code on Github. Are they written in JS or C++? I was looking for them in https://github.com/nodejs/node project.

This seems to be it (in node/lib/internal/modules/cjs/helpers.js).

To answer my own question, I think it's defined in loader.js. And you can see it's implemented for all modules.
It seems to be mapped to global.require in node.js

Related

Best way to evaluate javascript source code module with node.js

With node, I transpile a typescript tsx file int javascript. Then I need to call a function that is described in that javascript code. ES6 modules are used.
I use
writeFileSync('./temp/page1.js', js) // js is the module source code
import page from '../temp/page1.js'
page is here the default export which is a function that I want to call.
That works fine but I wonder if there is a better way to call the function inside the module source code? A way that avoids saving the source code into a temporary file.
eval would not be a security concern, but I think it would not give me the default export, modules are not considered in this old function.
Maybe something node related?
I found https://www.npmjs.com/package/node-eval which would avoid the file saving, although it still requires a filename, which causes some brittleness in my code.
node:module
node:vm
are maybe promising, looking at https://github.com/node-eval/node-eval/blob/master/index.js but I have not yet figured out if and how.
Similar, but more related to require: Load node.js module from string in memory
https://github.com/floatdrop/require-from-string
did not work either
all the variants with node:vm like
import vm from 'node:vm'
let context = vm.createContext({}, {name:"temp1.mjs"})
vm.runInContext(js, context, {filename: "temp2.mjs"})
result in "Cannot use import statement outside a module"
The best way would be nodes vm.Module class
https://nodejs.org/api/vm.html#vm_module_evaluate_options
but that is in draft stage and behind feature flags.

webstorm unresolved function or method for npm azure-storage (all azure modules)

WebStorm IDE having some issues with azure-storage intelligent coding does not find methods for that library (but the code works fine when executed).
image of unresolved function or method
Thank you in advance!
Per my experience, some issues for code completion or highlight hint not always mean the code exists errors, especially for Dynamic Language like JavaScript.
I tried to reproduce your issues, and I found some interesting things.
After I writed the code var azureStorage = require('azure-storage'), the code completion for the symbol azureStorage could not display the function suggestions for azure-storage module. WebStorm regard the varable azureStorage as a normal object, see below.
However, the code completion for any undeclared symbol will display all functions of all modules depended on the project after I commented the code var azureStorage = require('azure-storage'), see below. It seems that WebStorm default bind all functions of all modules with undeclared symbols automatically.
Although we can use the code completion in this way, the require code must be required when the code works.
For more details about Auto-completing code, you can refer to https://www.jetbrains.com/webstorm/help/auto-completing-code.html.

Alasql and Angular; jszip is not a constructor error

So reading another article and solution here I found the library Alasql which seems to do what I need.
I've installed it and was writing a proof-of-concept application to use it and when I attempt to use the illustration given in that article I get:
Error: jszip is not a constructor
write_zip#https://server:8443/vendor/js-xlsx/dist/xlsx.js:11295:12
write_zip_type#https://server:8443/vendor/js-xlsx/dist/xlsx.js:11407:10
writeSync#https://server/vendor/js-xlsx/dist/xlsx.js:11421:1
saveWorkbook#https://server:8443/vendor/alasql/dist/alasql.js:15656:17
doExport#https://server/vendor/alasql/dist/alasql.js:15556:3
alasql.into.XLSX#https://server/vendor/alasql/dist/alasql.js:15529:3
anonymous#https://server/vendor/alasql/dist/alasql.js line 7343 > Function:1:14
queryfn3#https://server/vendor/alasql/dist/alasql.js:6528:13
queryfn2#https://server/vendor/alasql/dist/alasql.js:6274:9
anonymous#https://server/vendor/alasql/dist/alasql.js line 7757 > Function:1:57
queryfn/<#https://server/vendor/alasql/dist/alasql.js:6223:12
queryfn#https://server/vendor/alasql/dist/alasql.js:6219:2
yy.Select.prototype.compile/statement#https://server/vendor/alasql/dist/alasql.js:7352:14
alasql.dexec#https://server/vendor/alasql/dist/alasql.js:4240:27
alasql.exec#https://server/vendor/alasql/dist/alasql.js:4190:10
alasql#https://server/vendor/alasql/dist/alasql.js:121:11
Index#https://server/app/states/index/index.controller.js:20:23
And those errors keep going as is the custom of Angular.
Looking for a solution I found https://github.com/SheetJS/js-xlsx/issues/184 but that didn't seem to help me out at all.
So I'm wondering if other people have encountered this and what their solution to the problem was as I'd really like to use this library but can't even get their example code to work.
EDIT:
So for those who might be coming up with the same problem...the solution I found was to add the JSZip library to my application (even though it looks like it was included with the SheetJs library) and made sure it was loaded before the Alasql piece. That seems to have done the trick...
It seems you are using it with requirejs or some dependency library
You will either need to include all .js files with the distribution or add them as a dependency in shim

Include javascript file in Node.js without require('..') ing

jade permits you to simply write
include folder/file
to include code from another file.
Is it possible to add simply cut - copy style code from another file in node for javascript files?
Its for development purpose, to isolate some code and work on it seperately.
PS:- I'm aware of require('jsfile.js') and export.x = function(){..
The accepted answer is wrong.
Depending on whether node fs and eval were available at the time this question was written, the accepted answer was probably always wrong.
While not recommended, what you want to do is essentially possible:
Use node's built-in filesystem functions to read the file you want to "copy-paste" into the current file.
Use eval() to "paste" that file into your current file and run it as if it was part of the current file.
https://github.com/dennishall/node-require-without-require
Update 6 Oct 2020: Embarrassingly, the answer I've provided below is false.
I am not certain what were the circumstances for my writings below, as I was familiar with eval at the time (and a very long time before then), however, it is what it is :)
Read the answer that #Dennis wrote for the correct one.
You cannot merge (or include) a script file into another script file during runtime. Utilizing require is your best option to separate your application logic into multiple files.
JavaScript is an object oriented language, and what you are asking for is a solution to a problem that exists in procedural programming languages.
I suggest that you design your application in such a way that would allow you to separate its files into object types that take on different responsibilities instead of treating each file as a script within some global state.
To answer your other question, Jade is actually parsing its source files and therefore can provide its own file merging. If we apply this to our scenario, Jade is to jade source files as V8 is to JavaScript source files. Since the jade language is procedural, it makes sense to allow this kind of feature where in JavaScript (which is object oriented) it doesn't.

How do I add my own JavaScript libs to ClojureScript?

I want to write a Google Chrome extension, using ClojureScript. With ClojureScript I can use all the Google Closure libs, but afaik access to the Chrome browser is not included in those libs. So I want to wrap all the Chrome stuff in my own JavaScript lib.
So far I tried creating my own jar that has a single JavaScript file that just creates a Foo object and exports the constructor. I'v added this jar to the lib directory of the ClojureScript compiler (which also has for example goog.jar), but so far without luck:
Exception in thread "main" java.lang.IllegalArgumentException: No implementation of method: :-compile of protocol: #'cljs.closure/Compilable found for class: nil
at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:494)
at cljs.closure$eval1056$fn__1057$G__1047__1064.invoke(closure.clj:187)
at cljs.closure$get_compiled_cljs.invoke(closure.clj:422)
at cljs.closure$cljs_dependencies.invoke(closure.clj:440)
at cljs.closure$add_dependencies.doInvoke(closure.clj:462)
at clojure.lang.RestFn.applyTo(RestFn.java:139)
at clojure.core$apply.invoke(core.clj:602)
at cljs.closure$build.invoke(closure.clj:701)
at user$eval1246.invoke(cljsc.clj:21)
at clojure.lang.Compiler.eval(Compiler.java:6406)
at clojure.lang.Compiler.load(Compiler.java:6843)
at clojure.lang.Compiler.loadFile(Compiler.java:6804)
at clojure.main$load_script.invoke(main.clj:282)
at clojure.main$script_opt.invoke(main.clj:342)
at clojure.main$main.doInvoke(main.clj:426)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:405)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Has anyone tried this before?
Take a look at this post from Luke Vanderhart: "Using JavaScript libraries in ClojureScript"
http://lukevanderhart.com/2011/09/30/using-javascript-and-clojurescript.html
Also, this video from Kevin Lynagh: "Extending JavaScript Libraries from ClojureScript"
http://www.youtube.com/watch?v=XfzXFWTT-z0
If I remember Rich Hickeys talk correctly the whole program optimization of the closure compiler needs any library code to adhere to certain coding conventions. I think he also said something of JQuery not doing this but Dojo doing this. I never did this but you might find some useful information here

Categories

Resources