Cannot find module "serve-static" | JavaScript | node js - javascript

I want to create a simple server.js file in node.js which will act as a
local server.
I installed the connect module by the following command
npm install -g connect
And I can see the connect module inside node_modules
Then I tried to install serve-static module as follows
npm install -g serve-static
It says packages added but I can't see the module inside node_modules.
And when I try to run the following server.js it throws an exception saying
Cannot find module serve-static
server.js
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("../angularjs"));
app.listen(5000);
Error
Error: Cannot find module 'serve-static'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Program Files\nodejs\server.js:2:16)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)

1- Install them locally
npm install connect -S
npm install serve-static -S
2- OR Link them into your folder
npm link connect
npm link serve-static
3- OR Make sure that if the environment variable NODE_PATH is set correctly

Related

node js module libxml xsd not found

I want to install to module libxml xsd.But when i run the command npm install libxml-xsd nothing happing.When i want to test my module i try to run
var xsd = require('libxml-xsd');
but i have this error :
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'libxml-xsd'
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:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\amine.brahmi\WebstormProjects\modeltestvalidation\test_validation.js:1:11)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789: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)
node-gyp version 3.8.0
node version 10.16.3
npm version 6.9.0
i try to install but i have the same error.
npm install libxmljs
npm install --global --production windows-build-tools
npm install libxml-xsd
node 10.X not compatible with libxml-xsd 0.5.2
you should install other version node 8.x for exemple :
then you need to install nvm to use the two node version from this link :
https://github.com/coreybutler/nvm-windows/releases
and then try nvm list to show all node versions
then install new node version with nvm 8.x 64
Next nvm use 8.x 64 to switch to use 8.x

Node JS Cannot find module 'node-properties-parser' Error while running as normal user in Amazon Linux

I have installed node-properties-parser globally by running npm install -g node-properties-parser as normal user(vaisakh). Then i ran my test.js file with the command node test.js now it's throwing the error
Error: Cannot find module 'node-properties-parser'
at Function.Module._resolveFilename (module.js:476:15)
at Function.Module._load (module.js:424:25)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/data02/bamboo/test.js:3:12)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
I ran the above npm install -g node-properties-parser as root user and run the node test.js here it's working fine. But why it's throwing the error while running as normal user?
Thanks. Finally i found the answer, it's working when in run sudo npm install node-properties-parser without the -g option. But still i don't know why the -g option is not woking, in the docs it's given as install globally.

Why do I keep getting "Cannot find module 'socket.io'"?

I have been trying to use socket.io in my NodeJS script, but I'm constantly getting the error "Cannot find module 'socket.io'".
Full error:
$ sudo node /var/www/apache/server/serverScript.js
module.js:549
throw err;
^
Error: Cannot find module 'socket.io'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/www/apache/server/serverScript.js:59:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
I've tried installing socket.io globally, I've updated everything npm related, and tried all other common suggestions I found online, but the error hasn't changed.
If I had to guess, I think it's related to the package.json file. I started off with NodeJS just recently, and never used a package.json file. I saw somebody mention it as a fix, so I added one through npm init. This placed it in /home/pi. Since this didn't help, I moved the file to the js file location: /var/www/apache/server. Unfortunately, no luck.
Can anyone tell me what's causing this issue?
It's hard to give a good answer without any of your source code. However, you can try do to this.
Open your cmd or terminal.
cd into your project folder.
Run the command: npm i --save socket.io
Within your source code, type const io = require('socket.io'); to import socket.io.
Have you place this code at the bottom of your render document (like index.html for example) ?
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
You can just install socket IO inside your project not globally.
If you are developing in NodeJS, you need to add the package library to your package.json. This can be done by doing:
npm install --save socket.io
Note: If your npm install fails to install socket.io, try to:
Install node-gyp globally: npm install -g node-gyp
Create a .npmrc file with this in the content: #types:registry=https://registry.npmjs.org/
which is going to add this line to your package.json:
"dependencies": {
"debug": "~3.1.0",
...
"socket.io-adapter": "~1.1.0",
"socket.io-client": "2.1.1",
"socket.io-parser": "~3.2.0"
},
Note: the dots are other packages library that may exists in there.
Then, you can use it in your code:
const io = require('socket.io');

fix module.js:328 throw err;

I'm learning nodejs through some tutorials and so far it went smooth. Now I got an error and I'm stuck. Can anyone help me with it.
This is my app.js file
var greet5 = require('/greet5');
greet5.greet();
And this is my greet5.js file which is located in same directory as app.js
var greeting = "Hello from Revieling Module pattern";
function greet(){
console.log(greeting);
}
module.exports = {
greet : greet
}
when I run node app.js on terminal this is the error I'm facing.
module.js:328
throw err;
^
Error: Cannot find module '/greet5'
at Function.Module._resolveFilename (module.js:326:15)
at Function.Module._load (module.js:277:25)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/pankaja/Nodejs/Tests/23-Module Patterns/app.js:19:14)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
Can anyone help me with this. Thanks
The README.md file of the nodejs project, appeared after running nodejs install, explains you how to run the web app through nodejs.
It says you have to execute npm run dev on the command line.
Here you are the default README.md:
# Front End Boilerplate
Front-end boilerplate for creating new web projects with Gulp.js and everything you need to get started.
# Getting Started
Required! Gulp installed `npm install -g gulp`
```
$ cd html-boilerplate
$ npm install
$ npm run dev
```
# Build to production
```
$ npm run build
```
Thank you for your suggestions!

Cannot find module 'node-static'

I am trying to run samples regarding webrtc. For that I went to
https://bitbucket.org/webrtc/codelab/src/50a47bb092483fd7ca27998a365dff434919bf89?at=master
At step 5 I needed to run server.js. For that I opened my Windows Command prompt and entered:
C:\Program Files\nodejs>node D:\GITProjects\codelab\complete\step5\server.js
But I got this error:
module.js:338
throw err;
^ Error: Cannot find module 'node-static'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (D:\GITProjects\codelab\complete\step5\server.js:1:76)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
I have already installed node-static module and it is present at
"C:\Program Files\nodejs\node_modules\node-static"
Still I am getting the error "Cannot find module 'node-static'".
Environmental "PATH" variable is set to "C:\Users\user\AppData\Roaming\npm"
I can see the node-static folder is present at "C:\Users\user\AppData\Roaming\npm\node_modules\node-static" path too.
Edit:
Based on the comments I tried this on Windows Command Prompt to install node-static:
C:\Program Files\nodejs>npm install node-static -g
I got this as the output:
C:\Users\user\AppData\Roaming\npm\static -> C:\Users\user\AppData\Roaming\np
m\node_modules\node-static\bin\cli.js
node-static#0.7.6 C:\Users\user\AppData\Roaming\npm\node_modules\node-static
├── mime#1.3.4
├── colors#1.1.2
└── optimist#0.6.1 (wordwrap#0.0.3, minimist#0.0.10)
Can you help me location the cause of my issue?
node_static is not an inbuilt nodejs module so there should be a folder node_modules in your doc root folder(step5) with this module in it or create a package.json and list it as a dependency then run npm install before node server.js
just run npm install on cd into the "complete" directory i.e codelab\complete

Categories