-1

I'd like to import from local

Testing environment Deno v1.6.0

I've tried local import by Deno lang

Local directory

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

'./localExport/arithmetic.ts' File to be imported

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

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

'./localImport.ts' File to run

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));

Run the above codes

❯ deno run src/sample/localImportExport.ts

I got the errors:

❯ 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)

What should I do?

KKK
  • 95
  • 7
  • 1
    Can you elaborate on what exactly is unclear about the error message you provided? The file you’re including doesn’t export anything called `add`, so it throws an error. This question is impossible to answer without seeing the source for `arithmetic.ts`. – esqew Dec 20 '20 at 22:25
  • @esqew I'm sorry to forget codes of import source file. I added target codes. – KKK Dec 20 '20 at 22:39
  • Can you cite the source on which you’re basing your implicit assumption that you can import arbitrary functions from external files that haven’t been properly `export`ed? That’s exactly the issue the error message is pointing to (you also still haven’t provided any color on what specifically you don’t understand about the error message itself). – esqew Dec 20 '20 at 22:40
  • @esqew Thanks for your advice!. I resolved this question By adding export keyword to the target function. ``` export function add(outbound: number, inbound: number): number { return outbound + inbound } export function multiply(sum: number, tax: number): number { return sum * tax } ``` – KKK Dec 20 '20 at 23:05

1 Answers1

1

The error The requested module './localImport/arithmetic.ts' does not provide an export named 'add' telling you that you should use export

Just add the following line to end of your file `export {add, multiply}

'./localExport/arithmetic.ts' File to be imported

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

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

export {add, multiply}
Morani
  • 446
  • 5
  • 15