I installed connect module using NPM running the following command:
npm install connect
it created the module in /Download/usr/node_modules/connect folder. I created a file which
uses connect module using
var connect = require('connect');
var util = require('util');
function sendjson(res,obj)
{
res.writeHead(200,{'Content-Type':'application/json',});
var objstr = JSON.stringify(obj);
util.debug('SENDJSON' + objstr);
res.end(objstr);
}
var server = connect.createServer(
connect.router(function(app){
app.get('/foo', function(req,res){
sendjson(res,{path:'foo'});
})
app.get('/bar', function(req,res){
sendjson(res,{path:'bar'});
})
})
);
server.listen(3000);
I run node createServer.js and it throws in the terminal and it gives me the following error.
Cannot find module 'connect'
NPM modules by default need to be installed locally in the folder that contains the source file that uses them. So if your source file is in /Desktop/nodescripts, you should run "npm install connect" in that same folder. That will create node_modules folder in that path, and your script will be able to find it.
Related
I am attempting to get a lighthouse script running in Node.JS (which I am new to). I followed the intial instructions here https://github.com/GoogleChrome/lighthouse/blob/master/docs/readme.md#using-programmatically. I was able to complete the prior steps in the package manager console (Visual Studio 2017):
npm install -g lighthouse
lighthouse https://airhorner.com/
//and
lighthouse https://airhorner.com/ --output=json --output-path=./report/test1.json
However, I do get an initial warning that NPM only supports Node.JS in versions 4 through 8 and recommends a newer version. The problem is I am running Node v12 and NPM v5 - both the latest.
When I create a script version like below (app.js)
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const config = {
extends: 'lighthouse:default',
settings: {
emulatedFormFactor: 'desktop',
onlyCategories: 'performance',
output: 'json',
outputPath: './report.json'
}
};
function launchChromeAndRunLighthouse(url, opts = null, config) {
return chromeLauncher.launch().then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
return chrome.kill().then(() => results.lhr);
});
});
}
// Usage:
launchChromeAndRunLighthouse('https://airhorner.com/', config).then(results => {
// Use results!
});
And run the command
C:\src\project> node app.js
I get the error - Cannot find module 'lighthouse'
don't install lighthouse locally use it inside your working dir .
first start by running npm init that will create the package.json file inside the current working dir
then npm install --save lighthouse will download it and save it to node_modules now you can use it locally inside your working dir
it should look something like this
app.js
package.json
node_modules/
then run node app.js
I have installed meteor library.
I want to run an angular project which has no back-end (of static content).
I want to create a server file using node.js for static content.
Is it possible to to create that and execute ?
There is a very simple example of how to create a static server in Node.js that server static content pages,
the following code is in the myserver.js file:
var http = require('http');
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serve = serveStatic("./");
var server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
server.listen(8000)
You need to install through NPM from command line:
$ npm install finalhandler serve-static
$ node myserver.js
It's possible with Meteor, but it's an overkill.
remove the default mongo package ($ meteor remove mongo)
put all your static files into a folder called public
You're done. This way your production build won't require a MongoDB server.
But it's a lot easier to just use the http-server NPM package to setup a static file server with Node.js.
I am trying to do one of the node-horseman examples, but I am finding with a problem. The example I am trying to follow is this:
var Horseman = require('node-horseman');
var horseman = new Horseman();
var numLinks = horseman
.open('http://www.google.com')
.type('input[name="q"]', 'github')
.click("button:contains('Google Search')")
.waitForNextPage()
.count("li.g");
console.log("Number of links: " + numLinks);
horseman.close();
And the errors it throws when I make phantomjs example.js are these:
Error: Cannot find module 'http'
phantomjs://bootstrap.js:299 in require
phantomjs://bootstrap.js:263 in require
:3
Error: Cannot find module 'tty'
phantomjs://bootstrap.js:299 in require
phantomjs://bootstrap.js:263 in require
:6
TypeError: Object is not a constructor (evaluating 'require('debug')('horseman')')
:5
TypeError: Object is not a constructor (evaluating 'new Horseman()')
phant.js:2 in global code
I try to install http locally using npm install http, but after this, there is only a package.json on example/node_modules/http, and if I use npm install in this location, it throws three warnings:
it is too the name of a core module
no description
no repository field
About tty, making a local installation it throws a 404 error.
I try this solution (include npm folder on the path) Nodejs Cannot find module but it didn´t work.
Any suggestion??
Thanks.
EDIT
NOT SOLUTION
I reinstall node (now my version is node 0.12.3, npm 2.9.1, and phantomjs 1.9.8), when I try this simple example from the nodejs web:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
And if I run "node example.js" it works, but if I do "phantomjs example.js" the problem persists "Cannot find module http".
I tried install phantomjs via npm ("npm install -g phantomjs") and via downloading the zip on their web, unzipping and adding to the PATH the route to unzipped folder.
One more data (maybe could be a help) my SO is Windows 8.1.
RE-EDIT
I am watching that on the folder where I have installed node, the only folder on node_modules is npm, is it right?? And on C:\Users\Eloy\AppData\Roaming I have two npm folders, one of them is npm-cache and the other one simply npm. The node_modules of this last one doesn´t contain the http module, the npm-cache has a lot of modules and http incluiding... is it important??
Thanks.
+1 your observation in first EDIT i.e scripts run like $ phantomjs can't access global modules via require(), it can access local modules though. Not sure if this is a documented shortcoming of phantomjs
I'm using a common node package 'csv' to do CSV parsing. It works great on my local mac, but not on heroku. In the "heroku log", I get Cannot find module 'csv'.
Yes, I have it in my package json file:
{
"name":"rimes",
"version":"0.0.1",
"dependencies":{
"sys":"",
"url":"",
"http":"",
"querystring":"",
"oauth":"0.9.10",
"fs":"",
"csv":"0.3.0",
"request":"",
"node-cache":"",
"underscore":""
}
}
and I require it in my app.js
var sys = require('sys'),
http = require('http'),
url = require('url'),
qs = require('querystring'),
OAuth= require('oauth').OAuth,
fs = require('fs'),
csv = require('csv'),
myreq = require('request'),
NodeCache = require('node-cache'),
us = require('underscore');
What can I do to fix this on heroku?
Thank you,
~Todd
So when you require packages on node, you also need to install them in your node_modules folder. They must be in the node_modules folder as well as "required" in your code. They can only be declared in your "package.json."
If you go into your project directory (the file where package.json is located) and run:
npm install
it should install your missing dependencies into your 'node_modules' folder.
Then if you re-deploy your app to heroku it should work.
I am trying to upload a file into my hosting server using node and easy-ftp.
I try with the following code:
var EasyFtp = require ("easy-ftp");
var ftp = new EasyFtp();
var config = {
host:'homexxxxx.1and1-data.host',
type:'SFTP',
port:'22',
username:'u90xxxx',
password:"mypass"
};
ftp.connect(config);
ftp.upload("/test/test.txt", "/test.txt", function(err){
if (err) throw err;
ftp.close();
});
No error message but no file uploaded
I tried the same using promises
const EasyFTP = require('easy-ftp-extra')
const ftp = new EasyFTP()
const config = {
host:'homexxxxx.1and1-data.host',
type:'SFTP',
port:'22',
username:'u90xxxx',
password:"mypass"
};
ftp.connect(config);
ftp.upload('/test.txt', '/test.txt')
.then(console.log)
.catch(console.error)
ftp.upload()
The same issued. No file is uploaded. No error in node console.
The config is the same used in filezilla to transfer files. SFTP protocol. Everything working well with filezilla.
What I am doing wrong?
Looks like you may have a path problem over here.
"/test/test.txt"
The path specified will try to take file from root folder like "c:\test\test.txt".
Assuming you want the file to be taken from your project folder try this path:
"./test/test.txt"
Other things in your code are precisely the same as in mine and mine works.
For me, it was just silently failing, and intelli-sense was not available.
npm remove easy-ftp
npm install easy-ftp
npm audit fix --force until no more vulnerabilities
Afterwards, intelli-sense was available and it started working.