0

I have written a very simple node module [ npm install twinconsole ] and published to npm.

I also included browser build ( umd module ) so that it can be used from browser as well.

Below is node module related code

module.exports.print = msg => {
console.log(msg);
}

Now I would like to use this node module from my Angular 2 typescript application, to do that

  1. I have included below CDN file in index.html.

    < script src="https://npmcdn.com/[email protected]/dist/index.umd.min.js">

What is that I need to do so that print() function exported in node module can be used in below root component

import { Component } from '@angular/core';

declare var twinconsole: any;  // ADDEED THIS LINE , IS THIS CORRECT ?


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  title = 'app works!';
}
1
  • Yes your declare statement should work just fine. I've done similar declarations of CryptoJS for encryption. I find it useful to create an interface for the declared vars if their APIs are more extensive. This allows editors like VSCode to still provide a nice development experience because they expose the interface via autocomplete. Commented Mar 22, 2017 at 8:32

1 Answer 1

1
import { Component } from '@angular/core';

//define interface to get "intellisense" aka autocomplete and type errors
interface TwinConsole{
  print(msg:string);
}

declare var twinconsole: TwinConsole;


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  title = 'app works!';
}
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.