Can't run script of MongoDB Atlas URL using node to database - node.js

I'm currently following along with a MDN tutorial Express Local Library Part 3. I've done everything correctly, that I know of, so far. I made a database then a cluster with MongoDB Atlas and am now trying to upload my data from a file populatedb.js by command node populatedb <my MongoDB Atlas URL here> but it doesn't work at all. I've linked it to the apps.js file with the correct URL (or so I think).
I'm getting this returned in the console:
___________-MacBook-Air express-locallibrary-tutorial % node populatedb 'mongodb+srv://_________:_______________#cluster0.mhzo6.mongodb.net/local_library?retryWrites=true&w=majority'
internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/________/Desktop/express-locallibrary-tutorial/populatedb'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I've even deleted my database, cluster, organization and everything and remade them exactly the same but still to no avail.

Here is the answer:
[for those of you stumped on this tutorial this will most likely be the cause!]
The tutorial made it very easy to misunderstand and create a secondary folder of the same name as the root in the root...[or perhaps and more likely its that we are still beginners and its very easy to misunderstand and make this simple mistake.] In creating the initial express project https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website at tutorial 2 "creating the project" I had accidentally made the simple mistake of creating my own express-locallibrary-tutorial folder/project in VS code then ran all of the cmds from the integrated terminal instead of the external terminal. This is what caused the double folder.
I was easily able to work around this just by making sure I was in the correct folder when running node/nodemon and made sure to path correctly for all routes just to "cover up" the mistake which I believed was no big deal... This caused me to have to trash my entire project. I had actually finished this tutorial but once it came to launching it to Heroku I quickly realized that it wasn't worth it to attempt to fix everything and best to restart it from the beginning because Heroku can only run scripts from the /root/ as it should [at least so far since this beginner/noob has been troubleshooting that is!]
So watch out for that simple mistake all! If you did it too and caught it early it is definitely worth the headache of restarting because it will help you in the long run and is a good lesson of a simple mistake, or it is for me!!

Related

Node JS (cpanel) Error: I'm getting an error [ERR_REQUIRE_ESM]: Must use import to load ES Module

So, I've spent quite a few hours today trying to put my nodeJS app that's fully using ESM (modules), and I've deployed it via cPanel on a server that's using Node v. 14.20.1. I'm constantly getting an error:
App 1153856 output: internal/modules/cjs/loader.js:948
App 1153856 output: throw new ERR_REQUIRE_ESM(filename);
App 1153856 output: ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /<serverlocation>/app.js
App 1153856 output: at new NodeError (internal/errors.js:322:7)
App 1153856 output: at Module.load (internal/modules/cjs/loader.js:948:11)
App 1153856 output: at Function.Module._load (internal/modules/cjs/loader.js:790:12)
App 1153856 output: at Module.require (internal/modules/cjs/loader.js:974:19)
App 1153856 output: at Module.require (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:80:25)
All files are written as modules, I don't have one "require()" anywhere.
Since the Node started fully supporting JS modules from v14 on, I'm taking a wild guess that the hosting server I'm using (and their Passenger for NodeJS) is using a loader that's using "require()" when calling my app.js file.
I've tried multiple solutions, I've even switched my app.js file to a CommonJS type, but then it required me to switch all other files to CJS as well, which would be too much hassle.
Has anyone managed to find a proper solution to this issue?
For anyone trying to solve this issue, this is how I solved it:
1- Create a loader script: Not necessarily in the same folder as the main app.js file of your app, but that's where I created it. Call it something like loader.cjs. The extension being .cjs is important if you have "type": "module" in your package.json.
As you might've guessed, this will be the new main of your app. Since the passenger's loaders have an issue with ES modules, just let it load a commonjs file instead.
2- Dynamic import of app.js: Did you know that you can still load ES modules in commonjs files? You just need a little extra bit to do so.
Apparently, ES modules are loaded asynchronously, and this doesn't work well with synchronous commonjs files. That's why you got the issue in the first place, right?
Therefore, the solution is: dynamic imports. Just like async functions, treat the imports of ES modules as promises. I don't really like using .then(), so I opted with await:
async function loadApp() {
await import("/path/to/app.js");
}
loadApp();
3- Rename your app.js's extension: I don't exactly know why this is necessary, but I got error along the lines of "couldn't find /path/to/app.mjs" and so I changed it to so. Then it worked. You can keep the name of the file as "app.js" in the path in the previous point, and the import will sstill correctly look for "app.mjs";
There might be more efficient ways to do this, but that's what my 2 bits of brain could come up with. Hopefully it helps others as well.

