Typescript 无法找到模块,但实际上它是存在的

huangapple go评论77阅读模式
英文:

Typescript cannot find module but it is actually present

问题

我正在尝试编写一个非常简单的TypeScript项目,但似乎无法工作。这是我的项目结构(包括dist文件夹):

├── dist
│   ├── hello.js
│   └── index.js
├── package-lock.json
├── package.json
├── src
│   ├── hello.ts
│   └── index.ts
└── tsconfig.json

我的hello.ts文件:

export function sayHello() {
  console.log("Hello");
}

我的index.ts文件:

import { sayHello } from "./hello";

sayHello();

我的tsconfig.json文件:

{
  "compilerOptions": {
    "rootDirs": ["src"],
    "outDir": "dist",
    "lib": ["es2020"],
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "types": ["node"]
  }
}

我的package.json文件:

{
  "name": "ts-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "compile": "tsc",
    "start": "npm run compile && node ./dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "module",
  "devDependencies": {
    "@types/node": "^20.4.9",
    "typescript": "^5.1.6"
  }
}

我遇到的错误是:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '****/ts-test/dist/hello' imported from ****/ts-test/dist/index.js

我漏掉了什么?

英文:

I am trying to write a very simple Typescript project but it does not seems to work. Here's my project structure

(including also the dist folder):

├── dist
│   ├── hello.js
│   └── index.js
├── package-lock.json
├── package.json
├── src
│   ├── hello.ts
│   └── index.ts
└── tsconfig.json

my hello.ts:

export function sayHello() {
  console.log("Hello");
}

my index.ts:

import { sayHello } from "./hello";

sayHello();

my tsconfig.json:

{
  "compilerOptions": {
    "rootDirs": ["src"],
    "outDir": "dist",
    "lib": ["es2020"],
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "types": ["node"]
  }
}

my package.json:

{
  "name": "ts-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "compile": "tsc",
    "start": "npm run compile && node ./dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "module",
  "devDependencies": {
    "@types/node": "^20.4.9",
    "typescript": "^5.1.6"
  }
}

The error I am getting is:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '****/ts-test/dist/hello' imported from ****/ts-test/dist/index.js

What am I missing?

答案1

得分: 0

请更新您的tsconfig.json文件,包括以下内容:

{
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "dist",
    // ...
  }
}
英文:

TRY Update your tsconfig.json to include:

{
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "dist",
    // ...
  }
}

答案2

得分: 0

尝试将您的导入更改为以下方式:

import { sayHello } from "./hello.js";
英文:

Try change your import like this:

import { sayHello } "./hello.js";

答案3

得分: 0

rootDir的定义:

> 重要的是,rootDir不会影响哪些文件包含在编译中。它与tsconfig.json中的include、exclude或files设置没有交互作用。

很可能您还需要"include": ["src/**/*"]来告诉TypeScript在编译中包含src中的所有文件(包含文档)。

英文:

From rootDir definition:

> Importantly, rootDir does not affect which files become part of the compilation. It has no interaction with the include, exclude, or files tsconfig.json settings.

Most likely you also need "include": ["src/**/*"] to tell typescript to include all files in src in the compilation (include documentation).

huangapple
  • 本文由 发表于 2023年8月9日 14:33:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865144-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定