9

I'll preface by saying I don't have advanced knowledge of TypeScript or JavaScript.

What I did

I'm making a barebones TypeScript "algorithmic toy-box" that implements algorithms from Fundamentals of Algorithmics (Brassard and Bratley). What I do is open a local HTML file and the transpiled TypeScript modifies the DOM to show the output (just like the Greeter example on the TypeScript webpage).

Everything was going fine until I decided to use separate files for each class. I used one of the many ways available to reference TypeScript files, but I'm not sure if it was the best suited. I also created a default tsconfig.json file with the Atom TypeScript plugin thinking that the compiler would now assume all .ts in the directory are the same module or something, but I guess that wasn't the case.

main.ts:

import { Monticulo } from "./stuff/monticulo.ts"
import { ProgDin } from "./programacion-dinamica.ts"

let arr5_13 = [1,6,9,2,7,5,2,7,4,10]
let mon1 = new Monticulo(arr5_13)
// ...

document.body.innerHTML = mon1.console_debug

The problem

When I open the HTML file on a browser, the console says Uncaught ReferenceError: require is not defined.

Sure enough, the transpiled code has a require() call:

main.js:

var monticulo_ts_1 = require("./stuff/monticulo.ts");
var arr5_13 = [1, 6, 9, 2, 7, 5, 2, 7, 4, 10];
// ...

document.body.innerHTML = mon1.console_debug;

What I've tried

I initially ran it on Firefox 47.0, then I tried running in Chromium 51.0 (in case it was browser related) but I got the same error. I'm on Ubuntu 16.04 for the sake of completeness.

I've read that require() is a function that is not implemented client-side, yet Node has it implemented and it's needed for using npm modules in-browser, but why would TypeScript need to call any npm module? Why doesn't main.js just reference the transpiled .js instead of the .ts itself? I'm pretty sure I'm missing one or more pieces of information.

Community
  • 1
  • 1
JoseHdez_2
  • 4,040
  • 6
  • 27
  • 44

2 Answers2

3

You are probably compiling down to the commonJS module format (check your tsconfig.json)

That means it generates require function calls for you and it is your responsibility to provide a commonjs loader. You also probably want to bundle all your files into one. It just so happens that webpack does both and is very often used together with typescript :)

AlexG
  • 3,617
  • 1
  • 23
  • 19
  • As you said, the tsconfig.json is set to the commonJS format. I'll look into using webpack or checking out the other module formats, since I'm still unclear on whether using webpack is necessary for this case (this is just some small local html+js thing). Thanks! – JoseHdez_2 Jul 22 '16 at 16:43
  • 2
    I just found out that my question is basically a repeat of another, 3-month old question where the author explained themselves much more clearly than me and [got a similar answer](http://stackoverflow.com/a/36711015/3399416)... I guess I'll go with webpack then. – JoseHdez_2 Jul 22 '16 at 16:50
  • 1
    if you have like 3 files, it may seems overkill indeed. You could compile an entire folder with tsc, then use some kind of concatenation tool that will put the files in the order you specify it. There is a huge disadvantage to this solution though: it doesn't scale. webpack figures out the dependencies of every modules and is not too difficult to setup :) – AlexG Jul 22 '16 at 17:02
1

You can remove the need for require by replacing

import { Monticulo } from "./stuff/monticulo.ts"
import { ProgDin } from "./programacion-dinamica.ts"

with

/// <reference path="./stuff/monticulo.ts" />
/// <reference path="./programacion-dinamica.ts" />

See Triple Slashes for more info on using the triple slash imports.

You will also want to remove all export class and replace with just public class.

Brant Olsen
  • 5,628
  • 5
  • 36
  • 53
  • 1
    This doesn't work if the referenced file contains an `export` statement. – Toast Oct 31 '17 at 16:22
  • You would not use exports with the triple slash. You only use exports with the import way of including files. I will add a comment to indicate that. – Brant Olsen Nov 01 '17 at 14:14
  • You're right. My point is, you can't use triple slashes if you reference any library in your project. – Toast Nov 01 '17 at 18:22