Fixing typescript error: TS2304: Cannot find name ‘require’

If you try to use “require” in a typescript file, you can get this error:

 TS2304: Cannot find name 'require'

Most likely, the best solution is for you to switch to using imports. For instance, instead of this:

let _ = require('lodash');

Do this:

import { map, groupBy, flatMap, keys, filter, find } from "lodash";

While this takes some time to convert, you get the advantage of much smaller Javascript files when bundled, because your tooling (e.g. webpack) will remove functions you aren’t using.

For reference, this is what I had to run on the command line to get this to work:

typings init
typings install 'dt!lodash'

And add a reference in my .ts file:

///