Express throwing module errors

I encountered an issue while attempting to expose a REST service in an electron app using expressJS. Following a tutorial, I added express and @types/express to the project. However, when trying to implement a "get" method and running the build with ng build --prod, I received the following errors:

ERROR in ./node_modules/cookie-signature/index.js
Module not found: Error: Can't resolve 'crypto' in 'app\node_modules\cookie-signature'
...

This is my first time working on such a task, so please bear with me if I'm unfamiliar with some concepts.

Here is an excerpt from my package.json:

> {
  "name": "angular-electron",
  ... (omitted for brevity)
}

The project was functioning perfectly until I integrated the express part using the following code snippet:

var app = express()

app.get('/', function (req, res) {
    res.send('Hello World')
})

Answer №1

I have exhausted all potential solutions found online, yet none seemed to work for me. My project utilized angular universal with express for server-side rendering. One morning, out of nowhere, I encountered an error that prevented my project from running smoothly. Despite attempting the suggested fix in the browser: {"fs": false}, the issue persisted and consumed the entirety of my day. After careful examination of my existing codebase, I realized that I had overlooked a crucial detail - I inadvertently imported Router from express within a component.

The mistake:

import { Router } from 'express';

The correction:

import { Router } from '@angular/router';

Answer №2

Your solution now consists of 2 applications:

1) The client application, angular, which operates in a browser environment

2) The server application, express, which functions in a node environment

When constructing the angular application with `ng build`, it is crucial to ensure that the express application files are not included in the angular build. The node modules utilized by express are not compatible with a browser environment.

Here's what you can do:

1) Shift the express source code out of the src folder and into the project root

// server.js
var express = require('express');
var app = express();

app.use(express.static('dist')); // or specify where the output of ng build is located

app.listen(3000, function() { console.log('Server running on port 3000'); });

2) Execute `ng build`

3) Run `node server.js` from the project root. This command will continue running

4) Access `localhost:3000` in your browser to view your application

Answer №3

Always double-check your application to ensure that it has not mistakenly imported any unnecessary packages.

I encountered this problem recently myself. It turns out that sometimes the application automatically adds packages while we are working on it. For example, when I was making a fetch request, {JSON, Express} were imported without my knowledge.

If you find yourself mixing backend packages within the src folder for front-end development, you may run into similar issues.

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

What is the reason for the Pointer Lock API entry displaying a significant number when the window is compressed?

Take a look at these two screenshots that illustrate the issue: The first screenshot demonstrates the correct behavior - the fullscreen mode works perfectly. However, in the second screenshot, a problem arises. Depending on the size of the browser window, ...

How can I use JavaScript to retrieve information from a different webpage?

I am trying to extract data from another webpage using JavaScript, jQuery, or Ajax without using PHP. I came across a helpful example on Stack Overflow (link here). However, when I implement these codes in my index.html file, it doesn't seem to work. ...

Is there a way for me to revert back to my initial list after the 3rd click?

Front end development const [clientList, setClientList] = useState([]); //store all data from the database in a list //make an axios request to retrieve data from the database useEffect(() => { Axios.get("http://localhost:3001/clients&quo ...

Utilizing Node.js to match arrays

let items = ["Product",["car","Bike"]]; let result = items[1].map((item) => [items[0], item]); console.log(result); My current output is: [["Product" , "car"], ["Product" , "bike"]], The above code works for this simple output, but I'm unsure ho ...

Only a fragment of the .attr() method

I am trying to work with an image HTML block <img src="folder1/folder2/folder3/logo1.png"> situated inside a large div similar to this structure <div id="editorial"> <div id="img_editorial"><img src="folder1/folder2/folder3/logo1. ...

What is the best way to wait for the state to be set before mapping the array

I have some data stored in an array (shown in the screenshot below) that I am trying to map, but I am facing issues accessing it as it is loaded asynchronously. How can I await the data? Is there a way to achieve this within the render function? render() ...

