ookamiiixd / baileys-api

Simple WhatsApp REST API with multiple device support
MIT License
431 stars 327 forks source link

Must use import to load ES Module #4

Closed reactmore closed 2 years ago

reactmore commented 2 years ago

I found this error when I was looking for a solution I had to add a type module in the json package but I see it already exists

Node js Version : 14.18.1

Log From hosting :

internal/modules/cjs/loader.js:948
    throw new ERR_REQUIRE_ESM(filename);
    ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/u8476361/public_html/api/app.js
    at new NodeError (internal/errors.js:322:7)
    at Module.load (internal/modules/cjs/loader.js:948:11)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at startApplication (/usr/local/lsws/fcgi-bin/lsnode.js:48:15)
    at Object.<anonymous> (/usr/local/lsws/fcgi-bin/lsnode.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32) {
  code: 'ERR_REQUIRE_ESM'
}
ookamiiixd commented 2 years ago

Is this a cPanel hosting? I suppose this is a problem related with how it works as previosly my clients had the same issue.

Transpiling the code to CommonJS would solve the issue.

reactmore commented 2 years ago

Is this a cPanel hosting? I suppose this is a problem related with how it works as previosly my clients had the same issue.

Transpiling the code to CommonJS would solve the issue.

Yes cpanel hosting, i try change to commonJs but stuck in whatsapp.js

Line const __dirname = path.dirname(fileURLToPath(import.meta.url))

do you have any suggestions?

ookamiiixd commented 2 years ago

You can just remove it since __dirname should be available by default when using CommonJS.

reactmore commented 2 years ago

thanks its running

image

ookamiiixd commented 2 years ago

You're welcome, closing this.

reactmore commented 2 years ago

Hi, sorry I opened it again, the MD version is quite stable but I was confused when I changed it to common js

makeWASocket.default(config)

Log : (node:18936) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'default' of undefined

reactmore commented 2 years ago

solved sorry need require like this const makeWASocket = require('@adiwajshing/baileys').default;

zeeid commented 2 years ago

makeWASocket

bang ini cara biar bisa di hosting cpanel gimana ya, aku udah ubah ubah tetep aja error lognya begitu sama