无法导入本地模块。

huangapple go评论83阅读模式
英文:

Can not import local module

问题

我正在尝试在一个Node.js应用程序中导入一个本地模块,但似乎无法成功。

modules/date.js:

const getDate = () => {...};
const getDay = () => {...};
export {getDate, getDay};

app.ts:

import {getDay, getDate} from "/modules/date";
//import {getDay, getDate} from "/modules/date.js"; 也不起作用

错误信息:
找不到模块'/modules/date'或其对应的类型声明.ts(2307)

配置文件

package.json:

{
  "dependencies": {
    "ejs": "^3.1.8",
    "express": "^4.18.2",
    "mongoose": "^6.8.3",
    "nodemon": "^2.0.20",
    "typescript": "^4.9.4"
  },
  "name": "todolist",
  "version": "1.0.0",
  "description": "A simple todo list",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Infinite Learner",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.15"
  },
  "type": "module"
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2016",
    "lib": ["es6"],
    "moduleResolution": "node",
    "rootDir": "./",
    "outDir": "build",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

你知道我缺少什么吗?

谢谢。

英文:

I am trying to import a local module on a node.js application and I do not seem to be able to.

modules/date.js :

const getDate = () => {...};
const getDay = () => {...};
export {getDate, getDay};

app.ts :

import {getDay, getDate} from "/modules/date";
//import {getDay, getDate} from "/modules/date.js"; doesn't work either

The error :
Cannot find module '/modules/date' or its corresponding type declarations.ts(2307)

Config files

package.json :

 {
  "dependencies": {
    "ejs": "^3.1.8",
    "express": "^4.18.2",
    "mongoose": "^6.8.3",
    "nodemon": "^2.0.20",
    "typescript": "^4.9.4"
  },
  "name": "todolist",
  "version": "1.0.0",
  "description": "A simple todo list",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Infinite Learner",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.15"
  },
  "type": "module"
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": ["es6"],                                      /* Specify a set of bundled library declaration files that describe the target runtime environment. */
   
    /* Modules */
    "moduleResolution": "node",
    "rootDir": "./",                                     /* Specify the root folder within your source files. */
   
    /* Emit */
    "outDir": "build",                                   /* Specify an output folder for all emitted files. */
   
    /* Interop Constraints */
    "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. */

    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */

    /* Completeness */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}

Do you know what I am missing ?

Thank you.

答案1

得分: 1

因为你正在尝试将一个Js文件导入到一个Ts文件中,要么你将其更改为date.ts,要么在tsconfig.json中添加一些设置,也许可以查看这个链接:https://stackoverflow.com/questions/54320967/nodejs-how-to-import-js-file-into-typescript

英文:

Well because you re trying to import a Js file into a Ts file, either you change into date.ts or add some setting into tsconfig.json, maybe check this threat : https://stackoverflow.com/questions/54320967/nodejs-how-to-import-js-file-into-typescript

huangapple
  • 本文由 发表于 2023年1月7日 06:26:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75036632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定