Uncaught ReferenceError: exports is not defined and require

23,904

Solution 1

I use angular 1.6 and webpack, it works on my side when i changed the module to "umd"

UMD: Universal Module Definition This has brought about the push for a “universal” pattern that supports both styles (AMD and CommonJS)

reference: http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/

pasted here is my tsconfig.json after I changed it, I run $ tsc

{
  "compilerOptions": {
    "target": "es5",
    "module": "umd",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "allowUnreachableCode": true
  },
  "exclude": [
    "node_modules"
  ]
}

then my "exports is not defined" error is gone.

Solution 2

click here "Solved finally" i was also getting same error i changed module:"commonjs" to "module": "es6", because targeting ES5 remove the import/export statements, so these tools cannot remove unused exports.

{
    "compilerOptions": {
    "target": "es5",
    "module": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
    }

}
Share:
23,904
hackp0int
Author by

hackp0int

May the the code force will be with you...

Updated on July 09, 2022

Comments

  • hackp0int
    hackp0int almost 2 years

    I'm using angularjs and typescript to create some app, I'm having a trouble with this error that I can't solve

    Here is my *.ts code

    export var NgApp = new application.Startup();
    
    ///<reference path="../../../../../typings/tsd.d.ts"/>
    import {NgApp} from "../../bootstrap";
    
    
    
    module views.components.home {
        export class HomeComponent {
            public constructor() {
            }
    
            public static factory():Function[] {
                return [
                    () => new HomeComponent()
                ]
            }
    
        }
    
    }
    
    NgApp.registerComponents(views.components.home,() => this.app.controller);
    

    Here is my GULP task

    let tsResult = gulp.src('src/**/*.ts').pipe(ts({
        module: 'commonjs',
        sourceMap: true
    }));
    
    let taskTs = tsResult.js.pipe(gulp.dest('built/'));
    

    This is my error: Uncaught ReferenceError: exports is not defined

    The question is: How can I use import like es6 in typescript? What I am missing?

  • hackp0int
    hackp0int over 8 years
    sorry mate I use angularjs 1.5 and actually not really familiar with webpack, can I use systemjs with the same abilities to load modules?
  • basarat
    basarat over 8 years
    You can. But it is much more bleeding edge (and needless for me as webpack already does everything I need it to)