简体   繁体   English

使用 tsc 使用 esm 编译 TypeScript 时出现“找不到模块”

[英]“Cannot find module” when using tsc to compile TypeScript using esm

I have an esm package.json like so:我有一个package.json像这样:

{
  "main": "config.js",
  "type": "module",
  "scripts": {
    "start": "npm run build && node ./build/config.js",
    "build": "tsc",
    ...

and a tsconfig.json :和一个tsconfig.json

{
  "compilerOptions": {
    "target": "ES2015",
    "module": "ES2020",
    "strict": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "outDir": "./build"
  },
  "include": ["./src", "config.ts"],
  "exclude": ["./*/**.spec.js"]
}

When running npm run start I get the following error:运行npm run start时出现以下错误:

> tsc

internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'F:\Development\test\example\build\src\handlers\index' imported from F:\Development\test\example\build\config.js
    at finalizeResolution (internal/modules/esm/resolve.js:277:11)
    at moduleResolve (internal/modules/esm/resolve.js:658:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:748:11)
    at Loader.resolve (internal/modules/esm/loader.js:97:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:243:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
    at link (internal/modules/esm/module_job.js:41:36) {
  code: 'ERR_MODULE_NOT_FOUND'

The build directory looks like so:构建目录如下所示:
在此处输入图像描述

I've tried already changing the type to commonjs and other configuration changes, what am I doing wrong?我已经尝试将类型更改为 commonjs 和其他配置更改,我做错了什么?

Appearently even though you are using typescript, we need to change the imports to .js , so in my example:显然,即使您使用的是 typescript,我们也需要将导入更改为.js ,所以在我的示例中:

import IndexHandler from "./src/handlers/index";
becomes:变成:
import IndexHandler from "./src/handlers/index.js"; . .

It isn't considered a bug but part of TypeScript.它不被视为错误,而是 TypeScript 的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿错误 -- 错误:找不到模块“打字稿/tsc.js” - Typescript error -- Error: Cannot find module 'typescript/tsc.js' 将 typescript 应用程序发布到 azure 时找不到模块 '../lib/tsc.js' - Cannot find module '../lib/tsc.js' when releasing typescript application to azure 即使 tsc 成功解决了它,也找不到 Typescript 模块 - Cannot find Typescript module even though tsc successfully manages to resolve it TypeScript:tsc.exe --execute:错误找不到模块 - TypeScript: tsc.exe --execute: Error cannot find module 是否可以使用tsc命令将单个TypeScript文件编译到输出路径? - Is it possible to compile a single TypeScript file to an output path using the tsc command? 无法使用webpack或TSC编译和发布我的Typescript库 - Unable to compile and publish my Typescript library using webpack or TSC LernaJs tsc 找不到模块 - LernaJs tsc Cannot find module sequelize-typescript - 使用带有 `tsc` 编译器(带有 NestJS)的 glob 配置时无法解析模型模块 - sequelize-typescript - When using glob configuration with `tsc` compiler (with NestJS) cannot resolve model modules Webpack / Vue.js:在编译时使用 ESM 依赖项生成模块代码 - Webpack / Vue.js: generate module code at compile-time using ESM dependencies 在 TypeScript 中导入时“找不到模块” - 'Cannot find module' when import in TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM