admin管理员组

文章数量:1320661

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

{
  "pileOnSave": true,
  "pilerOptions": {
    "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.

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

{
  "pileOnSave": true,
  "pilerOptions": {
    "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.

Share Improve this question asked Apr 7, 2017 at 12:51 kockburnkockburn 17.6k10 gold badges90 silver badges141 bronze badges 6
  • Maybe one of these will help you github./wallabyjs/public/issues/845 or github./wallabyjs/public/issues/160 – Ionut Necula Commented Apr 7, 2017 at 13:00
  • 2 @lonut thanks for the links, however they were not what I was looking for. – kockburn Commented Apr 7, 2017 at 13:18
  • Can you show us your Gruntfile? Are you using System.js (is there a reference to system.js and/or system.js.config somewhere in your project)? I've encountered the same problem and have managed to fix it. – ACOMIT001 Commented Apr 9, 2017 at 6:43
  • @ACOMIT001 if no module is specified then I believe by default monjs is chosen. I'm using the tsc piler and not grunt. I've also tried by specifying the system module, but was unable to get it to work properly. What was your solution? – kockburn Commented Apr 9, 2017 at 7:23
  • 1 I was actually having a similar problem with Jasmine. Creating a file with exports defined managed to fix the issue. – ACOMIT001 Commented Apr 9, 2017 at 13:07
 |  Show 1 more ment

2 Answers 2

Reset to default 1

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

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

$ tsc --version

Version 3.5.3
  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",

本文标签: javascriptReferenceError Can39t find variable exportsStack Overflow