use nodeJS module without nodeJs - javascript

it can look like a very simple question, but as a JS beginner, I would like to know if it was possible to use a NodeJS module (I would like to use this one for exemple : https://github.com/yaronn/xml-crypto) in a webextension without having to change it's code.
Thanks in advance.

I am the author of xml-crypto. xml-crypto is built for node.js and relies on the node.js crypto built in module which is not available for browsers. However there is an alternative browser crypto module called forge. Someone told me they were able to replace xml-crypto dependency in crypto with forge (by making small code changes in xml-crypyo) and that allows to run xml-crypto in a browser. You would also need to use browserify which should be easy here.

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.

Is it possible to build a full Node.js web application using Kotlin?

If I understand correctly, with the release of Kotlin 1.1, we can set JavaScript as a compile target for full compilation to JavaScript of Kotlin projects. Is it possible (or feasible) to write an entire Node.js application, such as an express webserver, using only Kotlin code?
As this question suggests, we can import Node modules into Kotlin classes:
external fun require(module: String): dynamic
val express = require('express')
...which seems like I can create an application using:
val app = express()
Is this the Kotlin way to set up an express application? Or should I declare a class as described in the docs:
#JsModule("express")
external class Express { ... }
What is the canonical way to set up a Kotlin project for Node.js application development? Is Kotlin's JavaScript interoperability robust enough to continue down this path, or will it be more trouble than it's worth?
Technically speaking, yes, provided the claim by Kotlin that:
You can use Kotlin to interact with server-side JavaScript such as node.js
Is correct, and the transpilation of Kotlin -> JS is reliable enough to be able to predict what JS is coming out, then you could write a Node app in Kotlin, much as you can write them in TypeScript.
I suspect, personally, that you'd find it difficult, buggy, and rather short on support, but it might make a good academic exercise...maybe.
Yes, it's possible https://kotlinlang.org/docs/reference/js-project-setup.html
But, NIO was the biggest reason to use NodeJS instead of any language to build a backend solution. Now, with reactive first class support you can have a stack like Kotlin + Spring Reactive + Coroutines + R2DBC and build a simple micro service or any full enterprise solution.

Is it possible to load twilio.js library with webpack?

What i need is a way to bundle all my javascript dependencies into one javascript file with Webpack (Just like with socket.io-client), but i can't do that with twilio.js.
I can see that the latest of twilio.js is listed here.:
https://www.twilio.com/docs/client/twilio-js as a script tag to:
//static.twilio.com/libs/twiliojs/1.2/twilio.min.js
But this is just a loader script for building the real twilio.js library here:
https://static.twilio.com/libs/twiliojs/refs/82278dd/twilio.min.js
And none of these supports Webpack.
https://github.com/twilio/twilio-node also exsistes, but this is for node.js only - not just plain client side javascript.
So my question is, is there a way to require the twilio.js library with Webpack ?
This answer is for Twilio.js client version 1.3.16 (script file, documentation).
The Twilio codebase is pretty bad. They don't publish their code to npm, and simply loading the twilio.js file from their CDN has side effects, including reading from window and looping through all script tags on the page. This means, even with the below answer, the client code can't be loaded in node, blocking server side rendering and testing, etc.
I published the package to npm as a mirror of the code, but it's not straightforward to use. Webpack can't correctly handle whatever bogus require structure they have set up. First, install:
npm install --save twilio-client-mirror
Then to use, require as normal:
import * as loadTwilio from 'twilio-client-mirror';
However, you can't actually use the loadTwilio object. You have to reference the global Twilio object, injected by the script:
const Twilio = window.Twilio;
Twilio.Device.setup(token);
...
This is a first attempt I made to try to include this flaky code inside a modern codebase. Hopefully with the coming 1.4 beta they will address these issues.
I know this is late, but I, too, just wrestled with this issue. Being new to Node, TypeScript and Webpack, I failed to understand that Webpack should be used for client-side scripts, while the Twilio-node library is a server-side library (as discussed loosely here). In other words, you shouldn't use require('twilio') in any client-side scripts...only Node scripts.
I had success with the following:
Add <script type="text/javascript" src="//static.twilio.com/libs/twiliojs/1.2/twilio.min.js"></script> to my main .html page
Use var twilio = require('twilio'); inside of a Node (server) script...not in client-side JavaScript (for example, add the Twilio calls inside of API methods, which can be created using a framework such as Express)
If you happen to be using TypeScript for Node scripting, use a transpiler such as the native TypeScript transpiler tsc (easy to do from a command line). If using JavaScript, use GulpJS (my fave) or GruntJS to help you consolidate and even run your Node server-side scripts
Use Webpack for anything else
Again, I'm new to this, so I'm open to input or corrections if I've stated anything incorrectly.

Javascript library for both the browser and node

I want to write a javascript library that works like an SDK for an external API.
Ideally this library could be used both for frontend projects, in the browser and for backend projects using node.js.
Initally I wasn't considering node so I was planning to require jquery as a dependency for using the ajax functions (for making the API calls) and deffered objects, but now I have second thaughts.
Considering that my goal is to have the same code base for both scenarios what do you think I should do? Is using the jquery npm package a good idea, or do you have other suggestions?
You can use the jquery package from the npm repo without problems.

How to run JavaScript using jsc on OS X?

I got this when I was trying to find some way to run my JavaScript programs through terminal. The run and load command mentioned can execute external JavaScript files. Help me how to do this. I am trying to run JavaScript programs which are store locally on my system.
EDIT: I am trying to solve Project Euler Q10 in JavaScript. So this is the program that I want to run in NodeJs or JSC. I need help in running the JavaScript files in Node and JSC.
Any example will be really helpful.
Thank You all.
Complements of this post, JSC lives at
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
and is not in the shell PATH by default. You can fix that with
ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin
jsc takes filenames as arguments. You could run a file named demo.js with
jsc demo.js
Note that you'll have to use debug() instead of the conventional console.log() in your script to see any output.
NOTE for MacOS Catalina (10.15.x) users
jsc now lives on a new path:
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc
Make that adjustment to #hurrymaplelad's instructions and you'll be good to go.
You're not very clear on what sort of programs these scripts are. Do they do local tasks (e.g. interact with the local file system)? Or are they web scripts that are supposed to interact with (HT|X)ML documents?
If your usage scenario is the former, I recommend using node.js. It is under heavy development, but it is already very usable. For my first project with Node I wrote an XMPP chat bot, completely in JavaScript.
Edit:
I seem to have overlooked "using jsc" in the question and answered "How to run JavaScript on OS X?" instead. Still, I think Node is a better alternative if the author is looking to use JavaScript in place of something like Python or Perl.
For future reference: I made a GitHub gist with updated info on using JSC, is available here.

Categories