Creating a seamless rotation effect on an SVG shape at its center across all browsers, even Internet Explorer

Is there a way to make an SVG figure rotate around its center? I have been trying to calculate the rotation center and scale it based on the viewBox. It seems to work perfectly fine in Chrome, Firefox, and Safari, but I just can't seem to get it to wo ...

Locate the parent element using a unique child element, then interact with a different child element

Can you help me come up with the correct xpath selector for this specific element? The element only has a unique href. Is it possible to locate the element by searching for the text iphone X within the href="#">iphone X and also checking for its paren ...

successive ajax requests

I am facing a challenge where I need to execute two separate ajax calls sequentially. The second call relies on the result of the first call for its data. Despite my efforts, I haven't been able to achieve the desired outcome. Here's what I have ...

Is there a way to accomplish this without using settimeout?

Whenever the Like button is pressed, I want to trigger the loading process I expect to see LOAD_POST_SUCCESS immediately after LIKE_POST_SUCCESS Pressing the Like button should initiate the load process After LIKE_POST_SUCESS, I want to see LOAD_POST_SU ...

How to use jQuery to remove the last element from an array when the back button

Currently encountering an issue with removing the last element from an array when a back button is clicked. The console is displaying the correct elements in the array, but it seems that the array.slice function is not working as expected, and I'm str ...

Looking for a method to substitute "test" with a different value

Showing a section of the menu using <li id="userInfo" role="presentation" data-toggle="tab" class="dropdown"> <a href="#" name="usernameMenu" class="dropdown-toggle" data-toggle="dropdown" role="button"> <span class="glyphicon glyph ...

How can I utilize express and node-http-proxy to forward proxy requests to an https server?

Is it possible to utilize the node-http-prox module along with proxy.proxyRequest for an https server? I attempted the following code but it doesn't seem to be functioning correctly. app.get('/c/users/moreuser', function(req, res) { pro ...

Issue with conditional comment in IE 8 not functioning as expected

Struggling with changing the SRC attribute of my iFrame specifically for users on IE 8 or older. This is the code I have written: <script> var i_am_old_ie = false; <!--[if lte IE 8]> i_am_old_ie = true; <![endif]--> </script> ...

Transform the text column into JSON format

We currently have a resource bundle/properties formatted as follows: [tag1] server1 server2 [tag2] server3 server4 [tag3] server5 server6 [No Software Installed] server7 [tag2] server8 [tag5] server9 [tag1] server10 server11 [tag3] server12 server13 serve ...

A guide on resolving the 'Unexpected identifier array in JSON Parse Error' encountered during an AJAX Request

I'm currently working on setting up a Contact Form Validation and I require the variable $msg from a php script called 'sendEmail.php'. The actual mailing system is functional, as the script successfully receives form inputs from index.php a ...

How to Overcome Read-only HTML Components in Selenium with Python?

I am working on automating a task using Selenium in Python, and part of it involves selecting a date. The website I am testing has an input box for date selection that displays a standard date table when clicked. Unfortunately, the input text box is read- ...

Is it possible to combine JavaScript objects using TypeScript?

Currently, I am dealing with two objects: First Object - A { key1 : 'key1', key2 : 'key2' } Second Object - B { key1 : 'override a' } I am looking to combine them to create the following result: The Merged Re ...

I am eager to develop a Loopback model tailored specifically for handling this JSON data

"US Virgin Islands": [ "Charlotte Amalie", "Christiansted", "Frederiksted", "Kingshill", "St John Island" ], I'm currently working with a JSON file that contains country names and corresponding cities. I want to store this data in my database using M ...

Show an alternative option in the dropdown menu

Currently working with the Select component from Material UI, and here's what I'm trying to achieve:https://codesandbox.io/s/divine-water-zh16n?file=/src/App.js Situation: With an account object like {id:1, name:'name'}, I want to sel ...