Modules Not Found When Using Express
Solution 1:
I have tried every solution available on the internet and nothing worked actually. My project was using angular universal and it was express to render the application on the server side. On one morning suddenly I started getting this error and my project was not running.I tried the browser :{ "fs": false } fix suggested by someone. That even did not work. It took my entire day. I started analyzing my old code and found I did not add anything on package.json so it could not be any other dependency. When I found what was wrong it was silly as hell. I accidentally imported Router from express in a component
What was wrong
import { Router } from'express';
What should be
import { Router } from'@angular/router';
Solution 2:
You have now 2 applications in your solution:
1) client application: angular which runs in a browser environment
2) server application: express which runs in a node environment
When building the angular application (using ng build) you have to make sure you don't include the express application files in the angular build. The node modules used by express are not available in a browser environment.
You can do:
1) move the express source out of the src folder into project root
// server.jsvar express = require('express');
var app = express();
app.use(express.static('dist')); // or where the output of the ng-build is placed
app.listen(3000, function() { console.log('Server running on port 3000'); });
2) run ng-build
3) run node server.js
from project root. this will keep running
4) go to localhost:3000
and see your app
Post a Comment for "Modules Not Found When Using Express"