【问题标题】:Deno -> How do I run the codes to run without errors?Deno -> 如何运行代码以无错误地运行?
【发布时间】:2020-12-27 02:10:52
【问题描述】:

我想从本地导入

测试环境Deno v1.6.0

我尝试过 Deno lang 的本地导入

本地目录

.
└── src
    └── sample
        ├── hello_world.ts
        ├── httpRequest.ts
        ├── localExport
        │       └── arithmetic.ts
        ├── localImport.ts

'./localExport/arithmetic.ts'要导入的文件

function add(outbound: number, inbound: number): number {
  return outbound + inbound
}

function multiply(sum: number, tax: number): number {
  return sum * tax
}

'./localImport.ts' 要运行的文件

import { add, multiply } from "./localImport/arithmetic.ts";

function totalCost(outbound: number, inbound: number, tax: number): number {
  return multiply(add(outbound, inbound), tax);
}

console.log(totalCost(19, 31, 1.2));
console.log(totalCost(45, 27, 1.15));

运行以上代码

❯ deno run src/sample/localImportExport.ts

我得到了错误:

❯ deno run src/sample/localImportExport.ts 
error: Uncaught SyntaxError: The requested module './localImport/arithmetic.ts' does not provide an export named 'add'
import { add, multiply } from "./localImport/arithmetic.ts";
         ~~~
    at <anonymous> (file:///Users/ko-kamenashi/Desktop/Samples/Deno/deno-sample/src/sample/localImportExport.ts:1:10)

我该怎么办?

【问题讨论】:

  • 您能否详细说明您提供的错误消息究竟有哪些不清楚的地方?您包含的文件不会导出任何名为 add 的文件,因此会引发错误。这个问题不看arithmetic.ts的出处就无法回答。
  • @esqew 很抱歉忘记了导入源文件的代码。我添加了目标代码。
  • 你能否引用你隐含假设的来源,即你可以从外部文件中导入任意函数,这些函数没有正确exported?这正是错误消息所指向的问题(您还没有提供任何颜色来说明您对错误消息本身不了解的具体内容)。
  • @esqew 感谢您的建议!我通过将 export 关键字添加到目标函数来解决了这个问题。 ``` export function add(outbound: number, inbound: number): number { return outbound + inbound } export function multiply(sum: number, tax: number): number { return sum * tax } ```

标签: javascript typescript deno


【解决方案1】:

错误The requested module './localImport/arithmetic.ts' does not provide an export named 'add'告诉你应该使用export

只需将以下行添加到文件的末尾 `export {add, multiply}

'./localExport/arithmetic.ts'要导入的文件

function add(outbound: number, inbound: number): number {
  return outbound + inbound
}

function multiply(sum: number, tax: number): number {
  return sum * tax
}

export {add, multiply}

【讨论】:

  • 感谢您教授解决方案。这个问题解决了。
猜你喜欢
  • 2018-09-19
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多