Running an angular app using node js - javascript

I have a html page and a serviceController.js, I need to automatically run the app through node js. Sorry for the naive question but am unable to run it. My serviceController.js looks like this :
var app = angular.module("app", []);
app.controller('serviceController', ['$scope', '$interval','$http', function($scope, $interval, $http)
{ $scope.service1 = {//and so on..
}
I was following an example and I have tried the following: created a package.json :
{ "name": "express-html",
"version": "0.0.1",
"dependencies": {
"express": "^4.11.0"
}}
My server.js looks like this :
var express = require("express");
var app = express();
var path = require("path");
app.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
});
app.listen(3000);
console.log("Running at Port 3000");
But, when I try starting the application from terminal using: node server.js. I get the following error :
(function (exports, require, module, __filename, __dirname)
{ var app = angular.module("app", []);
^ReferenceError: angular is not defined at Object.<anonymous> (/Users/dem/Desktop/frontend/task3/serviceController.js:1:81)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/dem/Desktop/frontend/task3/server.js:5:19)
at Module._compile (module.js:570:32)
Please guide!

app.use("/", express.static(__dirname)); Use this in place of
res.sendFile(path.join(__dirname+'/index.html'));

Update server file to serve a static directory
like app.use(express.static('dirPath'))
For more detail visit Serving static files in Express

Related

Why nodejs didnt require p-queue?

I want to use p-queue npm nodejs. But it get me error. Help me why it was and how to fix it? Here my code:
const shell = require("shelljs");
const async = require("async");
const fs = require("fs");
const path = require("path");
const { default: PQueue } = require("p-queue");
const queue = new PQueue({ concurrency: 1 });
And this error:
internal/modules/cjs/loader.js:1080
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
C:\Users\Feruz\Desktop\codeMaster\judge\node_modules\p-queue\dist\index.js
require() of ES modules is not supported. require() of
C:\Users\Feruz\Desktop\codeMaster\judge\node_modules\p-queue\dist\index.js
from C:\Users\Feruz\Desktop\codeMaster\judge\judge.js is an ES module
file as it is a .js file whose nearest parent package.json contains
"type": "module" which defines all .js files in that package scope as
ES modules. Instead rename index.js to end in .cjs, change the
requiring code to use import(), or remove "type": "module" from
C:\Users\Feruz\Desktop\codeMaster\judge\node_modules\p-queue\package.json.
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\Feruz\Desktop\codeMaster\judge\judge.js:5:29)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) { code: 'ERR_REQUIRE_ESM' } [nodemon] app crashed - waiting for file
changes before starting...

NODE [ERR_MODULE_NOT_FOUND]: Cannot find module './config/app'

FIXED, the names of the folders in my pc changed when uploaded to github
So, my app looks something like this
├── config
| ├── app.js
|
└── index.js
app.js
Here i have all my env variables, so I have a central point from which to extract them
require('dotenv').config()
module.exports = {
appPort: process.env.PORT,
appUrl: process.env.APP_URL,
appKey: process.env.APP_KEY,
frontendUrl: process.env.FRONTEND_URL
}
index.js
central file of the server
const express = require('express')
const cors = require('cors')
const app = express()
const { appPort, frontendUrl } = require('./config/app')
app.listen(appPort , () => {
console.log(`Server listening on port ${appPort}`);
})
The problem here is that when i deploy it and starts the server, it throws me error
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module './config/app'
Require stack:
- /app/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/app/index.js:8:34)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/index.js' ]
}
and i really don't know why, because it works perfectly fine in my pc
the code for the whole project is actually much bigger, but only this files are the ones throwing me errors
Hope someone can help see what is happening that throws that error

cannot find module in node express

