The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

TypeScript - Using Typings and Loading From node_modules

TypeScript - Using Typings and Loading From node_modules

4:44
Using TypeScript when installing packages from npm often requires you to install related definition files. This lesson shows you how to use typings to install es6-shim then how to configure SystemJS to load from node_modules.
Watch this lesson now
Avatar
egghead.io

Using TypeScript when installing packages from npm often requires you to install related definition files. This lesson shows you how to use typings to install es6-shim then how to configure SystemJS to load from node_modules.

Avatar
moti zharfati

Great Tutorial John!
It's seem to me very confusing to make it work, we have to set too much configuration in order to install lodash and rxJs, in the old way everything was easy: "script:src to remote lodash" (on the client, or i can use gulpfile to bundle everything) and that's it! I hope someone will improve it...

Avatar
Michael Peake

IMO, it's much easier to get it all working with Webpack, rather than with SystemJS. Just my preference though.

In reply to moti zharfati
Avatar
Michael

Really enjoy yet another great course from you John! Just want to note that npm install rxjs --save will install rxjs@5.0.0-beta.8 and the example with subscribing to the 1s observable interval in this clip won't run. However, revert it back to rxjs@5.0.0-beta.6 will do the trick.

Avatar
Alexander

You could also do "npm install symbol-observable" and adjust the System.config in the index.html.
In packages you would need to add:
"symbol-observable": {
"defaultExtension":"js",
"main": 'index.js'
}
and in map:
"symbol-observable": "node_modules/symbol-observable",

Maybe this helpful for someone.

In reply to Michael
Avatar
Paul

When I run the new command "typings install dt~es6-shim --save", I get this error.

typings ERR! message Attempted to compile "es6-shim" as an external module, but it looks like a global module. You'll need to enable the global option to continue.

typings ERR! node -v v6.2.0
typings ERR! typings -v 1.3.0

Any suggestions?

Avatar
G

Make sure to run it with the --global flag:
typings install dt~es6-shim --save --global

The --global flag replaced the --ambient flag in typings v1.0. It would be good for them to update the alert-warning box with this information.

For reference:

The --global flag tells Typings to accept type definitions that are "global" in nature. Currently, DefinitelyTyped typings are all "global". (src)

In reply to Paul
Avatar
G

IMO, it's much easier to get it all working with Webpack, rather than with SystemJS. Just my preference though.

I second this preference. I understand they were trying to limit the videos to focus on TypeScript, but I suspect that many viewers–particularly people brand new to TypeScript–may have been lost in the weeds of getting everything set up properly.

This is more of an issue with how new TypeScript is and how much of it's ecosystem is still in flux (you'll see that this video series is less than two weeks old yet multiple parts of it are already outdated). For what they had to work with, it's a good starter series.

In reply to Michael Peake
Avatar
G

It looks like symbol-observable is installed with rxjs automatically. However, you are correct in that I did have to add it to the "packages" object as well as "map".

An interesting add-on to this: if you include a "main" file for the "rxjs" package as "Rx.js", in main.ts you are able to say

import * as Rx from "rxjs";

Which allows you to write

Rx.Observable.interval(4000).subscribe(x => console.log(x));

Without having to import anything else from "rxjs".

In reply to Alexander
Avatar
Sequoia McDowell
  1. How are RX.js types loaded/accessed from webstorm? Your .tsconfig.json excludes node_modules... what's going on here?
  2. Why are you not getting code-hinting when you use RX types?
Avatar
Akshar

Thanks! This helped me out.

In reply to Alexander
Avatar
John
  1. tsconfig.json excludes is simply telling the TypeScript compiler to not compile files in node_modules
  2. WebStorm has recently updated to support code-hinting based on the RxJS project structure.
In reply to Sequoia McDowell
Avatar
John

Yes, Webpack hides a lot of what is really happening behind whichever loader you're using. So many people never learn (or simply never need to learn) that TypeScript compiles to a module format then you need to set up your own loader. I think it's an important distinction to understand, even if you don't need to deal with it because Webpack handles it so nicely.

In reply to Michael Peake
Avatar
Jason

I ran into an issue where current beta (5 10) of RXJS was causing an error. When I specified beta 6, issue was resolved. See this thread for a similar issue: https://github.com/IgorMinar/new-world-test/issues/13

Avatar
Yuri

Take into account that you should also add a global flag in order to install es6-shim

typings install dt~es6-shim --save --global

Avatar
Alberto

I'm having issues working with Typescript. My setup and versions are:
package.js - rxjs": "^5.0.0-beta.6"
tsconfig.json - "files": ["main.ts"]
main.ts - Contains only this two lines:

import {Observable} from 'rxjs/Observable';
Observable.fromArray([1, 2]);

And complains about not finding module "rxjs/Observable".

What am I missing?

Avatar
Ningze

I got an error when I put "Rx.Observable.timer(1000).subscribe( x => console.log(x) );" in main.ts

The error is : system.src.js:5031 Uncaught (in promise) Error: SyntaxError: Unexpected token <
at Object.eval (http://localhost:8080/dist/main.js:3:10)
at eval (http://localhost:8080/dist/main.js:16:4)
at eval (http://localhost:8080/dist/main.js:17:3)
Evaluating http://localhost:8080/node_modules/rxjs
Evaluating http://localhost:8080/dist/main.js
Error loading http://localhost:8080/dist/main.js

I compare the code line by line, but still cannot make it run. I think there is something wrong with es6-shim , I run "typings install dt~es6-shim --save --global
" to install es-shim .

Avatar
Wenyuan

I got the same error as you, and I solve it by adding "main": "Rx" to packages configuration, so it's like this:
"rxjs": {
"defaultExtension": "js",
"main": "Rx"
}

In reply to Ningze
Avatar
Stephen James

This one worked for me - thanks.

In reply to Wenyuan
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?