binance-api-node icon indicating copy to clipboard operation
binance-api-node copied to clipboard

Binance is not a function

Open guilledll opened this issue 3 years ago • 4 comments

Hi, i'm using the package in my Nuxt 3, Vue 3, Vite, Typescript Project, i just installed it and got a error when importing Binance.

With this config:

image

I got the next error:

image

When using const Binance = require('binance-api-node').default

image

apiKey, apiSecret & apiRoute are values form a .env file, url is https://api.binance.com or https://testnet.binance.vision

System Info "binance-api-node": "^0.11.29", Node js v16.13.2 Ubuntu 20.04

Any suggestion?

guilledll avatar Jan 16 '22 15:01 guilledll

Maybe add binance-api-node in your transpile config like that:

import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
  build: {
    transpile: ['binance-api-node']
  }
})

yovanoc avatar Jan 19 '22 11:01 yovanoc

Hello,

I'm having the same issue. I'm writing a simple type-script app using Binance API. Very basic setup. Using 'tsc' compiler. But I don't understand a hint above (I am a newbe to js/ts/nodejs). Can you elaborate more on that?

vckbarth avatar Feb 24 '22 22:02 vckbarth

Hello I code worked my project

import Binance from "binance-api-node";
const options ={
  apiKey: "<apiKey>",
  apiSecret: "<apiSecret>"
};
const client = Binance.default(options);

suhakesikbas avatar May 29 '22 14:05 suhakesikbas

Can confirm the same issue.

No transpiler, node v18.3.0, running a standard package.json. I had to run it as @suhakesikbas mentioned, using :

import Binance from 'binance-api-node'

const client = Binance.default(options)

Notice the .default here, meaning the documentation is currently erroneous.

ubarbaxor avatar Aug 01 '22 02:08 ubarbaxor

I have the same issue.

I tried with :

import Binance from 'binance-api-node'

const client = Binance.default(options)

I have this error : error TS2339: Property 'default' does not exist on type. Any idea ?

corentindenoeud avatar Oct 08 '22 16:10 corentindenoeud

I have the same issue.

I tried with :

import Binance from 'binance-api-node'

const client = Binance.default(options)

I have this error : error TS2339: Property 'default' does not exist on type. Any idea ?

Hey, the documentation has been updated to better reflect the current API. To initialize your client, you don't need to specify the .default part. Just call Binance(options). See the "getting started" section of the docs

@balthazar btw I think this issue is g2c now that the docs are on point ;)

ubarbaxor avatar Oct 12 '22 21:10 ubarbaxor

Cool thank you @ubarbaxor

balthazar avatar Oct 13 '22 02:10 balthazar

Sorry, but I still have the same issue :

import Binance from 'binance-api-node'

const client2 = Binance({
  apiKey: apiKey,
  apiSecret: apiSecret,
})

the error is :

        const client2 = Binance({
                        ^
TypeError: Binance is not a function

corentindenoeud avatar Oct 13 '22 11:10 corentindenoeud

Hey,

Seems this keeps popping up and again. This is behaving deferently depending on if your project is transpiled or not, if you're using ESM (import) or CommonJS (require).

Somehow the transpiling tends to break ESM exporting as it seems it's doing some shennanigans to have CommonJS compatibility. In the future, for those with this issue : just use Binance.default() rathen than use Binance() as a constructor, and vice-versa (if Binance.default() doesn't work, use Binance()).

TL;DR : use one or the other, whichever works, depending on your build pipeline.

ubarbaxor avatar Oct 18 '22 08:10 ubarbaxor