Typescript可以编译,但由于无法解析模块而无法运行

时间:2019-11-02 21:14:33

标签: typescript tsconfig

我可以使用tsc来编译代码,但是当我尝试运行代码时,出现了错误:

$ node ./outdir/services/preDeployPrepare/main.js
internal/modules/cjs/loader.js:670
    throw err;
    ^

Error: Cannot find module 'enums'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:668:15)
    at Function.Module._load (internal/modules/cjs/loader.js:591:27)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (C:\cos-tools\singularityGroupHQ\outdir\services\preDeployPrepare\main.js:3:17)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
error Command failed with exit code 1.

我的ts配置:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "@enums": ["./src/enums"],
            "@types": ["./src/typings"],
            "@tools": ["./src/tools"],
            "@features": ["./src/features"],
            "@errors": ["./src/errors"],
            "@middleware": ["./src/middleware"],
            "@mocks": ["./src/mocks"],
            "@mongo": ["./src/mongo"],
            "@presets": ["./src/presets"],
            "@routes": ["./src/routes"],
            "@services": ["./src/services"],
            "@api": ["./src/api"],
            "@typeguards": ["./src/typeguards"]
        },
        /* Basic Options */
        "outDir": "outdir",
        "rootDir": ".",
        "target": "ES2018",
        "module": "commonjs",
        "lib": ["esnext"],
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "composite": true,
        "removeComments": true,
        "noEmit": false,
        "pretty": true,
        "jsx": "react",
        "incremental": true,

        /* Strict Type-Checking Options */
        "strict": true,
        "noImplicitAny": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "strictPropertyInitialization": true,
        "strictBindCallApply": true,

        /* Additional Checks */
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,

        /* Module Resolution Options */
        "moduleResolution": "node",
        "typeRoots": ["node_modules/@types"],
        "esModuleInterop": true,
        "resolveJsonModule": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    },
    "exclude": ["outdir"]
}

我尝试更改路径,基本URL和根URL,但似乎无济于事。 我想这是映射的工作方式,即使它可以编译,但编译后的代码没有枚举?

由于枚举是js中的特殊对象,我是否需要在ts config选项之一中包含枚举?

0 个答案:

没有答案