I am facing an issue with my project structure where I have a node_modules
folder at the root level and another one within a subfolder named functions
. The directory layout looks like this,
├── functions
│ ├── index.js
│ ├── index.js.map
│ ├── index.ts
│ ├── package.json
│ ├── node_modules
│ └── tsconfig.json
├── package.json
└── node_modules
The problem arises when I try to compile the TypeScript files inside the functions
folder because the TypeScript compiler searches in the root node_modules instead of the local one, resulting in errors like:
tsc --project functions
functions/node_modules/@types/node/index.d.ts(60,13): error TS2451: Cannot redeclare block-scoped variable 'global'.
functions/node_modules/@types/node/index.d.ts(84,13): error TS2300: Duplicate identifier 'require'.
node_modules/@types/react-native/index.d.ts(8365,11): error TS2451: Cannot redeclare block-scoped variable 'global'.
node_modules/@types/react-native/index.d.ts(8366,14): error TS2300: Duplicate identifier 'require'.
Is there a way to instruct TypeScript to ignore the upper-level node_modules
or any other solution to address this issue?
This is how my tsconfig.json file looks like:
{
"compilerOptions": {
"target": "es2015",
"module": "CommonJS",
"outDir": ".",
"rootDir": ".",
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"preserveConstEnums": true,
"allowJs": true,
"sourceMap": true
},
"filesGlob": [
"./**/*.ts"
],
"exclude": [
"node_modules"
],
"compileOnSave": false
}