NodeJS How to import JS file into TypeScript
Asked Answered
G

1

6

I'm new with TypeScript. I'm currently learning NodeJS Loopback 4 framework which use Typescript language. And my question is how to import some function, class which has been exported in JS file into my TS file. After search several way but it still doesn't work with me. Here's example:

  // /src/index.ts
    import {plus} from './lib/test';

    console.log(plus(1,2));

// /src/lib/test.js
    export function plus(x, y) {
      return x + y;
    }

I also try using definition typescript like this

// /src/lib/test.d.ts
export declare function plus(x: number, y: number): number;

But still got error when import this function in index.ts file

Error: Cannot find module './lib/test' at Function.Module._resolveFilename (module.js:543:15)

Golanka answered 23/1, 2019 at 6:9 Comment(6)
Possible duplicate of How to import js-modules into TypeScript file?Bounce
@MaazSyedAdeeb Sorry, I've followed that post and many post about this but it still got error when I try to import itGolanka
How about you when you import { plus } from './lib/test.js'? Notice the js extension at the endBounce
It can't. It will be consider to test.ts or test.d.ts and ignore js extensionGolanka
Please share your tsconfig. It might help find the problem.Bounce
I just found that I must put allowJs true in compilerOptions to make js file be compiled. By the way, thank you for your supportGolanka
H
9

It looks like the tsconfig.json doesn't have 'allowJs' enabled because it exports declarations.

Is there a reason you are not wanting this to be a typescript file? If you change test.js to test.ts, by making it a .ts should allow it to be recognised in your index file.

UPDATE

Full chat history to get to this point can be found here. Repository used to test found here

OK so easy solution as @maaz-syed-adeeb mentions will work:

import { path } from './lib/test.js'

The reason the extension is important is because the definition file takes priority over a javascript file in a typescript environment. That is why the module import was blowing up.

To avoid specifying the .js extension you can also setup your directory structure like this:

src
|- index.ts
|- lib
  |- test.js
  |- test.d.ts
  |- index.[js|ts]

in ./lib/index file export all from test:

//./src/lib/index.[js|ts]
export * from './test'

and then import all from lib:

// ./src/index.ts
import { path } from './lib'
// or
import { path } from './lib/test.js'

If you are using a blend of javascript and typescript (say you are moving over to typescript with an existing code base), you will need to update your tsconfig.json to include so you don't get the warnings in your IDE:

{
  "compilerOptions": {
    "allowJs": true,
    "declaration": false
  }
}

This is so your javascript files will be transpiled to your destination directory along with the typescript files.

Hyperbole answered 23/1, 2019 at 6:30 Comment(9)
thanks for your reply. My intention is to import JS function into TS, in real case that maybe in previous I have many function write with JS language and I want to reuse it in my TS Project (loopback 4).Golanka
I've recognize that when I put this { allowJs: true, declaration: false} in my tsconfig.json file then it works but maybe the test.d.ts file may not be required in this caseGolanka
I have actually created a copy of loopback 4 and without the compilerOptions: { allowJs: true, declaration: false} and it works with your example. So I think you can safely disregard that suggestionHyperbole
So just to be clear, I am running npmr run build then npm run startHyperbole
That's weird. You mean that you create a new app using lb4 app command than add a js and d.ts file like above, then import it from index.ts and it does not throw any error. For me it produce "Cannot find module './lib/test' " errorGolanka
Yeah exactly that, would you like me to put the repo on github so you can compare? If the error persists, it could be more the environment than the application setup.Hyperbole
Yes, please put it on github then I will get and try it. Thank you in advanceGolanka
Let us continue this discussion in chat.Hyperbole
Thank you very much for explainingGolanka

© 2022 - 2024 — McMap. All rights reserved.