Firebase Can't Deploy

I have a firebase project I've been maintaining for months and haven't had any problems with.
I've tried running firebase deploy multiple times to no avail. I've googled it and searched on SO, github, and others, found these links, none of which worked. I've tried updating firebase tools, uninstalling and reinstalling, and everything between. Please don't blindly flag as duplicate without reading.
The first line of my error looks like this:
\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\lib\winston\logger.js:307
throw ex;
Here's my full output:
firebase deploy
C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\lib\winston\logger.js:307
throw ex;
^
Error: write after end
at writeAfterEnd (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_writable.js:261:12)
at PassThrough.Writable.write (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_writable.js:305:21)
at File.log (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\lib\winston\transports\file.js:185:34)
at File._write (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston-transport\index.js:103:17)
at doWrite (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\readable-stream\lib_stream_writable.js:428:64)
at writeOrBuffer (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\readable-stream\lib_stream_writable.js:417:5)
at File.Writable.write (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\readable-stream\lib_stream_writable.js:334:11)
at DerivedLogger.ondata (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_readable.js:681:20)
at DerivedLogger.emit (events.js:203:15)
at DerivedLogger.EventEmitter.emit (domain.js:448:20)
at addChunk (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_readable.js:298:12)
at readableAddChunk (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_readable.js:280:11)
at DerivedLogger.Readable.push (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_readable.js:241:10)
at DerivedLogger.Transform.push (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_transform.js:139:32)
at DerivedLogger._transform (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\lib\winston\logger.js:305:12)
at DerivedLogger.Transform._read (C:\Users\justi\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\winston\node_modules\readable-stream\lib_stream_transform.js:177:10)
I'm completely stuck on this, I've been searching for an hour, and I've never had this fail for me before, so I don't know what's going on with firebase. Thanks in advance.
For others that come this way, I had this exact error message and stack trace. I was attempting to run firebase deploy --except functions while following this code lab step.
For me the fix was simply to stop serving the firebase app locally for debug.
Once I had done this, no reboot, just back in the other terminal and submit the deploy command and it worked fine.
The code lab gets you to do this and then leave it served locally while changing various parts of the app, but I have no idea what caused winston to conflict this way.
This probably an issue with NPM try running npm cache clean and reinstall firebase tools. If that didn't work try contacting firebase support. Recently I ran into an issue with firebase hosting and they were really helpful but took about 30 hours for their response after making the support ticket.
I had the exact same error, I tried restarting Command Prompt, deleting the cache file in the .firebase project folder. deploy worked fine with a separate test project.
On a whim I shut down my local server, and firebase deploy worked fine. Not sure if that's the real fix, as I'm 99.9% sure I did deploys in the past with the local server running... but it works for me now.
And after deploy was working again, it stopped a second time with the same error. So I stopped the server, and once again deploy worked. Then if failed a third time, same fix, shut down the local server.
Local server was run via firebase serve --only hosting
npm cache verify also checked out A-OK:
https://docs.npmjs.com/cli-commands/cache.html

nestjs to azure windows app service - web.config settings

