node.js: cannot find module 'request' - linux

I installed request module, and getting the error:
module.js:340
throw err;
^
Error: Cannot find module 'request'
i've read all the posts about this error, and understand that this is because module requests is not globally found, but i've already tried the 2 suggestions
npm install request -g
should this install it in /usr/loca/bin ? because i don't see it there.
and
sudo npm link
/usr/local/lib/node_modules/request -> /Users/soulsonic/dev/sandbox/node_test/request
i restarted terminal after each command, but keep getting the cannot find module error.
update
there must have been some sort of conflict in my initial directory, because "npm install request" was not adding "request" under node_modules (there 10 others in there) ..
after switching to a new directory it just worked.
if i run it with -g switch, i do see it bing installed to /usr/local/lib/node_modules/request.
it seems that i just need to update my profile so that above path is automatically added.

Go to directory of your project
mkdir TestProject
cd TestProject
Make this directory a root of your project (this will create a default package.json file)
npm init --yes
Install required npm module and save it as a project dependency (it will appear in package.json)
npm install request --save
Create a test.js file in project directory with code from package example
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Print the google web page.
}
});
Your project directory should look like this
TestProject/
- node_modules/
- package.json
- test.js
Now just run node inside your project directory
node test.js

You should simply install request locally within your project.
Just cd to the folder containing your js file and run
npm install request

I had same problem, for me npm install request --save solved the problem. Hope it helps.

I have met the same problem as I install it globally, then I try to install it locally, and it work.

if some module you cant find, try with Static URI, for example:
var Mustache = require("/media/fabio/Datos/Express/2_required_a_module/node_modules/mustache/mustache.js");
This example, run on Ubuntu Gnome 16.04 of 64 bits, node -v: v4.2.6, npm: 3.5.2
Refer to: Blog of Ben Nadel

I tried installing the module locally with version and it worked!!
npm install request#^2.*
Thanks.

I was running into the same problem, here is how I got it working..
open terminal:
mkdir testExpress
cd testExpress
npm install request
or
sudo npm install -g request // If you would like to globally install.
now don't use
node app.js or node test.js, you will run into this problem doing so. You can also print the problem that is being cause by using this command.. "node -p app.js"
The above command to start nodeJs has been deprecated. Instead use
npm start
You should see this..
testExpress#0.0.0 start /Users/{username}/testExpress
node ./bin/www
Open your web browser and check for localhost:3000
You should see Express install (Welcome to Express)

ReferenceError: Can't find variable: require.
You have installed "npm", you can run as normal the script to a "localhost" "127.0.0.1".
When you use the http.clientRequest() with "options" in a "npm" you need to install "RequireJS" inside of the module.
A module is any file or directory in the node_modules directory that can be loaded by the Node.
Install "RequiereJS" for to make work the http.clientRequest(options).

Related

BrowserSync: command not found after installing locally

I ran the following command for my node app:
$ npm install browser-sync --save-dev
Installation was successful, browser-sync appears in my package.json file as well as my node_modules directory.
However, when I run $ browser-sync --version to check that it's working, I get the following error:
bash: browser-sync: command not found
Why isn't this working?
Note: this question is similar, but I don't want to have to install it globally as in this question.
Any help would be greatly appreciated!
This is because you're trying to use a module locally which is normally installed globally. Modules installed globally end up on your PATH environment variable, which is why you can run them from the terminal as you're trying to do:
$ browser-sync --version
If you want to use the browser-sync module from a local install you will have to prepend the full path to the browser-sync binary from within your .bin directory since all locally installed modules are placed within your current working directory node_modules directory. i.e. Node modules go in ./node_modules, executables go in ./node_modules/.bin/. So in order to run the browser-sync binary from a local install do the following:
./node_modules/.bin/browser-sync --version
Hopefully that helps!
If you installed browser-sync using npm --save or npm --save-dev you can run it by writing a script in your package.json. Here's an example of a script I added:
{
...
"scripts": {
"dev-server": "browser-sync start --server 'public' --files 'public'"
},
...
}
You can run the scripts from you project's root directory like so
npm run dev-server
This will run whatever command is set to dev-server in your script. In this case it will run browser-sync for the app/site in a folder called /public and watch for any file changes in the /public folder. I know this question is a bit old but it was unanswered and hopefully I can save someone time in the future.
The other answers still work, but a newer approach has emerged since npm added the npx command: npx <package-name>.
This command allows you to run an arbitrary command from an npm
package (either one installed locally, or fetched remotely), in a
similar context as running it via npm run.
Source: https://docs.npmjs.com/cli/v8/commands/npx
In this case, you would run npx browser-sync.

