A
A
Alexey2017-09-28 18:54:54
JavaScript
Alexey, 2017-09-28 18:54:54

Typescript compilation. How to solve "Uncaught ReferenceError: exports is not defined"?

Compilation succeeds using Gulp with the gulp-typescript plugin.
Unfortunately, when running the code in the browser, an error occurs:
Compiled JS has this line "Object.defineProperty(exports, "__esModule", { value: true });"
Accordingly, the error is: " main.js:2 Uncaught ReferenceError: exports is not defined "
tsc 2.4.2
The actual code is this:

main.ts

import * as _ from 'lodash';
import * as $ from 'jquery';

tsconfig.json
{
  "files": [
    "../../web/ts/main.ts"
  ],
  "compileOnSave": false,
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
   "emitDecoratorMetadata": true,
   "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
   "noImplicitAny": true,
   "removeComments": true,
   "preserveConstEnums": true,
   "allowUnreachableCode": true
  },
  "exclude": [
    "node_modules"
  ]
}

gulpfile.js
var tsProject = ts.createProject("tsconfig.json");
gulp.task("typescript", function () {
    return tsProject.src()
        .pipe(tsProject())
        .js.pipe(gulp.dest(paths.jsFolder));
});

Where did I make a mistake?
And what is the right way to do imports taking into account the configuration of modules in the tsconfig.json file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danakt Frost, 2017-09-28
@azovl

The error is due to the fact that you do not have commonjs connected, which you specified in tsconfig.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question