Utilizing a local module in NodeJS

I have developed a TypeScript module which contains the following functions:

export function hello() {
  console.log('hello');
}

export function bye() {
  console.log('bye');
}

In my HTML file, I am trying to use these functions like this:

  <head>
    <script src="./assets/js/myModule.ts" type="module"></script>
  </head>
  <body>
    <span onclick="myModule.hello()">Press to greet</span>
  </body>
...

The application also consists of a server.js file:

const proxy = require("http-proxy-middleware").createProxyMiddleware;
const Bundler = require("parcel-bundler");
const express = require("express");

const bundler = new Bundler("index.html");
const app = express();

app.use(
  "/api",
  proxy({
    target: process.env.API_SERVER || "http://localhost:1337/"
  })
);

app.use(bundler.middleware());

app.listen(Number(process.env.PORT || 1234));

However, whenever I start the server, I encounter an error message stating:

myModule is not defined

So far, I have attempted the following solutions:

  • Using npm link.
  • Requiring the file in the server.js and passing it with app.use(myModule.ts)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with timezone mismatches between Node.js and MySQL databases

I am struggling with the timezone settings on my server. My backend is using Node.js and Express routes for services. I adjusted the server time to the correct one by running: dpkg-reconfigure tzdata I verified that the server time appears accurate. ...

Error: Revelation 404 (Gateway not found)

I encountered a 502 (Bad Gateway) error when using Stripe. Although the payment is successfully processed and appears in the Stripe dashboard, it does not reflect as successful on the front end, and instead, I receive a 502 error. Do I need to include som ...

I encountered an issue during the installation of react-typical via npm

Oops! An error occurred: C:\Users\aselemidivine\Desktop\portfolio_website-STARTER> npm i react-typical npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi ...

The challenge of obtaining a URL as a query string parameter in Node.js Express

What is the proper way to include a URL as a query string parameter in node.js? For example, if I were to access the browser with the following URL: http://localhost:3000/key1/https://www.google.com/key2/c, I am encountering difficulties retrieving value ...

The npm encountered an error with code ENOENT and an error number of 34

Here is my initial script setup for a React project. "scripts": { "prestart": "babel-node tools/startMessage.js", "start": "npm-run-all --parallel test:watch open:src lint:watch", "open:src": "babel-node tools/srcServer.js", "lint": "node_ ...

NodeJS Express Double Authentication using JSON Web Tokens

Currently, I am in the process of developing an express server that enables users to authenticate using two third-party services: Google and Steam. The authentication is accomplished through JWT and functions well when only one service is active. However, ...

Unlocking Node.js packages within React JS is a seamless process

Currently, I am developing a React Serverless App with AWS. I am looking for ways to incorporate a Node JS specific package into the React JS code without requiring Node JS on the backend. One package that I need access to is font-list, which enables list ...

What is the reason for encountering an error when attempting to use a let variable in a new block before reassigning it

Check out this document here where I attempt to explain a coding scenario: // declare variables const x = 1; let y = 2; var z = 3; console.log(`Global scope - x, y, z: ${x}, ${y}, ${z}`); if (true) { console.log(`A new block scope - x, y, z: ${x}, $ ...

Perform an action when the timer reaches zero

I am working with a database entry that contains the following information: { _id:"fdjshbjds564564sfsdf", shipmentCreationTime:"12:17 AM" shipmentExpiryTime:"12:32 AM" } My goal is to create a timer in the front end ...

The connection between NodeJS and MongoDB is failing due to a refused connection on server ECONNREF

I've been working with a basic script designed to test database connectivity. Upon attempting to execute the script using node Connection.js, it appears to run without any errors or successful connection messages displayed in the console. It seems as ...

Obtain information from the get request route in Node.js

I've been diving into nodejs and databases with the help of an online resource. As part of my learning process, I have been tasked with replicating the code below to fetch data from app.use('/server/profil'); However, I'm encountering ...

Uh-oh! We encountered a little hiccup while running NodeJS on Azure with Sequelize for SQL

So I have been working on developing a NodeJs application on my Windows machine. Recently, I decided to deploy it on Azure cloud and set up a SQL Server instance. During the testing phase, where the node app was running locally and the SQL Server was conn ...

"Compiling RethinkDB from source on Ubuntu: A step

While attempting to compile rethinkdb from source, I encountered the following error: npm WARN engine <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="73121e1716151a1d1633435d425d42">[email protected]</a>: wanted: ...

Using Node JS to consolidate information from a join table

I am facing an issue with joining two tables in PostgreSQL for my quiz application. The data retrieved from the join table does not match my expectations. I am wondering if there is a way to group the data using a JavaScript function? Here are the table ...

Instructions for making a specific version of node the default using the n package

I successfully installed node using the npm commands listed below. sudo npm cache clean -f sudo npm install -g n sudo n 8 The command sudo n 8 installed version node 8. $ sudo n 8 install : node-v8.11.3 mkdir : /usr/local/n/versions/node/8.11. ...

Node.js accepts JSON data sent via XMLHttpRequest

I have successfully implemented a post method using xmlhttprequest: var xhttp = new XMLHttpRequest() xhttp.onreadystatechange = function () { if (this.readyState === 4 && this.status === 200) { console.log('Request finished. Pro ...

Despite being present in the node_modules folder, the ag-grid-vue module appears to be missing

Currently, I am diligently following the Vue.js AgGrid getting started tutorial step by step: https://www.ag-grid.com/vuejs-grid/ However, upon adding the <script> section and saving my progress, an error promptly appears: ERROR Failed to compile ...

Tips on storing JSON array data in mongoose via req.body?

I've been struggling with this issue for some time now. After successfully using JSON and req.body to save data to my MongoDB database in Postman, I decided to work with arrays for the first time. However, I'm encountering difficulties. (Just t ...

Module for managing optional arguments in Node.js applications

I'm on the hunt for a Node.js module that can effectively manage and assign optional arguments. Let's consider a function signature like this: function foo(desc, opts, cb, extra, writable) { "desc" and "cb" are mandatory, while everything else ...

"Utilizing Node.js to access modules with their absolute

My directory structure is as follows: -- app/ |- models/ |- user.js |- config.json I am trying to make my user.js file require the config.json. Currently, I am using require('/config') but it doesn't seem to be working. Can som ...