4

I saw a lot of related questions, but none of the answers seem to work for my scenario. I have 2 files in a node.js app (node version v13.5.0), start.ts and app.ts at root, and they look like this:

start.ts:

import app from './app';

console.log(app);

app.ts:

const app = {};
export default app;

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": ".",
    "esModuleInterop": true,
    "lib": ["esnext", "es2015"],
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./build",
    "pretty": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "target": "es6"
  },
  "include": ["start.ts", "app.ts", "src/**/*.ts"],
  "exclude": ["node_modules", "build"]
}

But when I tried to use ts-node to run start.ts, I'm getting this error:

Error: Cannot find module /<project_directory>/app imported from /<project_directory>/start.ts
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:96:13)
    at Loader.resolve (internal/modules/esm/loader.js:72:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:156:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
    at link (internal/modules/esm/module_job.js:41:36)

I've also tried adding the file extension to the import, but then it gets me the Unknown file extension ".ts" error. And it doesn't look like I will need typings since app is a simple object, but how come it is having trouble finding the module? Thanks.

1 Answer 1

4

Looks like I found it, it was because in package.json I added "type": "module"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.