Tuesday, May 29, 2018

Fixing NPM Global Packages

I recently got a new Windows computer and when I opened up an old Angular project I realized that I hadn't installed Angular CLI yet. So I installed it globally and tried it again. It still wouldn't work. When I typed "ng" on the command line it said it was not recognized as a command. I tried some other packages I know I had installed globally, like TypeScript. That didn't work either. WTF man!

Finally I discovered that there was a nodejs command prompt installed so I opened that and it worked from there. However that only fixed it for running from that particular command prompt. If I tried to run it in VS Code it still wasn't working.

So I opened up the batch file for the nodejs command prompt (C:\Program Files\nodejs\nodevars.bat) and noticed that it was adding npm to the path system variable. I looked at my path and I only had the nodejs path. So I added the path to npm, which I copied from nodevars.bat, and that fixed it.

Now that I think about it, this also solves a problem I had a few weeks ago when I created my own node app and installed it, but it wouldn't run from the command line. I figured I was just doing something wrong when configuring my app, but now I know I was right all along. Vindication!

Code Hard!

Tuesday, April 17, 2018

Node.js + TypeScript = Awesome

Node.js + TypeScript = Awesome

You can use TypeScript to write maintainable Node.js apps. Here's how to set up a project in VS Code.

Create the Project

First create a project folder. Next I like to use "npm init" to set up the package.json file for the project. This will make it easy to track your npm dependencies.

Now you can open the project folder in VS Code.

Set up TypeScript Compiler

To set up the compiler create a file called tsconfig.json and add the following into it.

    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "sourceMap": true

The important bit is to set the module to "commonjs" because this is the module loading system used by Node.js.

Start Programming

Create your main entry point TypeScript file. I usually call mine "main.ts".

Now you can add any supporting TypeScript files. To turn on the TS compiler to watch for changes and automatically compile go to the Tasks menu, select Run Build Task and select "tsc: watch" from the menu.

Although you can use the require keyword in your TypeScript files to import npm packages, you should use a TS import statement instead. Otherwise you will not get code hints.

import * as fs from "fs";

JavaScript Main File

You may want your main file to be a JavaScript file (don't ask me why). In that case you can import files generated by TypeScript using require. For example, if you have a TS file named myModule.ts then you would import it like so. Note that it must start with "./" so it knows it's a local module not an npm module.

let myModule = require("./myModule");

Note also that you probably don't want to use "export default" in your TypeScript file or you will have to reference the default item using the default keyword, which is a little awkward...

let something = myModule.default();

Code hard!