" TS2307:找不到模块"通过tsc的本地文件,但VSCode定位文件正常

时间:2017-05-11 14:11:13

标签: typescript ecmascript-6 visual-studio-code tsc

打字稿版本:2.3.2
VSCode版本:1.12.1

我试图将我的项目转换为所有绝对导入,以便更轻松地重新组织文件结构,但是我无法通过绝对路径tsc找到我的本地模块(相对于项目根目录)

当我通过tsc scripts/build.ts编译我的打字稿时,tsc无法找到我的文件:

[adambowles@localhost project]$ tsc scripts/build.ts
scripts/build.ts(1,26): error TS2307: Cannot find module 'scripts/utils/download'.

将导入更改为相对作品,我从tsc收到错误。

VSCode locates the module for its code suggestions without issue(这里你可以看到它建议download函数的正确方法签名)

项目结构:

projectRoot
├── scripts
│   ├── utils
│   │   └── download.ts
│   └── build.ts
└── tsconfig.json

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "build",
    "module": "commonjs",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDirs": [
      "./scripts",
      "./src"
    ],
    "baseUrl": ".",
    "paths" : {
      "scripts/*": ["./scripts/*"],
      "src/*": ["./src/*"]
    }
  },
  "include": [
    "src/**/*",
    "scripts/**/*"
  ],
  "exclude": [
    "node_modules",
    "build",
    "acceptance-tests",
    "webpack",
    "jest",
    "yarn.lock",
    "temp",
    "public"
  ],
  "types": [
    "typePatches"
  ]
}

脚本/ build.ts:

import { download } from 'scripts/utils/download';

脚本/ utils的/ download.ts:

import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
import { sync as mkdirpSync } from 'mkdirp';
import * as path from 'path';

/**
 * Download a file over https
 */
export const download = (source: string, destination: string): Promise<string> => {
  return new Promise((resolve, reject): void => {
    // Create directory if it doesn't exist
    const destinationDirectory = path.dirname(destination);
    if (!(fs.existsSync(destinationDirectory))) {
      mkdirpSync(destinationDirectory, (error: Error): void => {
        if (error) {
          reject(error);
        }
      });
    }

    const file = fs.createWriteStream(destination);

    const handleResponse = (response: http.IncomingMessage): void => {
      const statusCode: number = Number(response.statusCode);
      const statusMessage: string = String(response.statusMessage);

      if (statusCode < 400) {
        response.pipe(file);

        file.on('finish', (): void => {
          file.close();

          file.on('close', (): void => {
            resolve(destination);
          });
        });
      } else {
        reject(new Error(`${statusCode} ${statusMessage}`));
      }
    };

    const handleError = (error: Error): void => {
      fs.unlink(destination, (unlinkError: Error): void => {
        reject(error);
      });
    };

    if (/^https/.test(source)) {
      https.get(source, handleResponse).on('error', handleError);
    } else {
      http.get(source, handleResponse).on('error', handleError);
    }
  });
};

0 个答案:

没有答案