JavaScript - Runtime Environments
Node.js
About
-
"Run JavaScript everywhere".
-
"Runtime environment".
-
It's the most popular "Runtime environment".
Syntax
-
Executing the program:
node my_script.js
Deno
Setup
-
Deno .
Installation
-
irm https://deno.land/install.ps1 | iex
Creating a project
-
deno init-
A
deno.jsonfile is created to configure your project , and two TypeScript files are created:main.tsandmain_test.ts.-
The
main.tsfile is where you'll write your application code; initially, it will contain a simple program that adds two numbers together. -
The
main_test.tsfile is where you can write tests; initially, it will contain a test for your addition program.
-
-
VSCode Environment
-
"You will now get all the benefits of Denoβs LSP, including IntelliSense, code formatting, linting, and more."
-
Setup:
-
In the Extensions tab, search for "Deno" and install the extension by Denoland .
-
Open the Command Palette by pressing
Ctrl+Shift+Pand typeDeno: Initialize Workspace Configuration. -
That's it. A file called
.vscode/settings.jsonwill be created in your workspace.
-
Imports
-
Understanding the imports cache .
-
deno info-
Shows the cache directory of imports.
-
-
deno --reload main.ts-
Re-downloads the dependencies.
-
-
-
In Deno, the module system uses
ESM(ECMAScript Modules), which is different fromCommonJS(used in Node.js). Instead ofmodule.exportsandrequire, you useexportandimport.
Installing modules with Deno
-
I don't know when to use this.
-
deno install? -
deno install npm:express -
deno install npm:mongoose -
deno add?
Syntax
Running the program
deno main.ts
deno run main.ts
TypeScript
-
Deno will compile your TypeScript code to JavaScript with no extra config needed. Deno can also type check your TypeScript code without requiring a separate type checking tool like
tsc. -
Checks the file for static typing.
deno check module.ts
-
When using the
deno runcommand, Deno will skip type-checking and run the code directly. To perform a type check of the module before execution occurs, you can use the--checkflag withdeno run.
deno run --check module.ts
# or also type check remote modules and npm packages
deno run --check=all module.ts
Watching
-
You can supply the
--watchflag todeno run,deno test,deno compile, anddeno fmtto enable the built-in file watcher. -
The watcher enables automatic reloading of your application whenever changes are detected in the source files.
Others
Declaration file
-
.d.ts
Bun
-
.