最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught ReferenceError: exports is not defined when importing function - TypeScript - Stack Overflow

programmeradmin3浏览0评论

I'm learning TypeScript. I've imported the output function from console.ts:

export function output(value: any): void {
  console.log(value);
}

Which piles to console.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var console_1 = require("./console");
console_1.output('Hello');
//# sourceMappingURL=functional.js.map

Importing and usage in destination file:

import {output} from "./console";
output('Hello');

tsconfig.json:

{
  "pilerOptions": {
    "target": "es5",
    "module": "monjs",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ]
}

I got no piling error, but not understanding why I got the following error in the browser:

Uncaught ReferenceError: exports is not defined at functional.js:2

I'm learning TypeScript. I've imported the output function from console.ts:

export function output(value: any): void {
  console.log(value);
}

Which piles to console.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var console_1 = require("./console");
console_1.output('Hello');
//# sourceMappingURL=functional.js.map

Importing and usage in destination file:

import {output} from "./console";
output('Hello');

tsconfig.json:

{
  "pilerOptions": {
    "target": "es5",
    "module": "monjs",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ]
}

I got no piling error, but not understanding why I got the following error in the browser:

Uncaught ReferenceError: exports is not defined at functional.js:2
Share Improve this question edited Jul 27, 2023 at 13:04 Anas Abu Farraj asked Jul 26, 2018 at 13:12 Anas Abu FarrajAnas Abu Farraj 1,5984 gold badges23 silver badges32 bronze badges 4
  • 1 how are you bundling your files? export, import only work in nodejs, to run in browser you need a module bundler like webpack – Anurag Awasthi Commented Jul 26, 2018 at 13:14
  • I'm Using IntelliJ with TypeScript built-in and some plugins..nothing special everything is simple. – Anas Abu Farraj Commented Jul 26, 2018 at 13:22
  • bowser doesn't have imports or exports. If you want to test your code, run it in node.js. – Anurag Awasthi Commented Jul 26, 2018 at 13:24
  • @nrgwsth even when test in node: SyntaxError: Unexpected token import – Anas Abu Farraj Commented Jul 29, 2018 at 10:08
Add a ment  | 

2 Answers 2

Reset to default 8

I had a similar issue in a Vue.js / TypeScript project. Vue.js project used Webpack under the hood and I've run npm link during an external module development.

Eventually, I've discovered the issue was caused by Webpack trying to follow the symbolic link to the dependency I've linked with npm link mand.

The solution was to instruct Webpack to stop following symbolic links.

Relevant Webpack documentation section: resolve.symlinks.

Relevant Vue.js troubleshooting section: Vue.js and npm link.

I had the exact same issue. Telling webpack to stop following the symlink helped, here is the snippet in vue.config.js

chainWebpack: config => {
  config.resolve.symlinks(false);
},
发布评论

评论列表(0)

  1. 暂无评论