Can't find mongoose module NodeJS require - node.js

for some reason I can't get my require to work, it should just find both models fine but the path for the file just won't work.
app
models
user.js
match.js
server.js
Seems like a simple fix but cannot seem to do it myself right now. I'm using (var User = require('/app/models/user.js');)
Thanks guys.

Actually you also can write it as,
var User = require('./app/models/user')
without even putting .js at the end, as it is added by default.. Just a tip to know :)
and here ./ means the current directory.
Hope it helps :)

Related

How do I get my Nodejs .env file recognized?

Well I started to learn how to create api rest
and when I want to call it in the proccess.env it doesn't recognize it.
I already have dotenv installed
this is how my .env file looks like
this is the error, I have tried with other ports but I get the same error .
my package.json
I also tried following:
require('dotenv').config({path: '../.env'});
require('dotenv').config({path: '../../.env'});
and all I get is this
enter image description here
I hope I made myself understood, I tried to search on yt but found no answer. Thank you for your time
You do not need to write it like that, you can just use it like this
import * as dotenv from 'dotenv'
dotenv.config()
this is possible because the default path is your root folder.
Also, you are supposed to put it in your root folder as it is stated by the creators, you can read it here.
However if you really want to use it in another directory, you can read through solution on this question.

Nodejs export module syntax

Can anyone explain the following 2 lines of code from a node project as I'm very much new to Node.js. I know how to export a function or constant and to import those in node.js but following style I have never seen before.
require = require("esm")(module);
module.exports = require("./src");
These two lines are written in the index.js file in the root folder which seem to be triggering something.
Thanks.

Difficulties connecting to app.js file with node/nodemon

So I've inserted pictures of my vsCode, I'm trying to use node (or nodemon, which never seems to work for me) how ever I get an error message each time. I've even tried moving around my code, any assistance would be greatly appreciated!
Change line 2 of app.js into
const hotel = require('./models/hotel.js/hotel');
The reason you got an error in the console was because the require directory is wrong.

How to delete all txt files within directory NodeJs

I need to delete only the txt files within a directory (multiple levels). I'd hazard a guess that it's possible with fs-extra...
https://github.com/jprichardson/node-fs-extra
But I can't find a solution without specifying the individual file name.
I was hoping to solve it with the following...
fse.remove('./desktop/directory/*.txt')
but unfortunately the asterisk wouldn't select all... as I then could have done something like the following...
fse.remove('./desktop/directory/sub1/*.txt')
fse.remove('./desktop/directory/sub1/sub2/*.txt')
fse.remove('./desktop/directory/sub1/sub2/sub3/*.txt')
fse.remove('./desktop/directory/sub1/sub2/sub3/sub4/*.txt')
Not the cleanest I know... But it's all I've got.
Any help or suggestions on this would be appreciated. Thanks.
If you are using rimraf, You can try this.
const rimraf = require('rimraf');
rimraf.sync('**/*.txt');
rimraf accept glob as the first parameter.
If you want to use it asynchronously, you can even write
rimraf('**/*.txt', options, () => {
console.log('deleted')
})
Though fs-extra uses rimraf internally to delete the file.
What about this?
fse.remove('./desktop/directory/**/*.txt')
Adding ** means to include all sub-directories

learning node and modules / creating a file using

Im really new to node and feel like I understand nothing. I was watching a tutorial where the fs.writeFileSync creates a new file. However my code doesn't work and no file is being created-does someone know why? Also why do I need
var fs=require("fs");
? As I understand fs is a build in module and if we require something, there should be another file that exports something (which we require using the fs module)? Node is kind of hard to understand and would appreciate some explanation! Thanks
var fs=require("fs");
fs.writeFileSync("contents.txt","Thats a new file")
console.log(fs.writeFileSync("contents.txt").toString());
fs is indeed a node built-in module, and as other modules, you must require it to use its capabilities. the file you're referring is present internally, so you don't have to npm install it.
And regarding your code, fs.writeFileSync should work as you used it, however, when you tried to print it, you used this function again, this time with no contents, what probably caused the mix-up.
Code that should work perfectly is:
//Requiring the fs module in order to use it later on
var fs = require('fs');
//Writing "Thats a new file" as text to a new file called "contents.txt" in the same directory as the script file.
fs.writeFileSync('contents.txt', 'Thats a new file');
//If you want to print the file, read it, like so.
console.log(fs.readFileSync('contents.txt'));
Also, I think you should continue reading about node's async capabilities so you can understand better this technology and what is it good for. This is one site you can learn from but there are a lot of other good ones out there.

Resources