Languages

TypeScript - ReferenceError: define is not defined error

3 points
Asked by:
a_horse
538

How Can I fix the following error?

define(['require', 'exports', 'ts-polyfill/lib/es2017-string'], function (
^

ReferenceError: define is not defined
1 answer
3 points
Answered by:
a_horse
538

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:

  1. go to tsconfig.json file
  2. find "compilerOptions" and change "module" value to "CommonJS"

Example tsconfig.json file:

{
    "compilerOptions": {
        // ...
        "module": "CommonJS"
        // ...
    },
    // ...
}
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join