I've been trying to deploy my nestjs api to azure since yesterday and have not been successful. At first I was trying to push it to a linux cloud service plan and was able to deploy it from vs code, see in the azure logs that the app was successfully started, but it would not return any data from my endpoints and after some digging, I kept seeing some container failures. I tried troubleshooting that for a while then decided I would try setting it up on a windows app service instead.
Following this guide: https://learn.microsoft.com/en-us/azure/app-service/app-service-web-get-started-nodejs, I pushed my nestjs app up to an azure app service on windows and again, it did not work. I pulled down the repo in that article to compare it to mine and they have the entry point in the web.config for the nodejs app pointing to index.js. That index.js doesn't exist in my nestjs app and I did some research and found out that the entry point for a nestjs app after everything is compiled is main.js. There is another difference in that sample project and my nestjs app in that the nestjs app's main.js file appears to be in a dist folder, rather than at the root of the wwwroot path.
My nestjs app obviously didn't contain a web.config out of the box so I created one using the hello world repo's as an example. I updated the iisnode path to dist/main.js and the rewrite path as well to the same path/file since that's the only thing I can think to do.
The only other wildcard is the node version number that I'm changing in azure for the WEBSITE_NODE_DEFAULT_VERSION application setting...I'm not sure what that should be for my nestjs app. My current local version is 12.16.2 but when I change the version to that on azure, it doesn't work.
I'm not sure what else to try here - I'd be fine trying to go back to linux if I could sort out the docker container issue but that seemed like a dead end. I think I'll likely have more luck continuing down the windows route if I can just get this web.config / version thing to work.
Is main.js in the dist folder where I should be pointing my web.config? Is there a max version limit on azure? Is there anything else I might be missing?
EDIT: After turning on streaming logs - I'm now seeing this exception
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:434:25)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object. (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
after some googling, I've found that exception was common with older versions of node (< 4). I'm well beyond that version and have azure set to a version well beyond that as well.
I got it sorted out with the help of a friend. Running this in the azure cli got things sorted out using the linux setup.
az webapp config set --resource-group myResourceGroup --name myAppServiceName --startup-file "npm run start:prod"

Meteor crashing after update - A route for the path already exists by the name

Hi SO community I have this application and after I did the upgrade to the most recent version of meteor (1.3) the application fails uppon start.
/home/ec2-user/.meteor/packages/meteor-tool/.1.3.0_3.fc5itk++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:267
throw(ex);
^
Error: A route for the path "/oauth/authorize" already exists by the name of "oauth.authorize".
at Iron.utils.assert (packages/iron_core.js:66:11)
at Function.Router.route (packages/iron_router.js:745:3)
at meteorInstall.server.native-routes.coffee (server/native-routes.coffee:1:8)
at fileEvaluate (packages/modules-runtime.js:158:9)
at require (packages/modules-runtime.js:92:16)
at /home/ec2-user/oauth/.meteor/local/build/programs/server/app/app.js:1002:1
at /home/ec2-user/oauth/.meteor/local/build/programs/server/boot.js:283:10
at Array.forEach (native)
at Function._.each._.forEach (/home/ec2-user/.meteor/packages/meteor-tool/.1.3.0_3.fc5itk++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
at /home/ec2-user/oauth/.meteor/local/build/programs/server/boot.js:133:5
the output shows this error like 4 times (it looks like meteor is trying to run 5 times the same route files)
I tried to give a name to all routes, but doesn't work
I hope I can receive any help
Have a nice day

Node.js - cannot find module

I'm using Node Boilerplate and it all worked fine until I decided create another project on top of it(in another dir).
Now I have exactly the same code base(this project AS IS) in two different folders. I can run one of it without any problems but another one is failing with:
% node app.js
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'connect/middleware/router'
at Function._resolveFilename (module.js:299:11)
at Function._load (module.js:245:25)
at require (module.js:327:19)
at Object.<anonymous> (/home/gotts/Sites/nodejs-uploadr/lib/express/lib/express/server.js:17:14)
at Module._compile (module.js:383:26)
at Object..js (module.js:389:10)
at Module.load (module.js:315:31)
at Function._load (module.js:276:12)
at require (module.js:327:19)
at Object.<anonymous> (/home/gotts/Sites/nodejs-uploadr/lib/express/lib/express/index.js:28:31)
There is similar problem explained here - https://github.com/visionmedia/express/issues/535 which says that the problem is in the incompatible version of connect/express.
But how is this possible? Source code is exactly the same in two folders and it work fine in one copy and fails in another?
Do the two folders share a common parent? Node looks for modules in ~/.node_modules and /node_modules in your app dir.
OK, let me answer my own question:
Sorry for misinformation - those two folders were not exactly the same(I'm gonna need to learn unix diff better). And there it require("connect") in code
Local checkout of connect into /lib folder takes precedence but if it's not there - connect will be loaded from the ~/.node_modules as Ricardo previously set.
#see node.js express module not loading properly in the last comment, maybe it helps
the basic idea behind it is to install the module inside your application folder, not on a system level

Resources