Error: Cannot find module 'C:\server.js' in Node server for Meanjs - node.js

I have git cloned meanjs into a folder in D: drive as shown below and ran the command npm start to start the node server.
admin#admin-PC MINGW32 /d/Udemy Angular 1 projects/Project5/jobfinder (master)
$ npm start
> meanjs#0.5.0 start D:\Udemy Angular 1 projects\Project5\jobfinder
> gulp
[23:41:30] Using gulpfile D:\Udemy Angular 1 projects\Project5\jobfinder\gulpfile.js
....and got the below error.
Error: Cannot find module 'C:\server.js'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
[23:58:35] [nodemon] app crashed - waiting for file changes before starting...
After that, just to give it a try I copied the server.js from the current directory to C:\ and issued 'npm start' command at git bash from the same directory and the error changed to below.
Debugger listening on [::]:5858
module.js:457
throw err;
^
Error: Cannot find module './config/lib/app'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\server.js:6:11)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
[23:41:52] [nodemon] app crashed - waiting for file changes before starting...
Can anyone please help me understand why is Node looking for server.js and other resources at C:\ instead of the current directory? Is there anything very simple I am missing out (though I find it quite strange !). Btw sorry for posting the git bash errors as code since I did not have enough reputations to post more than one image link.
Cheers!
Adding my research ....
Invoking 'npm start' command in meanjs directory hits the file gulpfile.js. Looking into the file, I saw that a gulp task (nodemon) was initiated as below and the script property of the return object was set to 'server.js'.
Code from gulpfile.js.
gulp.task('nodemon', function () {
var nodeVersions = process.versions;
var debugArgument = '--debug';
switch (nodeVersions.node.substr(0, 1)) {
case '4':
case '5':
case '6':
debugArgument = '--debug';
break;
case '7':
debugArgument = '--inspect';
break;
}
console.log('nodemon task'); // checking execution of the callback
return plugins.nodemon({
script: 'server.js',
nodeArgs: [debugArgument],
ext: 'js,html',
verbose: true,
watch: _.union(defaultAssets.server.views, defaultAssets.server.allJS, defaultAssets.server.config)
});
});

The above problem was solved by deleting the Autorun string in the registry at the below path.
"HKEY_CURRENT_USER\Software\Microsoft\Command Processor".
The autorun string was set to C: and as a result Nodemon recognised C: as the root instead of the current directory in Gitbash or command prompt.

Related

Unable to --watch directory with node-dev

I am trying to watch only specific directories while starting node-dev because of the issue with multiple watches that causes;
Error: watch /usr/src/app/node_modules/common-tags/lib/stripIndent/stripIndent.js ENOSPC
I tried passing the watch flag but, the process is not able to find the module;
node-dev --watch api app.js
It throws the below error:
Error: Cannot find module 'api' from '/usr/src/app'
at resolveSync (/usr/src/app/node_modules/resolve/lib/sync.js:89:15)
at module.exports (/usr/src/app/node_modules/node-dev/lib/resolve-main.js:4:10)
at getConfig (/usr/src/app/node_modules/node-dev/lib/cfg.js:30:16)
at module.exports (/usr/src/app/node_modules/node-dev/lib/cli.js:46:14)
at Object.<anonymous> (/usr/src/app/node_modules/node-dev/bin/node-dev:11:5)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
The api directory is present in /usr/src/app. I tried using the same watch flag with nodemon and it seems to work but it has its own set of issues and crashes when a file change occurs.
I tried putting the configuration in .node-dev.json, but there has been no impact.
{
"watch": ["api"],
"ignore": [
"*.test.js",
"assets/*",
"coverage/*",
"docs/*",
"email_templates/*",
"kubernetes/*",
"logs/*",
"node_modules/*",
"queue/*",
"script/*",
"shared/*",
"tasks/*",
"test/*",
"tests/*",
"uitests/*",
"views/*"
]
}
Please note that I cannot modify fs.inotify.max_user_watches as the VM is shared and I don't have permission to run system commands.
Any help would be highly appreciated.

how can i fix this error that nodemon keep on showing

i following an express.js tutorial andd after some code and functionalities with api requests i followed exactly how he changed the code that handles these requests into a seperate js file in another folder just to clean up the code but after i did that this error showed up
i tried changing the file name , i tried removing the node-modules folder and running npm install again and i tried installing the module with the issue
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '/routes/api/members'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\Get_Rico\Desktop\crashcoursse\index.js:11:25)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[nodemon] app crashed - waiting for file changes before starting...
i expect nodemon to save the changes and work just fine with no errors just like what happened in the tutorial
It appears the error is probably coming from this line of code and it's telling you that it can't find the '/routes/api/members' module file.
And, this would not be working because you're trying to load a local module without a proper path name. If no path is specified, then it tries to load a global or built-in module. If that module is in the current directory, then you should use the correct path:
const yourModule = './routes/api/members';

Adding gulp-sass breaks by task runner

