Using the dart:js library without a html file - javascript

Is it possible to use the dart:js library without having a html file to load the js files but some alternative way of loading the javascripts in the context?
I need this for a command-line app, so having a html file makes no sense

When you run a command line app in the DartVM there is no Javascript VM, so you cannot use Javascript libraries.
However depending on your use case, you could run your javascript code with node.js, and communicate with the DartVM using sockets.
Perhaps add some more details about your specific use case.
Update:
To run lessc from Dart, first install node.js.
Then Install lessc:
npm install -g less
lessc styles.less styles.css
You can then call lessc from Dart using dart:io Process.run().

Related

How to use Flatbuffers with JavaScript in the browser?

Pure JavaScript support for Flatbuffers has been abandoned, and the project website tells you to use transpile from TypeScript.
This is what I tried:
Write a Flatbuffers file website.fbs.
Run flatc --ts website.fbs to receive website.ts.
Run tsc website.ts to receive website.js.
Run browserify website.js -o website.browser.js to receive a file which I can include with <script src="website.browser.js"></script>.
But console.log(Website) tells me there is no Website object.
What is the correct path to use Flatbuffers with JavaScript in the browser?
The problem is that browserify does not export to global namespace (window) by default. By supplying the -s parameter to browserify you can get it to export to a global symbol:
browserify website.js -o website.browser.js -s website
After that you should be able to find window.website with the same API as before with the old direct to js code generator.
Or, in my opinion, the better option is to use a more modern bundler (Rollup, Parcel, esbuild, Webpack etc.) and have it bundle the generated ts (or js) together with your application in a single step so that you do not need to use the global namespace at all. That would also allow for more efficient and small code, better IDE support and probably some other benefits.

How can I run Javascript without a browser or node js?

I want to create a Javascript app but I want to do that only using Javascript.
Because the node js is framework and I don't want to use any framework,
can I create a Javascript app without a browser and without any framework?
Only using Javascript?
In order to run a javascript application, you need a javascript runtime, in the end you will have to install some, however, you can try something different like live-server from npm
You could install nodejs runtime environment and run your code in cmd explained here. https://www.geeksforgeeks.org/how-do-you-run-javascript-script-through-the-terminal/#:~:text=You%20can%20Run%20your%20JavaScript,Environment%20Download%20and%20Download%20it.
Node js is actually not a framework or a library, but a runtime environment, based on Chrome's V8 JavaScript engine. I suggest you use it.
Just install, type in console(powershell, bash, etc.):
node file.js
and I will run your app.

How to import an external script in my Angular library?

I am trying to export some business logic ts files of my Angular application into an Angular library. Unfortunately, I need to use a Javascript file to connect to a proprietary service build by other people, which is only available as a pure javascript file and requires jQuery.
Without libraries (when I was using the script in my angular application), I solved this by adding these js files to the .angular.json under the "scripts" section.
The js file is huge so I could not consider the option to rewrite in typescript.
Is there some way to import and use a javascript file in my ng library?
Thanks
Karan
I have finally solved my issue.
Turns out there is no way to add a javascript file to an Angular library since ng library does not support "script" tag.
There is one other way: i.e. you can publish your library without the javascript file dependency, and the client using it will have to do 2 things:
npm install "your-library"
Now add the dependent javascript file to scripts tag in angular.json file.
Example in my case:
Run npm install wps-ng
And my Angular.json looks like this:
"scripts": [ "node_modules/jquery/dist/jquery.js", "node_modules/wps-js-52-north/src/web/lib/wps-js-all.js" ]
Unlike the other libraries, just a simple npm install is not sufficient, the client will just need to add their javascript file to scripts tag.

How to"permanently install" a scripting library into a web?

Right now I am following a Mojs tutorial, and, having played a little bit with it, I figured out that installing and uninstalling Mojs through npm install and npm uninstall, respectively, doesn't make any difference - if I have a hyperlink
<script src="http://cdn.jsdelivr.net/mojs/latest/mo.min.js"></script>
in my html file, the script works, if I remove it - it stops,no matter if I have Mojs installed through npm. How to make any sense of it?
Node is just a javascript runtime. You use it to run Javascript code outside of the browser. It doesn't automatically put any script files in your html. Node Package Manager (npm) is a helpful tool for getting packages and modules, such as Mojs, but they have to be 'required' in a seperate js file either by using require or import
If you've ever used Python, think of Node as Python and npm as pip where you have to run pip install before you can 'import' the module.
Using node you can run any js file by typing node file.js similar to how you would run a python script using python file.js.
When you install a package using npm install a node_modules directory is created and a whole bunch of (usually) javascript files are installed there under a folder of the name of the module, so if you want to manually include the files, you should look there to find it.
I suggest this introduction to Nodejs.
The script tag that you posted is using a url to a cdn (content delivery network (I think)). This means that when it tries to load the script tag, it looks at the url, then fetches the data from the cdn server and returns it to the browser.
The files you are installing via npm are generally not available to your front-end code unless you are using something like webpack or browserify. They will bundle all of the files you are using from node and allow them to be available in the browser.
So, yes, it does make sense that you installing or uninstalling that package is not affecting the app.

Can you use a npm package without using NodeJS

I found a library on github that I would like to use but the download instructions only mention using npm but I am not using a NodeJS project (just a basic html,css,javascript front-end with no back-end server). Am I still able to use that library or is it a lost cause? Is there another way to download it without using npm?
Is there another way to download it without using npm?
If it's on github, then you can checkout or fork the repository as you can with any other git repo.
Am I still able to use that library or is it a lost cause?
Whether or not the library will work without Node will depend on the library.
If it presents itself as a Node module, then you'll probably have to modify it (or find a compatible module loader for browser-side JS).
If it depends on NodeJS features (such as the filesystem API) then you'll be out of luck (unless, for example, you polyfill them to work across HTTP)
If you use a build tool such as browserify, or webpack, you can author scripts that require node modules and the build tool will generate a script that includes all the necessary dependencies (assuming that the necessary dependencies are compatible with client-side JavaScript environments).
Downloading dependencies would still be done via npm, but only for local development. Your server would only need the generated script.
Alternatively, if the script is on github or any other repo online you may be able to download it directly. Many modules are published using UMD, which would allow you to use the script using various inclusion methods.

Categories