无法导入本地模块。

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

Can not import local module

问题

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

modules/date.js:

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

app.ts:

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

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

配置文件

package.json:

  1. {
  2. "dependencies": {
  3. "ejs": "^3.1.8",
  4. "express": "^4.18.2",
  5. "mongoose": "^6.8.3",
  6. "nodemon": "^2.0.20",
  7. "typescript": "^4.9.4"
  8. },
  9. "name": "todolist",
  10. "version": "1.0.0",
  11. "description": "A simple todo list",
  12. "main": "app.js",
  13. "scripts": {
  14. "test": "echo \"Error: no test specified\" && exit 1"
  15. },
  16. "author": "Infinite Learner",
  17. "license": "ISC",
  18. "devDependencies": {
  19. "@types/express": "^4.17.15"
  20. },
  21. "type": "module"
  22. }

tsconfig.json:

  1. {
  2. "compilerOptions": {
  3. "target": "es2016",
  4. "lib": ["es6"],
  5. "moduleResolution": "node",
  6. "rootDir": "./",
  7. "outDir": "build",
  8. "esModuleInterop": true,
  9. "forceConsistentCasingInFileNames": true,
  10. "strict": true,
  11. "skipLibCheck": true
  12. }
  13. }

你知道我缺少什么吗?

谢谢。

英文:

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 :

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

app.ts :

  1. import {getDay, getDate} from "/modules/date";
  2. //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 :

  1. {
  2. "dependencies": {
  3. "ejs": "^3.1.8",
  4. "express": "^4.18.2",
  5. "mongoose": "^6.8.3",
  6. "nodemon": "^2.0.20",
  7. "typescript": "^4.9.4"
  8. },
  9. "name": "todolist",
  10. "version": "1.0.0",
  11. "description": "A simple todo list",
  12. "main": "app.js",
  13. "scripts": {
  14. "test": "echo \"Error: no test specified\" && exit 1"
  15. },
  16. "author": "Infinite Learner",
  17. "license": "ISC",
  18. "devDependencies": {
  19. "@types/express": "^4.17.15"
  20. },
  21. "type": "module"
  22. }

tsconfig.json:

  1. {
  2. "compilerOptions": {
  3. "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
  4. "lib": ["es6"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
  5. /* Modules */
  6. "moduleResolution": "node",
  7. "rootDir": "./", /* Specify the root folder within your source files. */
  8. /* Emit */
  9. "outDir": "build", /* Specify an output folder for all emitted files. */
  10. /* Interop Constraints */
  11. "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
  12. "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
  13. /* Type Checking */
  14. "strict": true, /* Enable all strict type-checking options. */
  15. /* Completeness */
  16. "skipLibCheck": true /* Skip type checking all .d.ts files. */
  17. }
  18. }

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:

确定