“错误:无法从打字稿中的相对路径找到模块”

我的 index.ts 文件从 bindings.d.ts 文件导入了一个类,但我在运行时得到了 Error: Cannot find module '../bindings',没有来自 TS 的编译时错误。

我的bindings.d.ts

export class MyClass {
  sayHello(): string;
}

我的index.ts

import { MyClass } from "../bindings";

console.log(MyClass);

我的tsconfig.json

{
  "compilerOptions": {
    "target": "es6","module": "commonjs","outDir": "./dist","rootDir": "./src","strict": true,"moduleResolution": "node","typeRoots": [
        "@types","./node_modules/@types"
    ],"esModuleInterop": true,"resolveJsonmodule": true,"skipLibCheck": true,"forceConsistentCasingInFileNames": true,}
}

我的文件树:

- src
  - index.ts
- bindings.d.ts
- tsconfig.json
- node_modules
- @types

运行 ts-node src/index.ts 时,出现以下错误:

Error: Cannot find module '../bindings'
Require stack:
- /Users/slider/Desktop/myProject/src/index.ts
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (/Users/slider/Desktop/myProject/src/index.ts:4:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1043:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1046:12)
    at Module.load (internal/modules/cjs/loader.js:950:32)

我也尝试将 "ts-node": { "files": true } 添加到 tsconfig.json 但无济于事。我错过了什么?

dayue0618 回答:“错误:无法从打字稿中的相对路径找到模块”

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/561.html

大家都在问