Error while using import statements in in server.js

I’ve upgraded my app to platform version 2.3 to make use of nodejs 18.x.

My manifest.json looks like this.

"platform-version": "2.3",
"dependencies": {
  "applicationinsights": "2.5.1",
  "franc": "6.1.0",
  "axios": "0.27.2"
},
...
"engines": {
  "node": "18.16.0",
  "fdk": "9.0.0"
}

While using import statements in my server.js file, I get the following error.

(event.js) error while parsing server file "'import' and 'export' may appear only with 'sourceType: module'"
(event.js) SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'

The beginning of my server.js file looks like this…

import { franc } from 'franc';
import CloudStorage from './services/CloudStorage';
import OpenAI from './services/OpenAI';

While nodejs v18 officially supports ES modules, it seems like fdk is throwing these errors.

I am unable to use npm packages like franc, which are ESM-only modules, meaning they can’t be required in my server.js.

@kaustavdm @zach_jones_noel @Raviraj

@Developer-Platform What do we need to do to support ESM imports?

You also need to put "type": "module" in your package.json and more.
Pure ESM package · GitHub

You will set "type": "module" in the package.json file within the server folder. Currently, developers don’t have access to that file.
I’m guessing that the fdk picks up the dependencies from manifest.json and constructs the package.json before npm installing all the dependencies.

1 Like

Thanks for the inputs, @arunrajkumar235. Support for ES modules and the import and export statements have come up a few times now. We will work with our engineers to see if we can ship it.

Meanwhile, I don’t see a way to require() this module, as you have noted.

1 Like

Thanks for acknowledging this @kaustavdm . Looking forward to support for ESM-only modules.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.