I have a basic gulp setup in VS2017 to minify my Javascript. I decided to add gulp-sass (my package.json says I'm on gulp-sass v4.0.1) but it throws this error:
C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
let sassMap;
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:11:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
My gulpfile looks like this:
var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var uglify = require('gulp-uglify');
var sass = require('gulp-sass');
gulp.task('minify', function () {
gulp.src('src/**/*.js')
.pipe(uglify({ mangle: false }))
.pipe(concat('scripts.min.js'))
.pipe(gulp.dest('Content'));
});
gulp.task('sass', function () {
gulp.src('src/css/**/*.scss')
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(gulp.dest('Content'));
});
gulp.task('watch', function () {
gulp.watch('src/**/*.js', ['minify']);
});
I did some Googling and a simple fix suggested was to add "use strict" to the top of the offending file, in this case index.js:66. However, after doing that I get:
Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15
throw new Error(errors.missingBinary());
^
Error: Missing binding C:\Work\MyProject\MyProject\node_modules\node-sass\vendor\win32-x64-47\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 5.x
Found bindings for the following environments:
- Windows 64-bit with Node.js 6.x
This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass --force` to build the binding for your current environment.
at module.exports (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15:13)
at Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\index.js:14:35)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:163:21)
at Module._compile (module.js:397:26)
I am running Node.js v6. I'm lost as to why what should be a simple process is giving me these errors. What am I doing wrong?
Update:
I ran the following commands suggested in the comments:
npm install node-sass -f
npm rebuild nose-sass
Both ran successfully. However, I'm still getting this error:
Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
let sassMap;
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:8:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
Update 2:
I was advised to add "use strict"; to the top of my gulpfile.js, but the same error occurs. Here's the file contents:
"use strict";
var gulp = require('gulp');
var sass = require('gulp-sass'); // If I comment this out, I can build
gulp.task('sass', function () {
gulp.src('src/css/**/*.scss')
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(gulp.dest('Content'));
});
Most common issue online appears to be Node.js version of < 6.0, but I'm running v6.11.1.
Update 3: (solved)
I finally found the cause & solution; I've added it as an answer down below for any future readers. Enjoy.
Managed to find the problem so I'm answering my own question for future readers.
Whilst I have node.js v6.11.1 installed, Visual Studio 2017 comes bundled with it's own version of node that it uses by default. Even if you run node -v in the VS2017 shell and it tells you it's running v6.11.1, it's actually - by default - running whatever it finds in .\node_mobules\.bin.
The solution is this:
In VS2017, go "Tools > Options > Projects and Solutions > Web Package Management > External Web Tools".
You'll probably see this:
Add the path to your standalone installation of node (default C:\Program Files\nodejs) and, using the arrows, position it above the .\node_modules\bin version, like this:
Hit OK and either refresh the Task Runner Explorer or restart VS2017. Your gulpfile should now build.
In my case, I had to move $(PATH) above $(VSINSTALLDIR)/Web/External to fix the problem.

Why isn't bin/www command not working in my cloud9 (c9) new run configuration?

I'm following along with a Lynda course on MEAN Stack development and while they are running bin/www command in a "New Run Configuration" and its yielding "debugger listening on port 15454"
whereas for me, when I enter bin/www, I get the following error:
debugger listening on port 15454
module.js:340
throw err;
^
Error: Cannot find module '/home/ubuntu/workspace/bin/www'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.runMain [as _onTimeout] (module.js:497:10)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
When you clone a empty repo from BitBucket it creates a workspace folder. Move the contents of that folder to the root of your cloud9 project.
OR
start from scratch and make sure to run "express . --hbs" in the correct folder
Run
**#**:~/workspace (master) $ express . --hbs
Not
**#**:~/workspace/workspace (master) $ express . --hbs

Node JS app on OpenShift gives error 503

So I created a NodeJS app for a college project which monitors page loading speed. In order to record said speed I need people to go to the web application, go to a certain page and hit a button a couple of times.
So I looked up a way to host this app, free of charge, and came up with OpenShift.
After figuring out how it all worked I managed to setup an OpenShift git-repository and I am able to push my changes to the server.
However upon visiting my page I kept getting error 503. Initially I thought it was a node modules error so I found npm shrinkwrap which solved npm version issues. The error, is still around.
When I run the app locally, I get no errors whatsoever and all modules are installed correctly. They are also in a package.json file (I used npm install --save for all my modules, so no manual editing was performed).
These are the first couple of lines from using the tail-command and whopping the result into a text file.
==> app-root/logs/nodejs.log <==
at Connection.connect (/var/lib/openshift/550db5624382ecf8f400000f/app>root/runtime/repo/node_modules/mysql/lib/Connection.js:109:18)
at Object.<anonymous> (/var/lib/openshift/550db5624382ecf8f400000f/approot/runtime/repo/server.js:28:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
DEBUG: Program node server.js exited with code 8
DEBUG: Starting child process with 'node server.js'
I also found this:
==> app-root/logs/nodejs.log <==
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
--------------------
at Protocol._enqueue (/var/lib/openshift/550db5624382ecf8f400000f/app-root/runtime/repo/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/var/lib/openshift/550db5624382ecf8f400000f/app-root/runtime/repo/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/var/lib/openshift/550db5624382ecf8f400000f/app-root/runtime/repo/node_modules/mysql/lib/Connection.js:109:18)
at Object.<anonymous> (/var/lib/openshift/550db5624382ecf8f400000f/app-root/runtime/repo/server.js:28:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
DEBUG: Program node server.js exited with code 8
Any idea's?
Mine did the same when I first pushed to OpenShift. I think I had to review some references and make some slight changes. My log on OpenShift indicated that such-and-such file couldn't be found. Yours might, too, if you review the entire thing.
For example, this is the "after" version of something I edited:
self.app.set('views', path.join(__dirname, 'views'));
I might have had just this before:
self.app.set('views', '/views');
Hope that helps.
Well... you've got...
Error: connect ECONNREFUSED
...which suggests that Express couldn't connect to the database. Maybe you've got dev versus prod credentials going on and the code's trying to use the local credentials, the local database name, the local username, the local server, etc.
I'm using MongoDB (no credentials locally) and have created a /db.js file to store all that. And then my /server.js selects which variable:
if (typeof process.env.OPENSHIFT_NODEJS_IP === "undefined") { mongoose.connect(dbConfig.devurl); } else { mongoose.connect(dbConfig.produrl); }
Note the two different variables after dbConfig.varname.

Resources