Node doesn't find local packages - node.js

I'm learning node. I've initialized a project with npm. I installed express, and everything looks alright.
npm list outputs this:
package.json looks like this:
And the project is structured like this:
with index.js just being the following:
const expressDep = require('express');
const app = express();
But when I try to execute index.js, node outputs the following:
What am I doing wrong ?
To my understanding, node just doesn't see any definition of express, but npm tells me everything is installed correctly. What's going on ?

How can the compiler know what is this express()?
Require is a built-in function to include external modules that exist in separate files.
If you want to use this app function you must use the variable you assigned the module. Like this:
const express = require("express");
const app = express();
or
const expressDep = require("express");
const app = expressDep();

Related

Pass command line argument from bin/www to app.js and then to module

I have looked at the solution here and while it is the right solution to passing an argument to a module, my requirement is to pass command like arguments from bin/www to app.js to a module.
This is a project created in WebStorm, which has bin/www as the startup script.
I have a traditional NodeJS project structure where the app starts at bin/www which call app.js and app.js has the router modules.
The startup script is bin/www which I cannot change and I want to pass an argument (path to a config file) to a router module.
What is the best way to achieve this without significantly changing the file structure?
app.js has the following structure:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index')(path_config_file);
var usersRouter = require('./routes/users');
var app = express();
module.exports = app;
As I have shown I want to pass path_config_file to routes/index module, which will be passed as a command line argument.
I cannot start app.js because the project has existing CI/CD and I am not able to change that process.
I need to be able to pass the argument as node bin/www path_config_file and get it over to app.js.
Thank you.
Basically, what you need is to read the process.argv var where those command line arguments are saved. Please look at the answers on the next post:
How to pass command line arguments on node

Nodejs swagger integration - Express Router

I am aware of configuring swagger to my NodeJs project using JSON / YAML. Is there any way to automate the documentation like swagger doing for spring webservices.
Note: I am using Express Router to create endpoints.
Yes, the package swagger-autogen performs the automatic construction of the Swagger documentation.
First, install the package with: npm i swagger-autogen
If you have a endpoint/route file like this:
routes.js
const express = require('express')
const ToolsController = require('./controllers/ToolsController')
const router = express.Router()
const apiV1 = require('./controllers/ApiRoute1')
router.use('/v1', apiV1)
module.exports = router
Just declare a file called swagger.js, for example, with this:
swagger.js
const swaggerAutogen = require('swagger-autogen')()
swaggerAutogen('./swagger-output.json', ['./routes.js']);
In the package.json, add a script such as:
"scripts": {
"swagger-autogen": "node ./swagger.js"
}
And run: npm run swagger-autogen
The Swagger documentation will be in the swagger-output.json.
To add descriptions, tags, summary, and so on, access the https://www.npmjs.com/package/swagger-autogen#endpoints
Examples: Links to projects that cover the simplest use of this module as well as the most complete use. See the links below:
Example using Router
Example without Router
yes, you could use swagger-ui-express or other npm libraries =)
You can use jsdoc. For this you need to install npm packages
npm i swagger-jsdoc
npm I swagger-ui-express
Please refer
node js swagger with express server

json-server 0.8.12 and Express 4.x Routing

I have an existing node-express project running on express using express server and have middlewares and routing on express
var express = require('express');
var app = express();
require('/path/to/express_conf_file')(app);
app.listen(config.port);
I want to use json-server for easy mocking and I've followed this. My current code looks like this:
var express = require('express');
var app = require('json-server');
var middlewares = express();
var server = app.create(); // Returns an Express server
**var router = server.router('db.json');** // Returns an Express router
server.use(middlewares);
server.use(router);
require('./config/express')(middlewares, config);
server.listen(4000);
server.route('db.json') seems to be deprecated in Express 4.x. What needs to be done to use 'db.json' with express 4.x?
I am invoking my application and json-server using concurrenrly using npm start and in package.json I've defined:
"js-server": "json-server --watch db.json --port 4000",
"start": "concurrently \"gulp command\" \"npm run js-server\""
Can somebody please advice as what should be the correct way of using json-server with Express 4.x?
It is solved here. json-server can be mounted at a certain path within express server. No need to start the json-server separately now. Only gulp command can be used to launch express server.
The json server can be hosted separately also and can be accessed through the specified port from different appilication(s).

How to set up an Express 4.0 application

I downloaded express today from npm, and to my surprise, it gave me express 4.0.0 instead of express 3.x.x.
I'm trying to configure a basic server with connect logger and body parser, but I'm not having much luck.
Could someone provide me with a boilerplate app.js file using express 4.0?
Thanks!
Got it!
You can get a skeleton app using:
$ npm install -g express-generator
Alternatively, you can swap out the connect logger and body parser for their connect standalone siblings (and it might be useful for learning what each middleware does, rather than relying on the generator to throw together a bunch of dependencies you may not need):
(based on Express 3.x to 4.x Migration guide)
var express = require('express');
var server = express();
...
server.use(require('body-parser')); //previously bodyparser (or rather urlencoded/json)
server.use(require('morgan')()); //previously connect-logger
...
server.listen('3000', function() {
console.log('server started');
});

Running console commands through node inspector?

I have connected node inspector to my running node.js program. But I'd like to issue commands through the console - not necessarily on a breakpoint. For example, I have global variables and functions in my server.js program. For example:
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
Now in node-inspector I go into the console and I type 'test()' and it returns "ReferenceError: test is not defined", same things with global variables I type 'app' and it tells me it's not defined. Any idea how to make this thing work? I just want to run my node program and then issue commands to it via a command line interface.
#foreyez,
Your question inspired me to make an npm module that lets you debug (via command line) a running node.js application without setting breakpoints or needing to make variables global:
https://github.com/go-oleg/debug-live
Hope that helps!
The reason why it doesn't work as you expected is that all your variables and functions are local to your module (application) and you can't access them from a global context.
You can save them in the global scope if you want to access them from anywhere (including from the console when not stopped on a breakpoint):
var express = require('express');
var app = express();
function test() {
console.log('yo');
}
app.listen(3000);
global.test = test;

Resources