EDIT:
Added export {};
to the first line of files that were throwing the error, resolved all issues.
I keep getting this error when I try to import (require) code inside other files/folders. For example:
I have a file which exports a function isArray()
. The file looks something like this:
const isArray = () => {...};
module.exports = isArray;
Inside some other file I import it by doing const isArray = require('./isArray.ts');
I then get the error Cannot redeclare block-scoped variable 'isArray' on the require statement and where I defined the function.
I also get that error when importing packages like dotenv and express.
How can I avoid this error?
EDIT:
package.json
{
...
"main": "./src/api/server.ts",
"scripts": {
"dev": "npx nodemon ./src/api/server.ts",
...
},
...
"dependencies": {
"axios": "^0.27.2",
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"express": "^4.18.1",
"firebase": "^9.7.0",
"firebase-admin": "^10.2.0",
"moment": "^2.29.3",
"morgan": "^1.10.0",
"plaid": "^10.3.0",
"uniqid": "^5.4.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.4"
},
"devDependencies": {
"dotenv": "^16.0.0",
"nodemon": "^2.0.16"
}
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"rootDir": "./src",
"outDir": "./dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules",
"src-broken/types"
]
}
},
"include": [
"./src"
]
}