Skip to content
Advertisement

Why isn’t my import being found by the esm loader?

I have a small express server and in my bin/www.ts I import my app.ts file like this:

JavaScript

When I build my project and turn it into JavaScript using: tsc --project ./ and then run it with nodemon ./build/bin/www I get an error in my console saying:

JavaScript

The file exists in the location I specified, I’ve checked and I’ve added the "type":"module" to my package.json file as well. I’ve removed every require from the app.ts file as well and still nothing yet. I’m not sure what to do at this point. Here is my package.json file (condensed to get to the point):

JavaScript

My ts.config:

JavaScript

If it helps, here is my app.ts, which also has no errors (condensed for clarity):

JavaScript

How do I get my project to see my file so that I can start up my server? thanks in advance and let me know if you need any more details.

Advertisement

Answer

@ASDFGerte pointed out that in esm you have to include the file’s extension for relative imports. So I was able to fix and run my code by changing: import app from '../app'; to import app from '../app.js';

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement