I am trying to use tsc in the cli on files in node_modules. Are files in node_modules excluded by default? What should I do?
Yes, files in the node_modules directory are excluded by default when using the tsc compiler in the CLI. This is because the node_modules folder typically contains third-party libraries written in JavaScript, which don’t require type checking by the TypeScript compiler.
Here are your options to include files from node_modules:
1. Not Recommended:
- Directly including
node_modules: While technically possible to include the entirenode_modulesdirectory in your compilation, it’s strongly discouraged. This can significantly slow down compilation due to the large number of files and potential conflicts with your own project’s code.
2. Recommended approach:
- Use type definitions: Most popular libraries in
node_moduleshave corresponding type definition packages available on npm (https://www.npmjs.com/). These packages usually start with the@typesprefix, followed by the library name (e.g.,@types/express). Install the relevant type definition package usingnpm install @types/<library-name>. This provides type information for the library without including the actual source code.
3. Specific file inclusion (for rare cases):
- In rare cases, you might need to include specific files from
node_modules. While not ideal, you can achieve this by using the--includeflag withtsc. However, this should be used with caution, as it can potentially introduce conflicts and dependencies on the external library’s implementation details.
Remember, the best practice is to leverage type definitions whenever possible. They provide type information without unnecessary compilation overhead and maintain a clear separation between your project code and external libraries.
For the command I am using there is little reason to have excluded directories, how can I remove all excluded directories from the settings in tsc cli? No tsconfig.json please.
While you can’t directly remove excluded directories from the CLI, you can achieve a similar effect using the --noResolve flag.
Here’s how to compile a file without any exclusion logic:
tsc --noResolve your_file.ts
Key points:
--noResolvetells the compiler to not perform module resolution, meaning it won’t consider any exclusion patterns or include directives.- It’ll only compile the specified file(s) directly.
- It’s crucial to note that
--noResolvehas broader implications:- No module resolution: References to other modules won’t be resolved, potentially leading to errors if your code depends on external modules.
- No type checking from included files: Type information from other files won’t be considered, potentially affecting type checking accuracy.
To include specific files:
- If you need to include files from excluded directories like
node_modules, use the--includeflag:
tsc --noResolve --include node_modules/some-library/file.ts your_file.ts
Best practices:
- Use
--noResolvejudiciously, as it can bypass intended project structure and type checking. - For most scenarios, creating a
tsconfig.jsonfile with appropriateincludeandexcludesections is recommended for better control and maintainability.
