Call node js module and function into LitElement file

I’m trying to call a javaScript function that’s in test.mjs file to contact-form.jsfile, but when I try it, doesn’t work and I have the following error:

Uncaught (in promise) SyntaxError: The requested module '../../../node_modules/emailjs/rollup/email.cjs' does not provide an export named 'SMTPClient'

And when I just import like import 'emailjs'; in test.mjs the error is: exports not defined but the exports exists in emailjs

Is there a way to do this? or another way to implement that but within contact-from.js without test.mjs ?

It looks like your import function could have the wrong file name. You have

import { STMPClient } from 'emailjs';

and not:

import { STMPClient } from 'email.js';

If thats not it, then you may not be properly exporting STMPClient from email.js

Yes but emailjs is a module, not a file,

You’re trying to import a CommonJS (Node) module: exports is not valid JS module syntax, JS modules use export.

I always get those mixed up my as well. :sweat_smile:

Yes but the error is the following when I try to import the module emailjs like: import 'emailjs' in test.mjs

And when I import like: import {SMTPClient} from "emailjs" I have the following error:

test.mjs:3 Uncaught (in promise) SyntaxError: The requested module '../node_modules/emailjs/rollup/email.cjs' does not provide an export named 'SMTPClient'

haha no problem but the error is with the emailjs module

Yes, that’s what would throw the error. emailjs is a commonjs module, and you are trying to import it into some environment that’s using standard JS modules (mjs extension is indicating this, but I don’t know what your setup is so can’t tell anything further than that atm).

The resolution algorithm of whatever you are using looks in emailjs'package.json at the “module” property (rather than the “main” property.

Well the configuration is node js, contact-form.js was done with LitElement but I am trying to send data from a form through a method that I want to import from external file using commonJS but when I send the data and I am sending it to test.mjs or the same file but test.js does not work

No I mean this is a browser app. How are you using the library: it needs to either be bundled by a module bundler into one file (tool like Webpack, Rollup, ESBuild etc), or it needs to be an ES module.

The browser can’t import a library from node_modules, node_modules doesn’t exist

Ah okay, yes is Rollup, but you mean that I should be setup in the rollup file or something like that?

I don’t know how you’ve got things set up at the minute: how are you using lit-element?

You can just import the library directly on the front end for now if you want:

Though going forward, it is probably easier to have all your JS bundled into a single file using something like Rollup, just using ES Modules isn’t the best experience at the minute

Ah hold on: is this the library you use for browsers or is it just for use in Node, because there’s emailjs-com as well

Yes is just for use in Node

And Im not use the emailjs-com

Yes, when I use this link, I have the following error:

You’re dealing with two different things here: the bit of your app that the user interacts with is in the browser – you can’t use node libraries you’re using server-side there, the client code doesn’t have access to them. You have to treat any JS you use there as completely seperate. So you can’t import this stuff into the client code even if you sort out the import/export issue. You need something designed for the browser that you can import into your code that can interact with whatever you have built server-side

You effectively have two seperate applications: the one that runs on a server somewhere, the Node app, and the one that runs in a user’s browser, which is HTML/JS. The former may serve HTML pages to facilitate the latter, but the latter can’t use the same JS code. The only way they can talk is over HTTP, so maybe you have some routes defined in the Node app. The client sends a request to one of those URLs, the server responds with some HTML (or JSON or whatever). You may have them in the same folder on your computer, but when they run, they don’t really share anything. One is a client for the other

Yes, fs is a Node standard library module for dealing with the filesystem. It doesn’t exist in the browser. You can use a version of it built so that it works in browser, and import that in your client code, but 90% of it will be useless because you absolutely cannot get access to a file system inside the browser. So emailjs has that as a dependency: this is normal, and expected (it will probably use a load of other Node standard library modules as well): it’s a Node library, you can’t use it client-side.

@DanCouper
Alright, so, thank you for your time, I’ll try it with another thing, thanks

Best regards

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