How to identify the running express.js version in code? - node.js

Can I identify the running version of express.js from within code?
For example, I can use process.version to get the Node version, and process.versions for the various dependancies.
Is there a way I can do this for Express?
Thanks

In order to find the current version of your Express JS, open your terminal and type:
npm list express
Another way is to open up your codebase and check the package.json file where you will be able to see all the packages installed along with its version number.

for local package use command npm list
for globally installed packages use command npm list -g
a specific package by passing its name as an argument use command npm list express

For getting the express version you can use
read-package-json
module. This will give the data of package.json and you can read the value of express version from it.
for more details, you can refer here

You can use the package.json file to get the Express version.
const fs = require('fs');
const package = fs.readFileSync('package.json')
const packageParse = JSON.parse(package)
const expressVersion = 'v' + packageParse.dependencies.express.slice(1)
console.info('Using Express.js ' + expressVersion)

Navigate to the project folder
type the command
npm list express
Note: On wrong path of project fodder it will show empty message.

Using npm list express shows the version and if express is install or not in cmd
below is example:-
C:\Users\Aditya Kumar\nodews\modules_node_js\contact_list>npm list express
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
contact#1.0.0 C:\Users\Aditya Kumar\nodews\modules_node_js\contact_list
└── express#4.18.1

You can refer to https://www.npmjs.com/ official website.
If I understand well ?
Express is a module comes from NPM so you have to follow that command and install the latest express framework there !
$ npm install express --save
CQFD welcome !!

Related

how to use #types/node in node application

I am working in VSCode on Ubuntu 16.04. I've created node project using below commads:
npm init
tsc --init
I've created a new file called index.ts. I'm trying to use fs and readling to read file contents. but when I am writing below lines of code at the top of index.d.ts:
import fs = require('fs');
import readline = require('readline');
I'm getting below error:
can not find module 'fs' and can not find module 'readline'
even process is not found.
I've installed typings of node from here using below command:
sudo npm install #types/node -global --save
Can anyone please help me how to resolve this error?
Since TypeScript 2.x, all typings are installed using npm like this: npm install #types/node.
For TypeScript 1.8, it might be better to typings to install the node types. For detail see the quickstart at: https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html.
For what i know you have two options here:
(Recommended) Install devDepencencie npm install #types/node --save-dev, which will add the type module for http.
Create a index.d.ts file declaring a definition for http module, like:
declare module 'http. This method will not enable auto-complete for http methods

Using NodeJS plugins in Electron

I am new to Electron (Atom-shell), and I am trying to load a NodeJS plugin into the application I am building, but I don't know how. The documentation is not clear on that.
For instance, I am trying to use sqlite3 plugin in my app, I used npm install sqlite3, and it was successfully installed. But the application throws and error when I try to call it var sqlite = require('sqlite3'). Are there any further steps I am not aware of ?
Thanks.
For pure JS (i.e. not native) modules you need the following:
Have the module listed in your package.json dependencies
Let electron know where to find the module (e.g. export NODE_PATH=/PATH/TO/node_module)
The first requirement is obvious and the second has its roots in this issue.
For native node modules (such as sqlite3) which use C++ bindings, you need to build them against electron headers to work. According to electron docs, the easiest way to do that would be:
npm install --save-dev electron-rebuild
# Every time you run npm install, run this
./node_modules/.bin/electron-rebuild
To install the npm modules correctly you should go into the folder of your electron app and install the module via npm.
npm install --save sqlite3
The flag --save is important, because npm will install the module inside your app.
Afterwards the require should work.

Globally instaling packages doesn't work (node.js, npm)

For example I installed express with global (-g) parameter. In node.js/node_modules folder express doesn't exists.
I tried to install it without global parameter and it works perfectly.
How to install it globally?
Thanks.
Installing modules via npm with -g is only for modules that provide command-line utilities, not for making any ordinary module accessible from anywhere.
So if you are looking to use the command-line express project generator utility/command, you need to use npm install -g express-generator instead.
If you want to require('express'); in your application, then you need to install express with npm install express.

Error: Cannot find module 'socket.io'

[~]# node node.js
Error: Cannot find module 'socket.io'
[~]# node -v
v0.10.10
socket.io installed:
npm install socket.io
npm WARN package.json policyfile#0.0.4 No repository field.
npm WARN package.json policyfile#0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
Looks like you have installed socket.io in a different location to your current path. Either install globally like below:
npm install -g socket.io
Or reference the location you've installed to:
var io = require('../lib/socket.io');
Thanks ajtrichards!
Just to add to the answer - in case you simple use
sudo npm install socket.io
The installation path will be
/home/.../.npm/socket.io
If you use
sudo npm install -g socket.io
The installation path will be
/usr/local/lib/node_modules/socket.io
In first case, I tried adding the socket.io path in global path variable but it did not work.
you might have installed but not added to the dependencies in package.json
Use below command to install socket.io module
npm install socket.io --save
Hope this will resolve your problem..
I had the same issue with version 0.12.0 on Windows. I tried npm install -g socket.io but that didn't change anything. Also tried npm cache clean also no change, but after npm update npm -g, things got well.
I had this problem with NodeJs, Typescript and Socket.io 4. the error was:
TS2792: Cannot find module 'socket.io'.
So my fix was to update the tsconfig.json adding a new property (moduleResolution).
tsconfig.json:
{
"compilerOptions": {
....
"moduleResolution": "node",
}
}
This almost happens than you try to get socket.io in you html files like :
index.html
where you have:
< script type="text/javascript" src="/socket.io/socket.io.js"></script>
It will not find socket.io because you did not started module in you application file wich contain the server like
server.js
You must include following lines after started your server in server.js :
var io = require('socket.io').listen(server);
Hope, will save time.
I think that you have executed the command npm install socket.io
in a different location and your files are in different directory..
So either run the command in the same directory which have your files or either mention the path where you have currently installed socket.io in your PATH variable.
I had the same problem in mac... you can install the module with sudo npm install socket.io-client

Configuring a Node JS App to Use NPM

I have a really simple Node JS app and I'd like to include the Express JS framework. I've installed Express with NPM (and NPM with Homebrew) without any errors using:
brew install npm
npm install express
And my server.js file contains only:
var express = require('express');
When I run my application I get Error: Cannot find module 'express'. How can I tell my Node application to include the library?
You need to tell node where your libs are.
extract from http://nodejs.org/api.html
require.paths
An array of search paths for require(). This array can be modified to add custom paths.
Example: add a new path to the beginning of the search list
require.paths.unshift('/usr/local/node');
Here's a walkthrough of using npm's bundle command:
http://intridea.com/2010/8/24/using-npm-with-heroku-node-js?blog=company

Resources