{"id":3982,"date":"2016-04-29T14:00:28","date_gmt":"2016-04-29T14:00:28","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3982"},"modified":"2016-04-29T14:00:28","modified_gmt":"2016-04-29T14:00:28","slug":"fixing-typescript-error-ts2304-cannot-find-name-require","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/fixing-typescript-error-ts2304-cannot-find-name-require\/","title":{"rendered":"Fixing typescript error: TS2304: Cannot find name &#8216;require&#8217;"},"content":{"rendered":"<p>If you try to use &#8220;require&#8221; in a typescript file, you can get this error:<\/p>\n<pre>\n TS2304: Cannot find name 'require'\n<\/pre>\n<p>Most likely, the best solution is for you to switch to using imports. For instance, instead of this:<\/p>\n<pre lang=\"javascript\">\nlet _ = require('lodash');\n<\/pre>\n<p>Do this:<\/p>\n<pre lang=\"javascript\">\nimport { map, groupBy, flatMap, keys, filter, find } from \"lodash\";\n<\/pre>\n<p>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&#8217;t using.<\/p>\n<p>For reference, this is what I had to run on the command line to get this to work:<\/p>\n<pre lang=\"bash\">\ntypings init\ntypings install 'dt!lodash'\n<\/pre>\n<p>And add a reference in my .ts file:<\/p>\n<pre lang=\"javascript\">\n\/\/\/ <reference path=\"typings\/main.d.ts\" \/>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to convert &#8216;require&#8217; usage into ES6 utilities with typescript files<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9],"tags":[302,557,592],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3982"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3982"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3982\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}