TypeScript - ReferenceError: define is not defined error
How Can I fix the following error?
define(['require', 'exports', 'ts-polyfill/lib/es2017-string'], function (
^
ReferenceError: define is not defined
When you compile TypeScript application to JavaScript we can use different module types - import
/export
 may be converted depending on module type.
e.g. "module": "AMD"
 - for web browser JavaScript
The below solution causes generating define()
function calls in output JavaScript source code during TypeScript compilation. AMD
modules require to attach require.js
file that defines define()
function.
Solutuion: add the following line in the head
element to the index.html
:
<script src="https://requirejs.org/docs/release/2.3.6/comments/require.js"></script>
Example index.html
 file:
<!doctype html>
<html>
<head>
<script src="https://requirejs.org/docs/release/2.3.6/comments/require.js"></script>
<!-- or: https://requirejs.org/docs/release/2.3.6/minified/require.js -->
<script src="path/to/application.js"></script>
</head>
<body>
<script>
// Application running ...
</script>
</body>
</html>
Note: check this article too see example configuration.
e.g. "module": "CommonJS"
 - for Node.js JavaScript
The below solution causes generating require()
function calls in output JavaScript source code during TypeScript compilation - that source code is ready to run under Node.js (require()
is supported natively by Node.js).
Solution:
- go toÂ
tsconfig.json
file - find
"compilerOptions"
 and change"module"
value to"CommonJS"
Example tsconfig.json
file:
{
"compilerOptions": {
// ...
"module": "CommonJS"
// ...
},
// ...
}