I have quite simple express server. I am using a self hosted package, so it works the same way as any npm package what it comes to installing and using it. Or at least that’s what I think and how it should work. My actual server file is importing from this package and also imports some route files. My structure is currently roughly like this:
// app.ts
import express from 'express';
import { createWinstonLogger } from 'myown-pkg';
import route1 from './routes/route1';
import route2 from './routes/route2';
const logger = createWinstonLogger(some, params);
const app = express();
app.use('/route1', route1);
app.use('/route2', route2);
export { app, logger }
// routeX.ts
import express from 'express';
import { locallyDefinedAuthMW } from '../mw/auth';
const router = express.Router();
router.get('/someRoute', locallyDefinedAuthMW, ...)
export default router;
// auth.ts
import { logger } from '../app';
export default async (req, res, next) => {
// some implementation using logger defined in the app file
}
Now this works. However, I want to move the auth middleware (and other stuff) to this separate package, because other services are using the same logic as well. Now that the auth middleware is in the packages I run into some weird import order issue. If I import the authMW in the app file, it is imported correctly. However, if I import it either in the route file or in the auth file, the import is undefined. Any idea why’s that? I have been able to narrow this down so that I am quite sure that this is some kind of module import order issue, but I can’t figure out how to fix it.
// app.ts
import express from 'express';
import { createWinstonLogger, authMW } from 'myown-pkg';
import route1 from './routes/route1';
import route2 from './routes/route2';
console.log(authMW) // <-- shows the function
const logger = createWinstonLogger(some, params);
const app = express();
app.use('/route1', route1);
app.use('/route2', route2);
export { app, logger }
// routeX.ts
import express from 'express';
import { authMW } from 'myown-pkg';
console.log(authMW) // <-- undefined
const router = express.Router();
router.get('/someRoute', locallyDefinedAuthMW, ...)
export default router;
// auth.ts
import { authMW } from 'myown-pkg';
import { logger } from '../app';
console.log(authMW) // <-- undefined
export default async (req, res, next) => {
// some implementation using logger defined in the app file
}
Has this something to do with the tsconfig
, which currently is following:
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "ES2020",
"noImplicitAny": false,
"strict": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"skipLibCheck": true
}
}