Node.js "Error: Can not find module" - javascript

I am doing the node.js lesson from lynda.com and am unable to find what is causing the "Error: Can not find module". From everything I can tell the package.json has the flight module in it.
Everything has been working so far so I know node.js is installed and running correctly. The way I am running this file is by typing "node app.js" at the command line. Also all of the files are in the same folder.
Here is the console log:
C:\Users\Jonathan\Desktop\flight>node app.js
module.js:340
throw err;
^
Error: Cannot find module './flight'
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\Jonathan\Desktop\flight\app.js:1:76)
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)
C:\Users\Jonathan\Desktop\flight>
Here is the code.
Package.json file
{
"name": "flight",
"version": "1.0.0",
"description": "a module for keeping track of a flight",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "JBaxter",
"license": "ISC"
}
index.js file
var number, origin, destination;
exports.setNumber = function (num){
number = num;
};
exports.setOrigin = function (o){
origin = o;
};
exports.setDestination = function (d){
destination = d;
};
exports.getInfo = function() {
return {
number: number,
origin: origin,
destination: destination
};
};
app.js file
var flight = require('./flight');
flight.setOrgin('LAX');
flight.setDestination('DCA');
flight.setNumber(462);
console.log(flight.getInfo());
Any help with be great. I thought the name within the package.json file was your module but maybe I am incorrect. Thanks in advance.

"./flight" does not evaluate to index.js in your case.
If you create a subdir :
flight
- index.js
then you can require("./flight") and have it evaluated to "./flight/index.js".
In your case, simplest thing you could to do :
require("./index.js");

you can do it in the following way:
var flight = require('./index.js');
OR
var flight = require('./index');
Following code will not work because the node will look for module named 'flight' in node_modules folder
var flight = require('flight');

Related

Problem with Node.JS and jsconfig.json with absolute imports

IDE: Replit
Good afternoon, thanks for checking out.
I wanted to implement absolute imports to my node project, but something odd is happening whenever I try to run the code.
Here are the things I set for my jsconfig.json file:
{
"compilerOptions": {
"baseUrl": "src"
}
}
Now, this is how i'm calling the file I need inside another one:
const {title} = require('util.js')
Oddly, replit's intellisense shows up the right path:
Thumbs Up From Replit
But then, when I finally execute the code, I get this error message:
Error: Cannot find module '/home/runner/Moniqueta-Discordjs/node_modules/util.js/index.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (node:internal/modules/cjs/loader:353:19)
at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/home/runner/Moniqueta-Discordjs/src/commands/rps.js:2:17)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) {
code: 'MODULE_NOT_FOUND',
path: '/home/runner/Moniqueta-Discordjs/node_modules/util.js/package.json',
requestPath: 'util.js'
}
I tried following the error by adding a "valid main entry to my package.json" file, like this:
{
"name": "moniqueta-discordjs",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"keywords": [],
"author": "sum117",
"license": "ISC",
"dependencies": {
"#replit/database": "^2.0.2",
"discord.js": "^13.8.0",
"src": "^1.1.2",
"util.js": "^0.0.0"
}
}
And when I checked it, it had "util.js" as a declared dependency, I really don't understand what's going on.
Thanks for reading. What am I missing? I really would love this to work...
If my explanation is not enough, you can access my open source code via the link.

anchor test command error - Error: Cannot find module '[#project-serum/anchor] ' Please verify that the package.json has a valid "main" entry

