首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编译用tsc运行节点,ReferenceError:导出未在ES模块作用域中定义

编译用tsc运行节点,ReferenceError:导出未在ES模块作用域中定义
EN

Stack Overflow用户
提问于 2022-10-20 15:25:34
回答 1查看 20关注 0票数 0

这个问题以前问过很多次了,但我找不到解决办法。

我在我的项目中使用打字稿。

我有我的index.ts文件,其中包含如下代码:

代码语言:javascript
运行
复制
import { getEnvOptions } from './env/envreader';
import express, {Express, Request, Response} from 'express';

当我使用命令tsc编译它并尝试使用node运行它时

我说错了:

代码语言:javascript
运行
复制
Object.defineProperty(exports, "__esModule", { value: true });
ReferenceError: exports is not defined in ES module scope

据我所知,问题在于节点运行时环境不识别由类型记录编译器生成的关键字"exports“。我打算在服务器上运行这个项目,而不是在浏览器中运行,这就是为什么我将“模块”设置为"CommonJS“。

以下是tsconfig:

代码语言:javascript
运行
复制
{
    "compilerOptions": {
  

      "target": "ES5",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */

      "module": "CommonJS",                                /* Specify what module code is generated. */

      "moduleResolution": "node",                       /* Specify how TypeScript looks up a file from a given module specifier. */

       "allowJs": true,                                  /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */

       "noEmit": false,                                   /* Disable emitting files from a compilation. */

      "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

      "strict": true,                                      /* Enable all strict type-checking options. */

      "skipLibCheck": true                          /* Skip type checking all .d.ts files. */
    },
    "include": ["./**/*.ts"],
    

  }

如果我想继续使用typescript导入语句,那么在节点环境中应该做什么才能使它运行呢?

EN

Stack Overflow用户

回答已采纳

发布于 2022-10-22 13:27:33

从package.json中删除"type":“模块”,因为您可以使用导入,而无需指定package.json中的导入,原因是导入是类型记录的一部分。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74142461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档