I created a small application as part of an employment process for a potential employer. I tried running the application locally, including on a different machine (windows laptop), and it all works. However, they are saying that they are getting an error when trying to run the backend.
Using Typescript and ts-node.
It's a little bit confusing, as I cannot seem to replicate this error on any machine I own (I don't own a MAC however, so I haven't tried that). The only way I can replicate is if I try just running ts-node src/main.ts, so without esm. In the email they mentioned specifically that they ran npm start though.
What do you think is happening? It is extra unfortunate as I cannot replicate this, so I don't know whether or not I've solved it. Check end of post to see what I tried so far.
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for *redacted*/backend/src/main.ts
at new NodeError (node:internal/errors:405:5)
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
at defaultGetFormat (node:internal/modules/esm/get_format:142:36)
at defaultLoad (node:internal/modules/esm/load:86:20)
at nextLoad (node:internal/modules/esm/hooks:726:28)
at load (/*redacted*/backend/node_modules/ts-node/dist/child/child-loader.js:19:122)
at nextLoad (node:internal/modules/esm/hooks:726:28)
at Hooks.load (node:internal/modules/esm/hooks:370:26)
at MessagePort.handleMessage (node:internal/modules/esm/worker:168:24)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
package.json:
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "main.ts",
"type": "module",
"scripts": {
"start": "ts-node-esm src/main.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@fastify/cors": "8.5.0",
"fastify": "4.25.2",
"mongodb": "6.3.0",
"mongodb-memory-server": "8.12.0"
},
"devDependencies": {
"@types/node": "20.10.5",
"ts-node": "10.9.2",
"tsconfig-paths": "4.2.0",
"typescript": "5.3.3"
}
}
tsconfig.json:
{
"compilerOptions": {
"target": "ES5" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "NodeNext" /* Specify what module code is generated. */,
"moduleResolution": "NodeNext" /* Specify how TypeScript looks up a file from a given module specifier. */,
"allowImportingTsExtensions": true /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */,
"noEmit": true /* Disable emitting files from a compilation. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* When type checking, take into account 'null' and 'undefined'. */,
"strictFunctionTypes": true /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */,
"noImplicitThis": true /* Enable error reporting when 'this' is given the type 'any'. */,
"noUnusedLocals": true /* Enable error reporting when local variables aren't read. */,
"noUnusedParameters": true /* Raise an error when a function parameter isn't read. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
What I tried so far is adding/changing this to tsconfig.js:
{
"compilerOptions": {
"target": "ES5",
"outDir": "./dist",
"rootDir": "./src",
},
"ts-node": {
"esm": true
}
}
and also adding these alternative scripts to package.json:
"alt-start": "node --loader ts-node/esm src/main.ts",
"third-alt": "ts-node src/main.ts",
"fourth-alt": "npx ts-node-esm src/main.ts"
Do you think this will solve the issue? What is your suggestion on sending the solution back to them? If it doesn't work again... I would like to avoid that.
Thanks for the help!