I was following an introduction tutorial using anchor (solana framework), it is based in the official github.
In the testing part, I used anchor test command and I got this error:
BPF SDK: /home/sebastian/.local/share/solana/install/releases/1.9.12/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
Finished release [optimized] target(s) in 3.86s
cargo-build-bpf child: /home/sebastian/.local/share/solana/install/releases/1.9.12/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/target/deploy/basic_1.so
To deploy this program:
$ solana program deploy /home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/target/deploy/basic_1.so
The program address will default to this keypair (override with --program-id):
/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/target/deploy/basic_1-keypair.json
yarn run v1.22.17
$ /home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/.bin/mocha -t 1000000 tests/
Error: Cannot find module '/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/#project-serum/anchor/dist/cjs/index.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (node:internal/modules/cjs/loader:353:19)
at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/basic-1/tests/basic-1.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
at async formattedImport (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
at async Object.exports.requireOrImport (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
at async Object.exports.loadFilesAsync (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/nodejs/esm-utils.js:88:20)
at async singleRun (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/cli/run-helpers.js:125:3)
at async Object.exports.handler (/home/sebastian/Documentos/solana-project/anchor/examples/tutorial/node_modules/mocha/lib/cli/run.js:374:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
This is my package.json:
{
"name": "basic-1",
"version": "0.24.2",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://github.com/project-serum/anchor#readme",
"bugs": {
"url": "https://github.com/project-serum/anchor/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/project-serum/anchor.git"
},
"engines": {
"node": ">=11"
},
"scripts": {
"test": "anchor test --skip-lint"
},
"dependencies": {
"#project-serum/anchor": "^0.24.2"
}
}
I really appreciate any information about it. I also attach the tutorial and anchor's github link.
https://github.com/project-serum/anchor
https://youtu.be/CmG5_sIas1Q
Try re-installing #project-serum/anchor
yarn add #project-serum/anchor
In your anchor.toml file, try changing the value of 'test' to mocha tests/test.js , replace test.js with whatever the name of your file in tests.
Go check node_modules/#project-serum/anchor if you don't have a "dist" folder there you will have to run yarn add#project-serum/anchor again. When you can see that the "dist" folder was properly installed you'll be able to run your tests again.

Error: Cannot find module 'config' at Function.Module._resolveFilename

Hii I have simple node server, with the following structure
myapp
-config
-default-json
-index.js
-package-lock.json
-package.json
Here is my part of my index.js
'use strict';
const
config = require('config'),
express = require('express'),
request = require('request'),
body_parser = require('body-parser'),
app = express().use(body_parser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));
when I run node index.js I get the following error
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module 'config'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\xampp\htdocs\chat\index.js:13:14)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
what is wrong with my code?
I found solution by by installing config from npm
https://www.npmjs.com/package/config
follow the instruction above and it should work , it might help some one in future
You have to explicitly add the config module in your package.json:
"dependencies": {
"config": "version number"
}
https://www.npmjs.com/package/config
It means there is no config.js in your current location. Put the exact location of the config.js file..
Try,
config = require('./config/config'),

Electron Cannot find module /resources/app/index.js

I'm new to electron and have made my first electron application, it runs normally running npm start but when using npm run I am unable to execute my application. (I am running ubuntu linux) The cli gives this error message when running the executable file:
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module '/home/user/Documents/dev/cocoa/cocoa-linux-x64/resources/app/index.js'
at Module._resolveFilename (module.js:455:15)
at Function.Module._resolveFilename (/home/user/Documents/dev/cocoa/cocoa-linux-x64/resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:403:25)
at Object.<anonymous> (/home/user/Documents/dev/cocoa/cocoa-linux-x64/resources/electron.asar/browser/init.js:171:8)
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)
at Module.runMain (module.js:590:10)
This is my package.json :
{
"name": "cocoa",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "electron main.js",
"package-linux" : "electron-packager . --platform=linux --arch=x64 --electron-version=1.4.13"
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron-packager": "^8.7.1"
}
}
this is the main.js file :
const electron = require('electron')
const {app, BrowserWindow} = electron
app.on('ready', ()=> {
let win = new BrowserWindow({
width:400,
height:400
})
win.loadURL('http://google.com')
})
Does anyone know what I am doing wrong, thank you for your help.
Ok, that was really stupid. I had named the main js file for the project main.js when in actual fact it had to be named index.js
problem solved!

not able to require publish module

Im was publish module to npm (very simple module for testing purpose) and it seems that I was not able to require it.
when I do npm install --save I saw that the package is located inside the node_modules (and new entry was created in the package.json) folder and in my server.js file I do
var myModule = require('nodewrapapp');
and I got the following error:
Error: Cannot find module 'nodewrapapp'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:289:25)
at Module.require (module.js:366:17)
The module contain only one very simple file :
function startInterval(fn) {
fn(); // do the function right now
return setInterval.apply(this, arguments); // defer to setInterval
}
module.exports = startInterval; // let me be required
what am I doing wrong here?
Change your main to server.js
{
"name": "nodewrapapp",
"version": "0.0.1",
"description": "test",
"main": "server.js", // Here
...
}

Categories

Resources