Node js - how to publish a package and let user run a single command to start the server

I have made a npm package for the first time. For my daily dev, I have used npm start to start the server and then user can use localhost:3000 to view the pages.
When I try to publish that to end users, I can still ask people to use npm install > npm start to use the package. However, it doesn't sound decent.
How can I make people being able to use:
npm install myPackage
myPackage start
to start the server and then they can open localhost: 3000 directly?
You can make a global module and attach it to a bin command.
Here's the tutorial for that by npm itself.
In your package.json file, you can add this:
"bin": {
"your-command": "bin/path/to/js/file.js"
}
and then whenever the user installs your module globally, then they can directly type in that command and the js file will execute your code.
You need to create global package for your module so that it can be installed
npm i -g module
then provide some config file like gulpfile.js which will be used by your package to carry out the task to start server any other task

Node http-server not working on Ubuntu linux

I am trying to run a simple http server in my project directory. All I need is GET request support, so I can GET html/css/js/etc.
For that I wanted to use http-server from npm.
I installed it with npm install http-server -g
Now I cd to my project folder where it has the index.html file, I open the terminal and run http-server
But when I open my browser at http://localhost:8080/index.html - it can't connect to the host.
Am I missing something?
Okay, the issue was - I had another package installed on Ubuntu, which is also called node
Node JS package is called nodejs on my system and I think that http-server is looking specifically for 'node'.
In order to work around this:
I removed the node package with sudo apt-get remove node and created a symlink for nodejs:
sudo ln -s /usr/bin/nodejs /usr/local/bin/node
First install npm.
Second npm install http-server -g.Next append after the http-server url template url like http-server C:\xampp\htdocs\

npm package error - Cannot find module 'users-node'

I installed the users-node package on my Windows7 machine using npm install users-node - great success!
Code:
...
var usermgr = require('users-node')(options, app);
...
But when I run my node server I get:
Error: Cannot find moduel 'users-node'
I implemented the code suggested on this page: https://npmjs.org/package/users-node but I could not download the repository from GitHub - link is broken: https://github.com/dpweb/users-node.
What do I need to do to implement users-node? I need to do users management, so do simple user login on a chat app using Node.js, Socket.io and express.
Are you sure that you didn't install it with -g option?
If so you need to set NODE_PATH environment variable.
The default path for Windows is:
%AppData%\Roaming\npm\node_modules
In case you didn't install it globally, you must place your app.js file beside the node_modules directory which is created after you run npm install users-node

'Express' is not recognized command (windows)

