NPM Package Location

Global libraries You can run npm list -g to see where global libraries are installed. On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node. Windows XP: %USERPROFILE%\Application Data\npm\node_modules Windows 7: %AppData%\npm\node_modules Non-global libraries […]

GZip with Pako

GZip is a super-fast compression technique, usable anywhere. In javascript, you can use the Pako library to compress your data. Browser <!DOCTYPE html><html><head> <title>Test Zip</title> </head> <body> <textarea id=”inputText” style=”width: 90%; height: 300px;”></textarea> <textarea id=”outputText” style=”width: 90%; height: 300px;”></textarea> <br> <input type=”button” value=”Go” onclick=”go();”> <script src=””></script&gt; <script> function go() { console.log(document.getElementById(“inputText”).value.length); var compressedBytes = pako.gzip(document.getElementById(“inputText”).value); […]

SHA hash in NodeJS

const crypto = require(“crypto”);const filename = process.argv[2];const fs = require(“fs”); const hash = crypto.createHash(“sha256”); const input = fs.createReadStream(filename);input.on(“readable”, () => { const data =; if (data) { hash.update(data); } else { console.log(`${hash.digest(“hex”)} ${filename}`); }}); Reference

NodeJS Request

“use strict”;const fs = require(“fs”);const request = require(“request”);// Simple GETrequest(“http://localhost:14519/Test/GetHtml&#8221;, (err, resp, body) => { console.log(“err: ” + err); console.log(“statusCode: ” + resp.statusCode); console.log(“body: ” + body);});// Output GET results to filerequest(“http://localhost:14519/Test/GetJson&#8221;).pipe(fs.createWriteStream(“result.json”));// POST with form{ url: “http://localhost:14519/Test/PostParams&#8221;, form: { name: “John Smith” },}, (err, resp, body) => { console.log(“err: ” + err); console.log(“statusCode: ” […]

NodeJS read/write text files

“use strict”;(function() { var fs = require(“fs”); var readline = require(“readline”); fs.readFile(“input.json”, function(error, data) { var obj = JSON.parse(data); fs.writeFile(“output.json”, JSON.stringify(obj), function(err) { if (err) { console.log(“Error: ” + err); } else { console.log(“Saved”); } }); }); (function() { var outputStream = fs.createWriteStream(“output.txt”); var lineReader = readline.createInterface({ input: fs.createReadStream(“input.txt”) }); lineReader.on(“line”, function(line) { console.log(“Line: ” […]

NodeJS Read/Write Files

const fs = require(“fs”);const readline = require(“readline”);// Read full filefs.readFile(“myfile.txt”, { encoding: “utf-8” } ,(err, data) => { console.log(“Full file contents:”); console.log(data); console.log(“===================”);});// Read file line-by-linevar lineReader = readline.createInterface({ input: fs.createReadStream(“myfile.txt”) });lineReader.on(“line”, line => { console.log(`Line: ${line}`); });lineReader.on(“close”, () => console.log(“DONE”));// Write full filefs.writeFile(“myfile.txt”, “some file contents”, err => { if (err) console.error(err);});// Write file […]

NodeJS Line Reader

Sometimes I’ll get a text file to parse, where it’s too big or too complex for a spreadsheet, and not worth the trouble of importing to a database. Here’s a quick way to parse the file line-by-line with NodeJS. Here’s the same thing in C# View code on GitHub

TypeScript in NodeJS

It doesn’t take much to set up TypeScript in NodeJS. TypeScript’s type safety makes it much more enjoyable to write than plain JavaScript. If you’re new to TypeScript, check out the playground where you can see a bunch of examples of what TypeScript looks like compared to JavaScript. When importing NPM packages, you can almost […]