Back

error using import statement in nodejs

  • 0
  • Functions
gautam24
12 Mar, 2023, 02:05

Trying to use ES6 import statement in index.js instead of require(). I have also added "type":"modules" in package.json. Still getting error when i deploy but running locally works fine in node 19.7.0 . Here are logs after running function:- 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.

TL;DR
The user is experiencing an error when trying to use the ES6 import statement in Node.js. They have added "type":"modules" in the package.json file but are still encountering an error when deploying. Running the code locally works fine in Node 19.7.0. The error message suggests that the user should rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from package.json.
Drake
12 Mar, 2023, 03:12

I'm not sure if you can change how you do the imports...you should be importing like the starter code.

Drake
12 Mar, 2023, 03:14

This is because your code is added to the runtime code: https://github.com/open-runtimes/open-runtimes/blob/main/runtimes/node-16.0

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