2

I'm trying to use the Dictionary class from from typescript-collections within NodeJS:

/// <reference path="../../../scripts/collections.ts" />
var collections = require('../../../scripts/collections');

export class AsyncProcessorCommand implements interfaces.IAsyncProcessorCommand
{
    public static Type = 'RabbitMon.AsyncProcessorCommand:RabbitMon';
    public GetType = () => 'RabbitMon.AsyncProcessorCommand:RabbitMon';

    public ID: string;

    constructor(public Action: string, public Arguments?: collections.Dictionary<string, string>) {
        this.ID = uuid.v4();

        this.Arguments = new collections.Dictionary<string, string>();
        //I've also tried the following
        //this.Arguments = new collections.Dictionary<string, string>((key: string) => sha1(key));
    }
}

But I keep getting the following error on the new Dictionary:

TypeError: undefined is not a function

Anyone know what's going on here? I'm also perfectly happy to substitute a better working TS collections library...

1 Answer 1

0

You have an internal vs external modules problem.

The TypeScript Collections library is written as an internal module -- a standard JavaScript file that you could just throw in to a script tag in a webpage.

Node's require, however, is expecting a CommonJS-compatible file that will assign something to exports, in other words an external module. What's happening is that node.js finds collections.js, executes it, and returns you back the exports object from evaluating the file. Because it's just a regular JS file, the exported object is {} -- empty.

The best fix would be:

  1. Replace your reference to collections.ts with one to collections.d.ts, just for correctness's sake (run tsc --d collection.ts to generate this file)
  2. Use some solution for loading "vanilla" JS files in node. A good one-liner (from the linked question) is eval(require('fs').readFileSync('./path/to/file.js', 'utf8'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.