cannot find module in node express
Actual code
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
error code in cmd
C:\Users\user\Desktop\project>npm express -v
7.5.3
C:\Users\user\Desktop\project>npm utils -v
7.5.3
C:\Users\user\Desktop\project>node server
node:internal/modules/cjs/loader:928
throw err;
^
Error: Cannot find module './utils'
Require stack:
- C:\Users\user\Desktop\project\node_modules\qs\lib\stringify.js
- C:\Users\user\Desktop\project\node_modules\qs\lib\index.js
- C:\Users\user\Desktop\project\node_modules\express\lib\middleware\query.js
- C:\Users\user\Desktop\project\node_modules\express\lib\application.js
- C:\Users\user\Desktop\project\node_modules\express\lib\express.js
- C:\Users\user\Desktop\project\node_modules\express\index.js
- C:\Users\user\Desktop\project\server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (C:\Users\user\Desktop\project\node_modules\qs\lib\stringify.js:3:13)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:997:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\user\\Desktop\\project\\node_modules\\qs\\lib\\stringify.js',
'C:\\Users\\user\\Desktop\\project\\node_modules\\qs\\lib\\index.js',
'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\lib\\middleware\\query.js',
'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\lib\\application.js',
'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\lib\\express.js',
'C:\\Users\\user\\Desktop\\project\\node_modules\\express\\index.js',
'C:\\Users\\user\\Desktop\\project\\server.js'
]
}
first try to remove the globally installed express
then initialize a package.json file with npm init. Here you will have a list of your dependencies, scripts and name of your package and some other data.
Then install express with npm i express.
after installing try to run it with node server.js or npm start if you have written the script to run the start command in package.json
I got the answer
Download utils https://www.npmjs.com/package/utils
this is the error
C:\Users\user\Desktop\project>
node server
node:internal/modules/cjs/loader:928
throw err;
^
Error: Cannot find module 'utils'
Require stack:
- C:\Users\user\Desktop\project\server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (C:\Users\user\Desktop\project\server.js:3:15)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\Users\\user\\Desktop\\project\\server.js' ]
}
C:\Users\user\Desktop\project>
npm i utils
added 43 packages, removed 2 packages, changed 3 packages, and audited 110 packages in 16s
found 0 vulnerabilities
C:\Users\user\Desktop\project>node server
Example app listening at http://localhost:3000
^C
C:\Users\user\Desktop\project>

Compile with nexe the main process electron js?

Is it possible to compile the main process of electron main.js with some tool? I am using nexe to compile the main process main.js but it doesn't work.Is there a way to do that?
I am using https://github.com/electron/electron-quick-start
Then I use nexe main.js to generate a compilation.
Then I modify package.json by changing the value of main.js to main.exe.
Finally npm start and I get an error message:
MZ´┐¢
SyntaxError: Invalid or unexpected token
at Module._compile (internal/modules/cjs/loader.js:895:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1004:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module._load (electron/js2c/asar.js:769:28)
at loadApplicationPackage (C:\Users\Onimac\AppData\Roaming\npm\node_modules\electron\dist\resources\default_app.asar\main.js:109:16)
at Object.<anonymous> (C:\Users\Onimac\AppData\Roaming\npm\node_modules\electron\dist\resources\default_app.asar\main.js:155:9)
at Module._compile (internal/modules/cjs/loader.js:967:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1004:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
Minified main.js code:
const {app, BrowserWindow} = require('electron');
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600
})
mainWindow.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
})

Node and Require import error in Mocha?

I have a working Node application and I'm trying to add Mocha tests, but getting some odd import errors.
This is my file structure:
package.json
index.js
src/
chart.js
highcharts-options.js
test/
test_chart.js
This is what my chart.js file looks like:
var chartOptions = require('./highcharts-options');
var analyseChart = {
doSomething: function() { ... }
};
module.exports = analyseChart;
And this is what highcharts-options.js looks like:
var HighCharts = require('highcharts-browserify');
Highcharts.theme = { ... };
Currently I import everything from /src into a single index.js file, then bundle it with browserify, which works just fine, no errors in the application.
Now I want to start writing Mocha tests for the functions in /src.
This is my first stub in test_chart.js:
var chart = require('../src/chart');
chart.doSomething();
But when I run mocha, I get the following error:
Users/.../js/src/highcharts-options.js:11
Highcharts.theme = {
^
ReferenceError: Highcharts is not defined
at Object.<anonymous> (/Users/.../js/src/highcharts-options.js:11:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/src/chart.js:7:20)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/.../js/test/test_chart.js:1:75)
How can I fix this import error for Mocha?
var HighCharts = require('highcharts-browserify');
Highcharts.theme = { ... };
You have a spelling error.
High[cC]harts
How did you not notice this yourself even when you were told exactly what was wrong?
Some errors are so common that we ultimately stop trusting them, and start looking for other faults in our code without even investigating it.
This is especially true for the "someVar is undefined" error in javascript.
Takeaway:
Sometimes you just got to trust your error messages.

Categories