I'm reading this doc on hapi-auth-cookie and trying to run sample server.here is what i did :
1-putting sample server in server.js
2-npm init
3-node server.js
4-npm install --save hapi
5-node server.js but this time i get a new error
Error: Cannot find module '../'
somewhere in the code it's requiring '../'
server.register(require('../'), (err) => {
if (err) {
throw err;
}
But i don't understand this part really.you can see the full code in the link above.what should i do? thanks
I had the same error. I just deleted the node_modules directory and rerun install.
rm -rf node_modules/
npm install
After that the app runs correct again.
Maybe there isn't a index.js file in directory ../.
Looking at the link you posted, the index.js file of that module is in the /lib (https://github.com/hapijs/hapi-auth-cookie/tree/master/lib) directory and usually you want to import a module by name in the npm construct.
So put your server.js in /lib
You can replace
server.register(require('../'), (err) => {
with
server.register(require('hapi-auth-cookie'), (err) => {
and be sure to run
npm i -S hapi-auth-cookie
and
npm i -S hapi
before you start your server
Related
I am trying to install npm module Fawn into my NodeJS file. I have installed it in my root directory of my project file. It is loaded into my root directory node_module folder.
However when I try to load it into my project file, I am getting the following error: Could not find a declaration file for module ‘fawn’.
const Fawn = require(‘fawn’);
Has anyone had this same issue? Any help is much appreciated!
For the importing JS module in the typescript, you should have the typings file as well. Commonly it's declared in the package or can be installed like this: npm i -D #types/package-name. However, for this module, it's not available and the author isn't planning to add this: https://github.com/e-oj/Fawn/issues/57 . You can create your d.ts file for this library and declare required types there: https://www.ankursheel.com/blog/custom-type-definitions-for-java-script-dependencies
Fawn.init('mongodb://localhost/vidly');
try{
new Fawn.Task()
.update('movies', { _id: movie._id }, {
$inc: { numberInStock: -1 }
})
.save('rentals', rental)
.run()
res.send(rental)
} catch (ex) {
console.log(ex)
}
try to add a database link instead of a mongoose. this worked for me
I am using React Native and Node.js. I want to share code between the two. My folder structure is as so.
myreactnativeapp/
mynodeserver/
myshared/
In the react native and node apps I have included the
package.json
"dpendencies" : {
"myshared": "git+https://myrepository/ugoshared.git"
}
This can then be included in each project via require/import etc. This all works fine and for production I'm happy with it. (Though I'd love to know a better way?)
The issue I'm facing is in development it's really slow.
The steps for a change to populate are:
Make changes in Shared
Commit Changes to git
Update the npm module
In development, I really want the same codebase to be used rather than this long update process. I tried the following:
Adding a symlink in node_models/shared - doesn't work in react-native package mangaer
Using relative paths ../../../shared - doesn't work in react-native package mangaer
Any other ideas?
Update 1
I created a script.sh which I run to copy the files into a local directory before the package manager starts. It's not ideal but at least I only have to restart the packager instead of messing with git etc.
#myreactnativeapp/start.sh
SOURCE=../myshared
MODULE=myshared
rm -rf ./$MODULE
mkdir ./$MODULE
find $SOURCE -maxdepth 1 -name \*.js -exec cp -v {} "./$MODULE/" \;
# create the package.json
echo '{ "name": "'$MODULE'" }' > ./$MODULE/package.json
# start the packager
node node_modules/react-native/local-cli/cli.js start
Then in my package.json I update the script to
"scripts": {
"start": "./start.sh",
},
So, the process is now.
Make a change
Start/Resetart the packager
Automatic:
Script copies all .js files under myshared/ -> myreactnativeapp/myshared/
Script creates a package.json with the name of the module
Because I've added the package.json to the copied files with the name of the module, in my project I can just include the items the same as I would if the module was included via the package manager above. In theory when I switch to using the package in production I wont have to change anything.
Import MyModule from 'myshared/MyModule'
Update 2
My first idea got tiresome restarting the package manager all the time. Instead i created a small node script in the shared directory to watch for changes. Whenever there is a change it copies it to the react native working directory.
var watch = require('node-watch')
var fs = require('fs')
var path = require('path')
let targetPath = '../reactnativeapp/myshared/'
watch('.', { recursive: false, filter: /\.js$/ }, function(evt, name) {
console.log('File changed: '+name+path.basename(__filename))
// don't copy this file
if(path.basename(__filename) === name) {
return
}
console.log(`Copying file: ${name} --> ${targetPath+name}`);
fs.copyFile(name, targetPath+name, err => {
if(err) {
console.log('Error:', err)
return;
}
console.log('Success');
})
});
console.log(`Starting to watch: ${__dirname}. All files to be copied to: ${targetPath}`)
I am trying to concatinate multiple files using gulp and gulp-concat modules of npm.
The following is the code snippet I have used.
gulp.src(['../a.js', '../b.js', '../c.js'])
.pipe(concat('destination.js'))
.pipe(gulp.dest('destination/path/'))
.on('error', () => {
console.error('Error');
})
.on('finish', () => {
console.log('Success');
});
The error event listener is not getting triggered when the file for suppose b.js or anyother is not found in the specified path.
The event finish is getting triggered anyway.
How do find error such as file not found in this process?
Thanks for the help in advance
I finally found a solution for this from the below post
How to make Gulp.src fail if a file is missing?
I Used the files-exist npm package to check if all files exist or not.
You can use gulp-plumber to get error while build.
Example:
Step 1: Install gulp-plumber:
npm i gulp-plumber --save-dev
Step 2: Require the gulp plmber module in gulpfile:
var plumber = require('gulp-plumber');
Step 3: Usage
gulp.src(['../a.js', '../b.js', '../c.js'])
.pipe(plumber())
.pipe(concat('destination.js'))
.pipe(plumber.stop())
.pipe(gulp.dest('dist/js'));
I have file test.txt in my root directory of app. When I run my app with command npm start, I can write to my file without any problem, but when I make package using electron packager, writing text to my file is not possible anymore - I got error
Error: EACCES: permission denied, open './test.txt'
For this, I'm using node.js filesystem:
fs.writeFile("./test.txt",text,function(err){
if(err) {
return alert(err);
}
alert("saved");
});
How is possible to make this working? And is possible to include some extra folder in my app after package process? Thanks for your help!
There are a lot of options to choose to package your electron app in 2019, so in case you come to this question like I did and are using electron-builder, please try my suggestion below.
If you are using electron-builder to package your application and need to read/write a file that is stored within your solution, you can add it to your files property in your package.json. The properties in this file property are files that are copied when packaging your electron app - reference.
In my example, I was reading/writing to file.json.
let fs = require("fs");
fs.writeFile("./file.json", "data to file", "utf-8", (error, data) => {
if (error){
console.error("error: " + error);
}
});
My folder structure looked like this.
parent-folder
app/
assets/
configs/
images/
resources/
...
file.json
My app was not working after I packed it until I added the following file.json in my "build" property in my package.json.
"build": {
"productName": "MyApp",
"appId": "org.dev.MyApp",
"files": [
"app/dist/",
"app/app.html",
"app/main.prod.js",
"app/main.prod.js.map",
"package.json",
"file.json", // << added this line
],
//...
}
Didn't really found out what the problem was, so I tried another solution, which works for me (my main aim was to save data to some local memory of app).
I used npm package electron-store which is really easy to use.
You can get it by typing this to terminal
npm install electron-store
More info about it here: Electron store
Hope it helps someone else too :-)
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