1

I had problem with LODASH importing to Angular2 app, got answer on this post here and all working now.

Next problem I have is that I do not know how to make "twitter" npm module to work within the app.

Now I installed "npm install twitter" and tried to import it with:

import * as Twitter from 'twitter';

but I get "cannot find module 'twitter'. I tried adding to system.config without luck.

What am I doing wrong here? How should the code look like for import and using this twitter module?

Here is system.config in index.html:

  System.config({
    packages: {
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    },
    map: {
      lodash: 'node_modules/lodash/lodash.js',
      twitter: 'node_modules/twitter/index.js'
    },
    meta: {
      lodash: { format: 'amd' },
      twitter: { format: 'amd' }
    }
  });
Community
  • 1
  • 1
Milan Milanovic
  • 433
  • 1
  • 7
  • 14

3 Answers3

1

I believe you have to add the definition file of the twitter module to a typings folder. Or install it using tsd. After that you can use it in your typescript code using a reference:

download manually

  1. Download the definition file from here
  2. Add it to a typings folder in your source root

or

install using tsd

  1. Open cmd in source folder
  2. npm install tsd -g
  3. tsd install twitter --save

After that add the reference in the top of your typescript file

/// <reference path="../typings/twitter.d.ts"/>
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
0

The twitter module is a commonjs module, so it won't work with amd. Try the following config to see if it works:

System.config({
  packages: {
  app: {
    format: 'register',
    defaultExtension: 'js'
  }
},
map: {
  lodash: 'node_modules/lodash/lodash.js',
  twitter: 'node_modules/twitter'
},
meta: {
  lodash: { format: 'commonjs' },
  twitter: { format: 'commonjs' }
}
});

try the import like this:

import * as Twitter from 'twitter/index';

Does it work? I don't think lodash is an amd module as well, its also commonjs. You probably can remove the full meta section, systemJs will auto-detect the type of module system successfully most of the times.

Angular University
  • 42,341
  • 15
  • 74
  • 81
0

I don't think you could use this module in such scenario as its a server-side module and needs Node running. I've managed to implement twitter authentication using other module: https://github.com/jublonet/codebird-js

I've been working for a while on a simple project implementing social media logins. If you want you can take a look at: https://github.com/bkulov/EventOrgApp

bkulov
  • 471
  • 6
  • 6