7

I'm trying to import Tesseract into Angular2 (TypeScript). I can see it saved into the node_modules folder but when using

import { Tesseract } from '@types/tesseract.js';

it says:

[ts] Module '"c:/Users/black/Projects/projectCLI/tess/node_modules/@types/tesseract.js/index"' has no exported member 'Tesseract'.

In the index.d.ts file there is a namespace called Tesseract.

Is there some other way to import this or are we looking at this the wrong way?

I used npm install --save-dev @types/tesseract.js to install typescript Tesseract.

If there are any demo tutorials using tesseract can you please link them here?

thanks, in advance, for your help.

The BrownBatman
  • 2,593
  • 1
  • 13
  • 29
  • `@types` just has type declarations. Do you have the actual javascript module? you have to import from there – Suraj Rao Mar 04 '17 at 09:02
  • Do you use Angular 2 with Angular CLI, Webpack or SystemJS ? – Monicka Mar 04 '17 at 09:03
  • Suraj: Could you please give an example of the import syntax? It is already visible in node_modules inside @types. Thanks _the first syntax in question is what we are trying at the moment_ – The BrownBatman Mar 04 '17 at 09:04
  • @SimonaMi We use Angular 2 with AngularCLI, Webpack. – The BrownBatman Mar 04 '17 at 09:05
  • try `import * as T from Tesseract` – Suraj Rao Mar 04 '17 at 09:12
  • @Suraj this is the error we got `[ts] Cannot find module 'Tesseract'` we also tried `import * as T from '@types/tesseract.js';` as @types... is our directory. – The BrownBatman Mar 04 '17 at 09:19
  • @suraj Just tried that and we got the following error: `gyp ERR! configure error gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable. gyp ERR! stack at Object.failNoPython (C:\Users\black\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\configure.js:454:19)` (plus more) when i ran the above command in a new project it worked perfectly. Could it be a conflict with `npm install --save-dev @types/tesseract.js` ?? Thanks – The BrownBatman Mar 04 '17 at 09:25
  • that is a different error entirely http://stackoverflow.com/questions/21365714/nodejs-error-installing-with-npm – Suraj Rao Mar 04 '17 at 09:27
  • @Suraj Should i make a new AngularCLI project and then try your npm install command?? – The BrownBatman Mar 04 '17 at 09:27
  • no.. @types is *just* type declarations of the module..the actual module is the javascript one I put – Suraj Rao Mar 04 '17 at 09:28
  • typescript declarations are needed to import the actual javascript module in a TS project. you will need both in your project. Check the question I linked for the error – Suraj Rao Mar 04 '17 at 09:28
  • @Suraj: I started a new CLI and followed your instructions on the large error msg. First i did `npm install --global --production windows-build-tools` this and then i did `npm install --global node-gyp` this installed with 0 errors. Then i ran `npm install tesseract-js --save` which gave me a really big error so i ran `npm install --save-dev @types/tesseract.js` which gave no errors. – The BrownBatman Mar 04 '17 at 09:42
  • @Suraj: `ng serve` compiles with no errors. i added `import * as T from '@types/tesseract.js';` into my app.components.ts and there are no errors. How do i use Tesseract on an image? thanks – The BrownBatman Mar 04 '17 at 09:47
  • `npm install tesseract.js --save` https://github.com/naptha/tesseract.js – Suraj Rao Mar 04 '17 at 09:50
  • `npm install tesseract.js --save` worked and everything seems to be installed correctly - How do we use Tesseract methods, do we have to call new tesseract methods? im still using this `import * as T from '@types/tesseract.js';` import statement. Is it correct? cheers – The BrownBatman Mar 04 '17 at 09:58
  • import * as T from Tesseract. Let me know if it works.. will add answer – Suraj Rao Mar 04 '17 at 10:01
  • @Suraj: we used `import *as T from Tesseract` and got `[ts] String literal expected.` and then we used `import *as T from 'Tesseract'` and it gave and error `[ts] Cannot find module 'Tesseract'.` – The BrownBatman Mar 04 '17 at 10:09

1 Answers1

14

You need to install the actual javascript module:
npm install tesseract.js --save

Also install @types declarations:
npm install @types/tesseract.js --save-dev

Finally do the folowing to import:
import * as Tesseract from 'tesseract.js'

To use the library check here

The @types command saves this type declaration file.
This is a namespace and all the contents of the actual module are declared within.When you do import * as aliasname from tessreact.js, you can use all the functions within the namespace as aliasname.functionname. Example is the test file for the same type declaration file.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • Followed your instructions exactly, Prohram compiles with `ng serve` with zero errors an `import * as Tesseract from 'tesseract.js'` works well. Thanks you for this. How do i call a Tesseract commands? we are trying 'Tesseract. ' is there another approach to this? – The BrownBatman Mar 04 '17 at 10:24
  • Thank you very much for your time and help - it works perfectly and we are onto actual development. :) – The BrownBatman Mar 04 '17 at 10:49