How do I read a local file in Deno?

(Update: wrapping code in async function main() { … } is no longer needed because Deno now supports top-level await) Using built-in Deno.readTextFile const countWords = (s: string): number => s.split(/\s+/g).filter(w => /[a-z0-9]/.test(w)).length; const text = await Deno.readTextFile(‘input.txt’); const count = countWords(text); console.log(`I read ${count} words.`); See the docs at: https://doc.deno.land/builtin/stable#Deno.readTextFile Using built-in Deno.readFile const … Read more

How can one check if a file or directory exists using Deno?

There is the standard library implementation, here: https://deno.land/std/fs/mod.ts import {existsSync} from “https://deno.land/std/fs/mod.ts”; const pathFound = existsSync(filePath) console.log(pathFound) This code will print true if the path exists and false if not. And this is the async implementation: import {exists} from “https://deno.land/std/fs/mod.ts” exists(filePath).then((result : boolean) => console.log(result)) Make sure you run deno with the unstable flag and … Read more

How can I avoid the “an import path cannot end with .ts extension” error in VSCode?

This can be solved by installing and configuring the Deno Extensions for VSCode. Press CtrlShiftX to open the extensions view, then type “deno” and click on the entry name “Deno – Deno support for VSCode”: and install it. After installation, you can choose any one of following methods: Go to settings: (Ctrl, or Cmd, on MacOSX), … Read more

Node.js’ __dirname & __filename equivalent in Deno

In Deno, there aren’t variables like __dirname or __filename but you can get the same values thanks to import.meta.url On *nix (including MacOS), you can use URL constructor for that (won’t work for Windows, see next option): const __filename = new URL(”, import.meta.url).pathname; // Will contain trailing slash const __dirname = new URL(‘.’, import.meta.url).pathname; Note: … Read more

How to use npm module in DENO?

Deno provides a Node Compatibility Library, that allows using some NPM packages that do not use non-polyfilled Node.js APIs. As of Deno 1.25 there’s an experimental NPM support by using npm: specifier npm:<package-name>[@<version-requirement>][/<sub-path>] import express from “npm:express”; const app = express(); app.get(“/”, function (req, res) { res.send(“Hello World”); }); app.listen(3000); console.log(“listening on http://localhost:3000/”); The –unstable … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)