TypeError: path.join is not a function

Andreas picture Andreas · Mar 4, 2019 · Viewed 9.3k times · Source

The code that I have posted is working if I take AWAY the below function that I have wrapped the code with:

But if running the below code. I do get this error where path.join is not a function. As seen I want to all the time run the code inside watcher.on when a file has been added. The code does react when I add a file but again I always receive below error. Do I set up the watcher.on code wrong or what causes the error?

File C:\myproject\instances\b53pd4574z8pe9x793go\New Text Document.txt has been added (node:14360) UnhandledPromiseRejectionWarning: TypeError: path.join is not a function

Complete code:

'use strict';
const ccxt = require('ccxt');
const fs = require('fs');
const path = require('path');
var chokidar = require('chokidar');
var watcher = chokidar.watch('C:/myproject/instances/b53pd4574z8pe9x793go', { ignored: /^\./, persistent: true });


var i;
const exchangename = "binance";
const exchange = new ccxt.binance({
    'enableRateLimit': false
});



watcher.on('add', function (path) {
(async () => {
    console.log('File', path, 'has been added')
    const start = Date.now()


    var orderbookPromises = []
    var symbols = ['ETH/BTC']
    for (i = 0; i < symbols.length; i++) {

        const symbol = symbols[i]
        
        try {
                let tickerProcessing = new Promise(async (resolve) => {
                    const orderbook = await exchange.fetchOrderBook(symbol, 5)

                    const exchangename2 = exchangename + '#' + symbol.replace("/", "")
                    const dumpFile = path.join(__dirname, 'orderbooks', `${exchangename2}Orderbook.txt`)
                await fs.promises.writeFile(dumpFile, JSON.stringify(orderbook))
                resolve()
            })
            orderbookPromises.push(tickerProcessing)

        } catch (e) {
            console.error(e)
        }
    }

    // wait for all of them to execute or fail
    await Promise.all(orderbookPromises)


    const end = Date.now()
    console.log(`Done in ${(end - start) / 1000} seconds`)
    })()
});

Answer

js_Yuriy Ermolaev picture js_Yuriy Ermolaev · Jan 5, 2022

I think, it because you send 'path' as a parameter and at the same time, trying to call join() from 'path'. Rename input param in 'function (path)', i think it will help.