简体   繁体   中英

How can I use Typed package in React?

I want to use the idm_db package with Typescript and React. In its document, it only explains nodejs and also it does not support types. Luckily, there is @types/ibm_db and I think it supports types, but I cannot figure out how to use this one.

In nodejs,

var ibmdb = require('ibm_db');

ibmdb.open("DATABASE=<dbname>;HOSTNAME=<myhost>;UID=db2user;PWD=password;PORT=<dbport>;PROTOCOL=TCPIP", function (err,conn) {
  if (err) return console.log(err);

  conn.query('select 1 from sysibm.sysdummy1', function (err, data) {
    if (err) console.log(err);
    else console.log(data);

    conn.close(function () {
      console.log('done');
    });
  });
});

node ibm_db

Following this one, I just changed first line, from var ibmdb = require('ibm_db'); to import * asibmdb from 'ibm_db' . But, it keeps throwing an error related to webpack.(I don't use webpack at this point). It compiles, but page is showing the error messages.

So, my question is, how can I use this package in React and Typescript way? Thanks in advance!

EDIT It throws lots of errors and I am posting errors that I think it may cause.

  28 |                      path.resolve(__dirname, '../installer/clidriver/lib');
  29 | }
  30 | 
> 31 | var odbc = require("bindings")("odbc_bindings")
  32 |   , SimpleQueue = require("./simple-queue")
  33 |   , util = require("util")
  34 |   , Readable = require('stream').Readable
199 |   // Avoids an infinite loop in rare cases, like the REPL
  200 |   dir = process.cwd();
  201 | }
> 202 | if (
      | ^  203 |   exists(join(dir, 'package.json')) ||
  204 |   exists(join(dir, 'node_modules'))
  205 | ) {
  147 |         );
  148 |         hotCurrentParents = [];
  149 |     }
> 150 |     return __webpack_require__(request);
      | ^  151 | };
  152 | var ObjectFactory = function ObjectFactory(name) {
  153 |     return {

For something like this, I might use

import * as ibmdb from 'ibm_db';

Does that help? Some reference: https://stackoverflow.com/a/43172907/8678978

I have the same issue when I use the moment package in React with typescript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM