kockburn
kockburn

Reputation: 17616

ReferenceError: Can't find variable: exports

Question:

Why am I getting the following error? Did I forget to include a script in my html?

ReferenceError: Can't find variable: exports

Generated javascript from typescript that causes it:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* more code */

Extra:

tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "noImplicitAny": true,
    "rootDir": ".",
    "sourceRoot": "../../../",
    "outDir": "../../../js/dist/",
    "sourceMap": false
  },
  "exclude": [
    "node_modules"
  ]
}

requirejs is included before my js files in html

There are similar questions but this is simply about typescript and not about ember/babel/etc.

Upvotes: 15

Views: 8088

Answers (2)

ivaigult
ivaigult

Reputation: 6667

  1. Change module code generation option to es6 in tsconfig.json
"module": "es6",

So, the code generated by the tsc for import is supported by the browser.

  1. Make sure that your <script> tag has type="module":
<script src="index.js" type="module"></script>
  1. Use importmap in your .html to resolve the module paths:
<script type="importmap">
{
    "mymodule": "/path/to/mymodule.js",
}
</script>

or even load it from CDN:

"mymodule": "https://thecdnlink",

Upvotes: 0

Nickolay
Nickolay

Reputation: 32063

I can't reproduce. Your tsconfig.json causes tsc to bail with

error TS5051: Option 'sourceRoot can only be used when either option '--inlineSourceMap' or option '--sourceMap' is provided.

Once I remove the sourceRoot option, there are no references to exports in the output.


$ ls

my.ts       tsconfig.json

$ cat my.ts

console.log(1)

$ cat tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "noImplicitAny": true,
    "rootDir": ".",
    "sourceRoot": "../../../",
    "outDir": "../../../js/dist/",
    "sourceMap": false
  },
  "exclude": [
    "node_modules"
  ]
}

$ tsc --version

Version 3.5.3

Upvotes: 1

Related Questions