0

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"
]
}
Brace Sproul
  • 593
  • 1
  • 5
  • 15
  • This problem totally depends on your setup, which is unknown. Consider adding details regarding it. Node doesn't support ts natively, so they work together in some way – Estus Flask Jul 15 '22 at 23:06
  • @EstusFlask I'm not sure what you mean by my setup, so I added my package.json and tsconfig.json files. If that's not what you're referring to could you please let me know so I can include that? Thanks – Brace Sproul Jul 16 '22 at 15:52
  • Here you seem to nothing that could make TS work properly. `node` command doesn't support TS. AFAIK nodemon doesn't specifically support TS. This means that Node executes scripts that contain invalid JS, because TS is a superset of JS. You need either ts-node, or compile TS files before executing them, or bundle them. – Estus Flask Jul 16 '22 at 16:25
  • @EstusFlask Okay that sounds like it makes sense, so I decided to build my ts and then run the js file, however when I run tsc --build I get the same error Cannot redeclare block scoped var 'dotenv' so I'm not sure how I can build my code so I can just run as node. – Brace Sproul Jul 16 '22 at 16:44
  • @EstusFlask same error when I run ts-node server.ts as well* – Brace Sproul Jul 16 '22 at 16:45
  • Avoid module.exports, it's type-unsafe, use ES modules. I'm quite sure in your case you get this error because a file isn't treated as a module and so types are exposed to global scope from TS point of view. See https://stackoverflow.com/questions/40900791/cannot-redeclare-block-scoped-variable-in-unrelated-files – Estus Flask Jul 16 '22 at 16:47
  • @EstusFlask added a ```export {};``` to the first line of the files that were throwing errors and that seemed to solve it. Thanks! – Brace Sproul Jul 16 '22 at 16:52
  • Glad this approach still works. Usually you won't need this because there are either export or import keywords in almost every module with ESM – Estus Flask Jul 16 '22 at 16:57

0 Answers0