Node not loading module from project root - javascript

I'm beginning some learning projects in node and am having problems with node finding installed modules.
I installed a module into the root directory of a project, which looks like the following:
C:\Users\Dean\Documents\NodeProjects\ReadWords
which has my node_modules folder and readwords.js file I would be running to call the module.
Within node_modules is censorify-deanmau5 folder and in there, my various module files.
readwords.js has on line 1
var censorify = require('censorify-deanmau5');
and in the console when I try to run the file, I get the following:
C:\Users\Dean\Documents\NodeProjects\ReadWords>node readwords.js
module.js:340
throw err;
^
Error: Cannot find module 'censorify-deanmau5'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (C:\Users\Dean\Documents\NodeProjects\ReadWords\readwo
rds.js:1:79)
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)
Surely this should be working with having node_modules in the same directory as the .js file I'm trying to run? Am I missing something here?

Your package seems to have invalid main entry point. Either rename censorify.js as index.js or add "main": "./censorify.js" to package.json.

Related

Node js: Error: Cannot find module 'mime-types/node_modules/mime-db'

I followed a tutorial to create a CRUD App with Nodejs, I finished the project and everything worked fine. After I tried to move all the files and folders to a new folderand I destroyed everything.
Now when I try to run node app.js the following happen:
enter code herinternal/modules/cjs/loader.js:797
throw err;
^
Error: Cannot find module 'mime-types/node_modules/mime-db'
Require stack:
- C:\Users\Costis\Documents\Code\Vue\Mongodb_app\server\node_modules\mime-types\index.js
- C:\Users\Costis\Documents\Code\Vue\Mongodb_app\server\node_modules\accepts\index.js
- C:\Users\Costis\Documents\Code\Vue\Mongodb_app\server\node_modules\express\lib\request.js
- C:\Users\Costis\Documents\Code\Vue\Mongodb_app\server\node_modules\express\lib\express.js
- C:\Users\Costis\Documents\Code\Vue\Mongodb_app\server\node_modules\express\index.js
- C:\Users\Costis\Documents\Code\Vue\Mongodb_app\server\app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
at Function.Module._load (internal/modules/cjs/loader.js:687:27)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\Users\Costis\Documents\Code\Vue\Mongodb_app\server\node_modules\mime-types\index.js:15:10)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\Costis\\Documents\\Code\\Vue\\Mongodb_app\\server\\node_modules\\mime-types\\index.js',
'C:\\Users\\Costis\\Documents\\Code\\Vue\\Mongodb_app\\server\\node_modules\\accepts\\index.js',
'C:\\Users\\Costis\\Documents\\Code\\Vue\\Mongodb_app\\server\\node_modules\\express\\lib\\request.js',
'C:\\Users\\Costis\\Documents\\Code\\Vue\\Mongodb_app\\server\\node_modules\\express\\lib\\express.js',
'C:\\Users\\Costis\\Documents\\Code\\Vue\\Mongodb_app\\server\\node_modules\\express\\index.js',
'C:\\Users\\Costis\\Documents\\Code\\Vue\\Mongodb_app\\server\\app.js'
]e
Any suggestion?
You probably have to make a clean install your dependencies again with npm install, as you might have broken some symlinks (just assuming as I'm not a Windows user).
You can manually delete your node_modules folder and run npm install, or if you have an existing package-lock.json, use npm ci to install exactly the same versions of deps you had before.

Get a error missing modules on the terminal

I want to make a new project running npm install -g create-react-app on the folder directory. After doing this I get the following error:
module.js:487
throw err;
^
Error: Cannot find module '../package.json'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at /usr/local/lib/node_modules/npm/bin/npm-cli.js:30:15
at Object.<anonymous> (/usr/local/lib/node_modules/npm/bin/npm-cli.js:92:3)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
Your question is not very precise. You should give more details about what you are doing.
Still, here are some suggestions. Make sure:
You run the command from within the folder, where your package.json is located.
If you have not created a package.json yet, run npm init
Your package.json is a valid format and readable

How to resolve 'cannot find module 'serialport' using Node.js and Johnny-Five?

Main goal: using Johnny-five and node.js to run a script for my Arduino, using LeapMotion Controls
I have a folder with my
1: 'robotarm.js' script
2.the Leapjs 'lib' folder
the 'node_modules' folder in leapjs which has underscore, glmatrix and ws.
I am have uploaded the Standard Firmata on the Arduino Board and I am trying to run my Script to start the program. However when I go to terminal, get inside my proj folder and type node robotarm.js, this is what happens:
Kriss-MacBook-Pro:armproj Kris$ node robotarm.js
module.js:340
throw err;
^
Error: Cannot find module 'serialport'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Board.Serial.detect (/Users/Kris/node_modules/johnny-five/lib/board.js:69:7)
at new Board (/Users/Kris/node_modules/johnny-five/lib/board.js:299:23)
at Object.<anonymous> (/Users/Kris/Desktop/armproj/robotarm.js:90:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Just npm install (not globally) did the trick for me. I didn't even have to delete my node_module folder.
Just add:
var board = new five.Board({ port: "COM3" });

node.js and node-postgres syntax error

I just installed node-postgres by running npm install pg .
In my source code, I added var pg = require('pg");
and when I run I now get
module.js:340
throw err;
^
Error: Cannot find module 'pg'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/rrazavipour-lp/Documents/workspace/Astute/app.js:9:10)
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 Module.runMain [as _onTimeout] (module.js:497:10)
What step am I missing?
Where is the node_modules folder in relation to your application?
from http://nodejs.org/api/modules.html:
If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then node starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.

Running UglifyJS under node.js generates cannot find module error

Running node.js v0.10.15 on windows 7 - installed VIA node-v0.10.15-x86.msi.
Downloaded latest UglifyJS2 library.
Created batch file with syntax:
node "path\to\uglifyjs\bin\uglifyjs" -o "file.min.js" "file.js"
Getting error:
module.js:340
throw err;
Error: Cannot find module 'source-map'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (D:\Projektid\Tools\uglifyjs\tools\node.js:9:21)
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 Module.require (module.js:364:17)
What could be the problem?
It seems that you've used node instead of uglify in the command line. Since uglifyjs is its own CLI, you don't need to call on node. I believe you need to have this installed globally to work as intended.
npm install uglify-js -g
then
uglifyjs "path\to\uglifyjs\bin\uglifyjs" -o "file.min.js" "file.js"
should work.

Categories

Resources