Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS import file with the same name as a folder

I have the following file structure: ([] are folders)

[MySQL]
   ConnectionPool.ts
   Connection.ts
MySQL.ts

In my code, I am using typescript to develop the app, which will be built to javascript for the production version. The development version is tested directly from the uncompiled typescript files, using babel with the following configurations:

{
    "presets": [
        ["@babel/preset-env", {
            "targets": {
                "node": "current"
            }
        }], 
        "@babel/preset-typescript"
    ],
    "plugins": [
        "@babel/plugin-transform-runtime", 
        [
            "module-resolver",
            {
                "alias": {
                    ... list of some aliases
                }
            }
        ]
    ]
}

My problem is the following, I do most of my imports like this:

import ConnectionPool from 'MySQL/ConnectionPool`

which works for me, as when I run my dev code, the compiler correctly identifies the file extension to .ts, and it also correctly identifies the built versions file extension to .js.

But if I want to import my MySQL.ts file, I can't do it this way, as I will get the following error: Error: Cannot find module './'. If I specify in my import statement the file extension, everything works correctly, but then, when I build my code, there will be no more .ts files in there, so I will get errors there.

Strangely, on the frontend, where I use webpack, I get no complaints about importing files without extension whose name is the same as a folders name in the same directory. What solutions do I have for this issue, which does not revolve around renaming my files or folders in the structure?

like image 764
Adam Baranyai Avatar asked Oct 28 '25 22:10

Adam Baranyai


1 Answers

Rename your file MySQL.ts to index.ts and move it into the folder MySQL

like image 134
Romain V... Avatar answered Oct 31 '25 13:10

Romain V...