Mustache and Express in Node - node.js

I'm trying desperately to tie in the mu2 module into Express in node.js. However, I can't seem to figure it out, and when trying to run the example using the mu2express module, I keep getting this error when trying to run:
module.js:340
throw err;
^
Error: Cannot find module 'mu2Express'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/myapp.js:1:80)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Is this an error caused by something locally, or with the module itself? Right now, I'm using the node http module to work with mu2 exclusively, but I really want to use express is possible.
Can anyone out there help? Is there more information I should provide? I'm very new to node.js and I could use some direction if possible!

Look into consolidate:
https://github.com/visionmedia/consolidate.js/
It is made by the man behind Express himself and has support for Hogan and Handlebars.
EDIT:
You can initialize Express with this for consolidate
var express = require('express');
var cons = require('consolidate');
var app = express();
app.engine('html', cons.hogan);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
Renders will now be served from /views with HTML extension and Mustache-flavor syntax.
app.get('/', function (req, res) {
res.render('index', {msg: 'Hello world!'}
});
And a basic template, again just mustache-flavor syntax
Hello {{msg}}

You're requiring the mu2Express module from the myapp.js file, so you need to install it first.
You'll need to create a package.json file with at least the following content:
{
"name": "myProject",
"version": "0.0.1",
"dependencies": {
"mu2express": "~0.0.1"
}
}
And I'm not sure, that the examples are good for this project, you may need to require mu2express instead of mu2Express.

Related

Node.js Express JS - Cannot find module './config/express'

Im Learning to make a web application with Node.js Express JS.
When I run my server.js
$ node server
I get this
Error: Cannot find module './config/express'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/david/Desktop/Node/ejemplo/server.js:2:18)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
my server.js file is like this...
server.js:
var express = require('./config/express');
var app = express();
app.listen(3000);
module.exports = app;
console.log('Server running at http://localhost:3000/');
before doing this I run this command in the root of my app
npm install
Here is a like to a picture to show you the project folder structure I have.
I dont understand why cannot find module './config/express' while apparently the folder structure is fine.
Any idea??
I dont understand why cannot find module './config/express' while apparently the folder structure is fine.
after installing npm, you should try express installation into your project directory as
npm install express
it will create node_modules of express.
then you should use
var express = require('express');
Your server.js doesn't work because express.js file into config folder create confusion.
Change the name from express.js to init.express.js
and then it will work.

Error: Cannot find module 'express' when running on Azure

I have a node.js app which uses express and runs locally with no problems. However, on Azure I am seeing:
Application has thrown an uncaught exception and is terminated:
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\home\site\wwwroot\server.js:1:79)
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)
The packages.json file contains the dependency:
"express": "^4.15.3"
Assuming something had gone wrong with the npm install, I went to the Kudu remote execution console and ran npm outdated. No packages are missing.
This is my server.js file:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.port || 8000;
app.use(bodyParser.urlencoded({ extended: true }));
require('./app/routes')(app);
app.listen(port, () => {
});
I'm assuming it is something very obvious, but I can't work out what I'm missing.
Assuming your Azure App Service is Windows environment, try to include node_modules in the wwwroot.
There isn't much we can really help with here, as the error you have provided indicates that the express npm package has not been installed properly. So, I would suggest that you use App Service Editor (https://[YouAppName].scm.azurewebsites.net/dev/wwwroot/) to troubleshoot this issue by checking whether the express folder exists in the node_module.
You can also run command npm install in the console, restart your app and run it (Ctrl + F5) in the browser.

node server.js not responding error message

I am working on a project that requires the use of node server.js file. The documentation for the project states to type in 'node server.js' in terminal to start the server, but when I do that I'm receiving an error cannot find module 'express'.
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/.../Desktop/react/react-app-project/server.js:6:15)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
This is the code in server.js
var express = require('express');
var app = express();
/*
this says: serve all the files in the src directory if they match the URL
For example, if the client requests http://server/css/app.css then the file in src/css/app.css will be served
But if the client requests http://server/step-2 then since there is no file by that name the middleware will call next()
*/
app.use(express.static(__dirname + '/src'));
/* insert any app.get or app.post you need here. only if you do the advanced part */
/*
This says: for any path NOT served by the middleware above, send the file called index.html instead.
For example, if the client requests http://server/step-2 the server will send the file index.html. Then on the browser, React Router will load the appropriate component
*/
app.get('/*', function(request, response) {
response.sendFile(__dirname + '/src/index.html');
});
app.listen(process.env.PORT || 8080, function() {
console.log('server started');
});
Cannot find module 'express'
the reason is that you didn't install express yet.
solution: npm install express

how to install new nodejs modules to expressjs project?

Currently I am working on online yaml code editor. I use expressjs framework for it. I want to install yamljs module to it. How do I install it? Because following code line cause to an error.
var YAML = require('yamljs');
This is the code segment
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var YAML = require('yamljs');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
This is the error
Error: Cannot find module 'yamljs'
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.<anonymous> (/home/tharindu/Documents/projects/Group project2/CuubezFinal/app.js:12:12)
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)
I use webstorm for coding. I have installed yamljs module using
npm install yamljs
This library is perfectly using if I run my code manually from the ubuntu terminal. But When I run the expressjs project, It gives above error.
could you provide more information like the error text?
Have you tried
npm install yamljs
to make the module accessible to the node environment?
This library is perfectly using if I run my code manually from the
ubuntu terminal. But When I run the expressjs project, It gives above
error.
Make sure your node_modules folder is available in the same or a more outer directory than the folder of your executing script.

How can I fix this node.js path issue?

I'm in the process of refactoring my server.js file and trying to incorporate MVC pattern. I'm running into a problem trying to access my controller from my routes.js. I've tried just about every variation of absolute and relative path that I can think but I must be missing something.
Here is my directory structure:
And from my routes.js, here is my code:
module.exports = function ( app, passport, auth ) {
var Clients = require('controllers/clients');
app.get('/clients', Clients.list);
}
I don't think this is relevant, but here is my clients controller:
var mongoose = require('mongoose')
, Client = mongoose.model('Client');
exports.list = function( req, res ) {
Client.find( function( err, clients ) {
res.renderPjax('clients/list', { clients: clients, user: req.user });
});
}
Here is the error that I'm getting when trying to access my controller from routes:
module.js:340
throw err;
^
Error: Cannot find module 'controllers/clients'
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 module.exports (/Users/sm/Desktop/express3-mongoose-rememberme/app/routes.js:5:16)
at Object.<anonymous> (/Users/sm/Desktop/express3-mongoose-rememberme/server.js:334:24)
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)
I'm sure it's something simple that I'm over looking. How can I access my controller from
my routes?
To require something that isn't a separate package (isn't in node_modules), you need to use an explicitly relative path:
require('./controllers/clients')
For more information, see the documentation.
Local Modules
require(...) takes a relative path for local modules
require('./controllers/clients')
Installaed modules
For modules installed via npm install -S foo, use the syntax
require('foo')

Resources