Back

[SOLVED] Type module in node js clound function

  • 1
  • Functions
  • Storage
Binyamin
10 Apr, 2023, 17:38

So you'll have something like that

TypeScript
...
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "src/index.js",
...
TL;DR
The user is trying to use ES modules in a Node.js cloud function. They have changed all 'require' statements to 'import' statements and added "type: module" in their package.json file. However, they are still getting an error message stating that they must use import to load the ES module. Some users suggest downgrading the package, but it does not work. One user suggests changing the 'require' statements back and removing "type: module" from the package.json file. The user tries this solution but still gets the same error message. Another user suggests changing the first and second lines of the code to use 'import
Mosh Ontong
10 Apr, 2023, 17:38

ok I'll try that

Binyamin
10 Apr, 2023, 17:41

Also you can change the second line to

TypeScript
const sdk = require("node-appwrite");
const { parseBuffer } = require("music-metadata");

But both solutions will do just fine

Mosh Ontong
10 Apr, 2023, 17:42

But according to music-metadata package:

The JavaScript in runtime is compliant with ECMAScript 2017 (ES8). Requires Node.js® version 6 or higher.

Binyamin
10 Apr, 2023, 17:42

Yes I saw

Mosh Ontong
10 Apr, 2023, 17:42

is it still working?

Binyamin
10 Apr, 2023, 17:42

Is this fixed the problem?

Mosh Ontong
10 Apr, 2023, 17:43

unfortunately , it doesnt work this is the new error I got:

TypeScript
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/code-start/src/index.js
require() of ES modules is not supported.
require() of /usr/code-start/src/index.js from /usr/local/src/server.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/code-start/package.json.
Binyamin
10 Apr, 2023, 17:44

Yes, sorry You need also to change the first line Try change the first line to this and test and lmn

TypeScript
import sdk from "node-appwrite";
import { parseBuffer } from "music-metadata";
Mosh Ontong
10 Apr, 2023, 17:45

And I try this also this is the error I got:

TypeScript
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/code-start/src/index.js
require() of ES modules is not supported.
require() of /usr/code-start/src/index.js from /usr/local/src/server.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/code-start/package.json.
Mosh Ontong
10 Apr, 2023, 17:45

okok I'll try this one

Binyamin
10 Apr, 2023, 17:45

Good

Binyamin
10 Apr, 2023, 17:46

Yes, a bit confusing What I meant is if you change to this code

TypeScript
const sdk = require("node-appwrite");
const { parseBuffer } = require("music-metadata");

Then you need to remove the type:"module"

Mosh Ontong
10 Apr, 2023, 17:50

does not work

Mosh Ontong
10 Apr, 2023, 17:50

I think I am going to downgrade the pagkage

Binyamin
10 Apr, 2023, 17:50

Have you try that? What error you got?

Mosh Ontong
10 Apr, 2023, 17:52
TypeScript
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /usr/code-start/src/index.js
require() of ES modules is not supported.
require() of /usr/code-start/src/index.js from /usr/local/src/server.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /usr/code-start/package.json.
Binyamin
10 Apr, 2023, 17:52

And you change all the require to import?

Mosh Ontong
10 Apr, 2023, 17:55

finally its work I just downgrade the package

Mosh Ontong
10 Apr, 2023, 17:55

anyways thank you for your time to assist me

Mosh Ontong
10 Apr, 2023, 17:55

god bless

Binyamin
10 Apr, 2023, 17:55

👍

Osman
27 Jul, 2023, 15:16

i want to use ES modules, i inserted type: module in package js, and changed all require to import statements, but still get the "Must use import to load ES" as above @Binyamin

Guille
27 Jul, 2023, 16:55
Guille
27 Jul, 2023, 18:14

[SOLVED] Type module in node js clound function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more