I have a web project which uses requireJS with TypeScript and the base URL is set as
/Scripts. Most Nuget packaged script libraries (jquery, etc.) install the script into
/Scripts so I try to keep my own code separate in sub folders.
For example all the scripts used by views are in the
/Scripts/Views folder, e.g.
/Scripts/Views/Home.ts. Various libraries I have created exist in sub-folders off the script directory: for example my KnockoutJS related code lives in
/Scripts/ko and Knockout components in
When writing a view script that needs a library you can then reference it thus:
import bindingHandlers = require("ko/bindingHandlers");
All so good so far. However, I also have code in a different folder which is not a sub-folder of
Scripts – and this is where TypeScript breaks down. In versions 1.x it will only walk up the current path to look for modules, and therefore cannot find the libraries.
For example, I have a folder
/Products/Broadband with a script
Order.ts If I start this with the require statement
require("ko/bindingHandlers"); this fails. TypeScript will look in the current directory, then /Products and finally / – and none of these work.
At present there isn’t a way to fix this short of moving all scripts into the /Scripts folder, but there is a proposed change in TypeScript 2.0 that will support a
paths option in
tsconfig.json configurations. This will allow the non-relative module references to resolve correctly.