简体   繁体   中英

Typescript Cannot find module imported

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.

貌似找到了,是因为在 package.json 里加了"type": "module"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM