Express Not Installing - javascript

I know that this has been asked before but none of the solutions worked for me. Whenever I try to install express, it seems to work just fine, displaying this message:
npm WARN website#1.0.0 No description
+ express#4.17.1
updated 1 package and audited 50 packages in 6.581s
found 0 vulnerabilities
However, when I try to run my program that uses express:
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
at Module.require (internal/modules/cjs/loader.js:961:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (C:\Users\darcy\OneDrive\Sutton\Website\server.js:2:15)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12) {
code: 'MODULE_NOT_FOUND',`
Here is my code:
let express = require("express");
let account = require("accountBack");
let shop = require("shopBack")
let serverConnection = express();
serverConnection.listen(3000, () => {});
serverConnection.use(express.static("public"));
serverConnection.use(express.json());
serverConnection.post("/account", account.accountServer);
serverConnection.get("/shop", shop.sendShop);
I am using code from other files (hence the
extra 2 require statements) but what they do I do not believe to be significant.
Here are the commands that I am typing in:
npm init
npm install express
node server.js
It does not appear from the error that express is gettinginstalled correctly, but I'm not sure. Is it a problem with the commands that I a typing in or something else? Any help would be appreciated.

For the internal module, you should import like this
const account = require("./accountBack");
const shop = require("./shopBack")
Unless you publish those modules to npm registry (private/public), then you can import like what you did.

Related

Mammoth not finding any modules

I am trying to create a Word to HTML converter, and I am trying to use Mammoth as a framework. Whenever I run my script, I get:
Internal/modules/cjs/loader.js:983
throw err;
^
Error: Cannot find module 'mammoth'
Require stack:
- C:\Users\magnu\OneDrive\Documents\GitHub\work_app\app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\magnu\OneDrive\Documents\GitHub\work_app\app.js:4:15)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\Users\\magnu\\OneDrive\\Documents\\GitHub\\work_app\\app.js' ]
}
This is the code for my app.js
// Requirements
var express = require('express'),
ejs = require('ejs'),
mammoth = require('mammoth')
var app = express();
app.set('view engine', 'ejs');
// Mammoth
mammoth.convertToHtml({path: "../../../../Downloads/Federal Gov't - Debate Sheet.docx"})
.then(function(result){
var html = result.value; // The generated HTML
var messages = result.messages; // Any messages, such as warnings during conversion
})
.done();
app.get('/', function (req, res) {
res.render('landing');
})
app.listen(4009, function () {
console.log("Server ready on PORT 4009");
})
If anyone knows the solution to my problem, please post below.
For all of my code, go to Github
The answer to this is very simple. Just make sure you have installed the package.
But I am sure I have already installed the package. What now?
My next course of action is to make sure you have it under dependencies in the package.json file you store other dependencies in. To automate this process, create a GitHub page and start commiting to it. This can speed up your development process by months simply because other people are able to help you.
If those don't work, ask a question and comment it to me and I might be able to help!

node.js SyntaxError: Unexpected token { in Version v0.10.43

I habe the following line of code in my js file:
const { connectDb } = require("./db");
When I run nodejs locally everthing is fine and I dont get an error.
Im using v10.16.0 locally. When I put that on a server, I got the follwoing error:
2019-07-23 15:49:36.925754500 const { connectDb } = require("./db");
2019-07-23 15:49:36.925779500 ^
2019-07-23 15:49:36.930604500 SyntaxError: Unexpected token {
2019-07-23 15:49:36.930606500 at Module._compile (module.js:439:25)
2019-07-23 15:49:36.930607500 at Object.Module._extensions..js (module.js:474:10)
2019-07-23 15:49:36.930608500 at Module.load (module.js:356:32)
2019-07-23 15:49:36.930609500 at Function.Module._load (module.js:312:12)
2019-07-23 15:49:36.930610500 at Function.Module.runMain (module.js:497:10)
2019-07-23 15:49:36.930611500 at startup (node.js:119:16)
2019-07-23 15:49:36.930612500 at node.js:945:3
I'm running Version v0.10.43 on that server. What am I doing wrong?
I'm running Version v0.10.43 on that server. What am I doing wrong?
You're running v0.10.43.
Is it too old to support destructuring (which was added in 6.0.0 … which is also past end of life!).
You could replace it with:
const connectDb = require("./db").connectDb
… but are likely to run into other problems down the line.
v0.10.43 is from 2013. It is over half a decade old. It is past the end of life. It is not supported. It does not get security releases. Don't use it.
Use a supported version instead.

Module not found unless I use a complete path when creating plugins in Hapi

I'm trying to familiarise myself with HapiJS and have been playing around with it this week, I've run into a problem with plugins and paths. I get an error regarding the path I specify when I require a file. I can't use " ./ " without getting an error. The only way to overcome the error is to use the full complete path.
Here's my code that works:
'use strict';
const indexController = require('/Users/mylaptop/docker-node/controllers/IndexController.js');
module.exports.plugin = {
name: 'myPlugin',
version: '1.0.0',
register: async function (server, options) {
// Create a route for example
server.route({
method:'GET',
path:'/test',
handler: function (request, h) {
return indexController.loadIndex(h);
}
});
}
};
However, if I try to require my IndexController file this way:
const indexController = require('./controllers/IndexController.js');
Then I get this error:
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module '/Users/mylaptop/docker-node/Users/mylaptop/docker-node/controllers/IndexController.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/Users/mylaptop/docker-node/config/routes/index.js:5:25)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
It doesn't work inside of my plugin, yet outside of my plugin, requiring files this way works fine.
What could be the problem? How can I solve it?
Thanks in advance.
Problem solved with:
const indexController = require('../../controllers/IndexController.js');
My project root folder was 2 directories away, hence ../../ working for me.

Running a JavaScript program requiring jsfeat

I have downloaded jsfeat from here, and unzipped the downloaded file.
Inside the directory, I wrote the following:
require("./build/jsfeat.js");
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
matrix.data[1] = 4;
console.log(matrix.data[1]);
I ran the program as follows:
$ node matrix.js
But, got the following error:
/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2
var matrix = new jsfeat.matrix_t(2,2, jsfeat.U8_t | jsfeat.C1_t);
^
ReferenceError: jsfeat is not defined
at Object.<anonymous> (/Users/abc/Desktop/JavaScript/inspirit-jsfeat-59cc928/matrix.js:2:19)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
Why is that? How can I solve the issue?
Thanks.
You need to assign the imported module to a variable:
var jsfeat = require("./build/jsfeat.js");
If you prefer you could install the module with NPM to save downloading it manually:
$ npm install jsfeat
The module will be saved to ./node_modules/jsfeat. You can then require it with var jsfeat = require('jsfeat').

How do I get html-snapshots to work?

I'm just trying to run html-snapshots. This should be easy, right?
This is what I started with:
npm install html-snapshots
That's all I need, right? Here's my snapshots.js file:
var htmlSnapshots = require('html-snapshots');
htmlSnapshots.run({
source: "sitemap.xml",
hostname: "localhost",
outputDir: "snapshots",
outputDirClean: true,
selector: "#results-widget"
});
And to run it:
node snapshots.js
But nooo:
module.js:340
throw err;
^
Error: Cannot find module '.\robots'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.module.exports.create (C:\webdev\node_modules\html-snapshots\lib\input-generators\index.js:38:16)
at Object.module.exports.run (C:\webdev\node_modules\html-snapshots\lib\html-snapshots.js:42:39)
at Object.<anonymous> (C:\webdev\snapshots.js:2:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
wtf?
Additional Info...
This is part of html-snapshots.js:
var inputFactory = require("./input-generators");
...
run: function(options, listener) {
...
var inputGenerator = inputFactory.create(options.input);
...
result = inputGenerator.run(options, (function(options, notifier){
Also, the html-snapshots\lib\input-generators folder contains the file robots.js
It looks like an issue inside html-snapshots\lib\input-generators\index.js file - it works fine on Linux systems but fails on Windows (path.sep has been used to build module name)
Problem is that it should load './robots' module instead of '.\robots'.
Quick fix is to update html-snapshots\lib\input-generators\index.js file - line 38.
Change line:
result = require(file);
to:
result = require(path.join(__dirname, file));
And it will work fine. I hope that will help.

Categories