Okay I am running node on windows (7). Using npm I just installed modules to d:\ directory. Therefore my files structure looks like the following:
D:\
-myproject
-node_modules
-.bin
-express
However, when I am in this 'myproject' directory, I can't seem to run 'express' for example:
D:\myproject\express site
'express' is not recognized as an internal or external command, operable program or batch file.
Am I doing anything wrong?
Try:
npm install -g express-generator#3
That solved problem for me.
Edit: for version 4
npm install express-generator -g
Description:
express is the package for dependency of express js.
express-generator is the package for enabeling express command and create a sample project, etc.
Assuming that this is kept separate for the decoupling of project dependency with cli tool of express.
Another SO ref: https://stackoverflow.com/a/41311733/1666582
Here's what to type in the command line to make it work in windows:
npm install express-generator -g
[Source: http://expressjs.com/starter/generator.html]
My guess is that you didn't install Express globally. You can install express globally (and therefore available in your PATH) with the following command (see http://expressjs.com/guide.html) :
npm install -g express
The way you install it is available only in the folder that you installed it and there is nothing wrong with that approach. There is very little advantage of having it available globally.
If express is not in your PATH you can run it by entering the full path to it:
\myproject\node_modules\.bin\express.cmd
With the release of Express 4.0.0 it looks like you need to do
npm install -g express-generator
We need to set path for express global directory
C:\Users[User_Name]\AppData\Roaming\npm\
After add a new path, please reopen the CMD console
Tried all of these and never worked. A repair of Node.js by kicking of installation and selecting repair option does the magic.
Cheers
What worked for me was:
I used the windows command prompt instead of the node.js command prompt.
In windows 10 simply type in the windows search bar for "node"
You see a node.js desktop app and a node js command prompt.
Choose the "node js command prompt"
Type in the command prompt
npm install express-generator -g
Then navigate somewhere and type in:
express your-website-text-here -e
A directory with express files will be generated. Also now you won't see the express error.
I was able to fix with the following package install:
npm install express-generator -g
Thanks
When you install Node.js, the below path is added to the Windows OS %Path% variable, I'm presuming similar happens on other operating systems as well:
C:\Users\<your-windows-username>\AppData\Roaming\npm
In my case, because I use a work Windows laptop for an employer that severely restricts what employees can do on their machines (I.e. many actions require elevated admin privileges), Node.js was being prevented from adding the above path to the Windows %Path% environment variable, and much to my chagrin the Node.js installation was silent about it. Navigate to above folder and you will notice the express command lives there, see screenshot below.
How did I figure this out? I did a fresh installation of Node.js on a personal, home Windows machine that has no admin privilege restrictions like my work machine does, compared the before and after %Path% value, and noticed the addition of that path. My work machine was missing it.
I had no choice but to add the path manually to %Path%, and then express was recognized from within any path I ran it.
I was able to fix this with:
npm install express-generator -g
I tried all the above solution, no luck for me.
Open "Node.js" command prompt and tried as administrator. It is working fine for me. Don't try with windows command prompt.
I have the same problem and understand the solution, but i can´t undestand why, running npm install -g express, express.bat isn´t added automatically to Path.
Running with npm install -g express have the same result. Download packages and store in node_modules, but express.bat isn´t added to path.
Run the node command prompt as administrator and then install express globaly
npm install -g express
and then go to folder where you want to install express generator, open command prompt there and run this command
npm install express-generator, it will then fix the issue
I have tried out all above solutions, but its did't worked for me, finally I have re-installed the node.js with newer version and started to express install process again. Its worked for me.
npm install -g express
npm install -g express-generator
What command are you using to open the directory?
That error means CMD can't find the "express" executable in the current directory.
Use the "PUSHD" command or "CD /D" instead of "CD"
#Echo OFF
PUSHD "D:\myproject\express" || (Echo bad folder)
express.exe "site"
Pause&Exit
Express is loaded someplace else and not in the windows path environment variable. You need to find were express.exe is installed and add the path. Maybe something like ;"C:\Program Files\Express\bin";
Running "npm install express" and "npm install express-generator" from your project directory will resolve the issue (if that helps).
But, this doesn't solve the problem of being global.
You might check the permissions to the folder if you are getting this when creating your project
Express Project
change script section in package.json file like this
"scripts": {
"start": "node app.js"
}
I too faced the similar problem and at last I tried using node.js command prompt instead of windows command prompt and it worked. So, try from node.js command prompt.
Tried all of these but didn't work for me. Also, I tried from different sources but never worked for me.
In the end, found that I need to run the command forcefully. It worked for me.
Make sure you run the command with Run as Administrator.
npm install -g --force express-generator
It will overwrite the existing express files.
Use npm start .. then the app.js runs .. which can be listened on the usual port 3000

Resources