Missing ./lib files in node_modules while using allowJs = true with Typescript

时间:2018-03-23 00:42:25

标签: node.js typescript

I am new to NodeJS. I started building an express application where one of my .js files was using requestRetry module. It was working great. Later I decided to slowly introduce Typescript into my project. I converted the basic sections required by express framework to Typescript, but some of my files (the one i mentioned) is still .js.

Here is my tsconfig.js

{
    "compilerOptions": {
        "module": "commonjs",
        "allowJs": true,
        "esModuleInterop": true,
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true,
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
            "*": [
                "node_modules/**/*",
                "src/types/*"
            ]
        }
    },
    "include": [
        "./src/**/*"
    ]
}

The project compiles fine but when I run the application it crashes with

module.js:559
    throw err;
    ^

Error: Cannot find module './lib/cookies'
    at Function.Module._resolveFilename (module.js:557:15)
    at Function.Module._load (module.js:484:25)
    at Module.require (module.js:606:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\@MyProjects\OneDriveAriaStats\dist\node_modules\request\index.js:16:15)
    at Module._compile (module.js:662:30)
    at Object.Module._extensions..js (module.js:673:10)
    at Module.load (module.js:575:32)
    at tryModuleLoad (module.js:515:12)
    at Function.Module._load (module.js:507:3)

With my current setup, tsc outputs all of the compiled files into the dist folder and also creates a node_modules underneath it since allowjs = true in my tsconfig.js. I dug in further and figured out that lib folder underneath the dist/node_module/request is missing. Its present in the node_module/request.

What am I doing wrong here? Why is tsc not copying over all the required files for request module?

1 个答案:

答案 0 :(得分:0)

我不认为librequestretrydist的缺席是问题所在。我看过package.json of requestretry。因为其声明的main字段为index.js,如果您使用tsconfig.json中的node resolution strategy,TypeScript会将requestretry模块解析为{{1}因此根本不涉及node_modules/requestretry/index.js。实际上,that package内没有node_modules/requesretry/lib/目录。