Cannot find module app.js? - node.js

I am starting node app.js in the server directory, and I get the following:
module.js:544
throw err;
^
Error: Cannot find module 'server/server/app.js'
at Function.Module._resolveFilename (module.js:542:15)
at Function.Module._load (module.js:472:25)
at Function.Module.runMain (module.js:682:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:613:3
[nodemon] app crashed - waiting for file changes before starting...
How to fix this? It was working before, and now it's looking for a weird path /server/server instead of just /server.
I just reinstalled node to a more up-to-date version, from 5. to latest stable.

Most likely you try to load your module like so:
require('server/server/app.js')
Node.js then tries to find it somewhere below node_modules. But I suppose, the file is not a dependency but part of your own code. Normally, you would then require it like so (consider the point at the beginning):
require('./server/server/app.js')
This makes Node.js look for the file beginning at the current directory ('.').
This would work in an environment with the following folder structure:
/myfiles/main.js <-- this file contains the `require` statement
/myfiles/server/
/myfiles/server/server/
/myfiles/server/server/app.js <-- this file is being `require`d

I encountered with this issue because I deleted build folder and then try to build the project.
Solution - in my case
I run the tsc --watch that compiled all .ts files to .js

maybe you want to access file app.js,
require('./server/server/app.js')
// to be like this
require('server/server/app.js')
use can using this package
npm i app-module-path

Related

Can I use a custom node_modules path with create-react-app

I'm working on a project inside a VM (really docker on windows via vscode remote-containers). I would like to avoid building my react app at $(pwd)/node_modules, because everything under $(pwd) is synced to my windows file system which causes serious performance issues.
I'm fairly new to nodejs in general, but I'm surprised how difficult this seems to figure out.
I successfully started with this:
yarn --modules-folder /tmp/vendor
This gave me a /tmp/vendor folder with expected dependencies listed.
For the next step, I want to run the development server, so I run this:
yarn --modules-folder /tmp/vendor start
I get /bin/sh: react-scripts: command not found. I figure yarns --modules-folder doesn't reconfigure PATH information when running commands (yarn start calls react-scripts start). So I just added my own PATH as a workaround:
PATH=$PATH:/tmp/vendor/.bin/ yarn --modules-folder /tmp/vendor start
But now I get this:
[root#352b76226b83 owio]# PATH=$PATH:/tmp/vendor/.bin/ yarn --modules-folder /tmp/vendor start
yarn run v1.22.5
$ react-scripts start
internal/modules/cjs/loader.js:965
throw err;
^
Error: Cannot find module 'react-dev-utils/crossSpawn'
Require stack:
- /tmp/vendor/react-scripts/bin/react-scripts.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:962:15)
at Function.Module._load (internal/modules/cjs/loader.js:838:27)
at Module.require (internal/modules/cjs/loader.js:1022:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/tmp/vendor/react-scripts/bin/react-scripts.js:18:15)
at Module._compile (internal/modules/cjs/loader.js:1118:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1138:10)
at Module.load (internal/modules/cjs/loader.js:982:32)
at Function.Module._load (internal/modules/cjs/loader.js:875:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/tmp/vendor/react-scripts/bin/react-scripts.js' ]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command
Since it is complaining about missing modules, I also set the NODE_PATH (I guess yarn doesn't forward this variable either):
NODE_PATH=/tmp/vendor PATH=$PATH:/tmp/vendor/.bin/ yarn --modules-folder /tmp/vendor start
With this, I get:
./src/index.jsx
Line 0: Parsing error: Cannot find module 'eslint-scope' from '/tmp/vendor/eslint/lib/api.js'
I'm stuck here, because eslint-scope does exist, and I've already what I can to work around path problems.
I'm half tempted to start trying other tools outside of CRA and yarn but I don't really know the ecosystem well enough yet to understand how to port my application over. Any advice would be greatly appreciated.
EDIT:
I've gotten closer by changing vendor to node_modules. I guess this convention is somehow necessary. I also added NODE_PATH=/tmp/node_modules to a .env file, and the --modules-folder /tmp/node_modules to a .yarnrc file.
EDIT 2:
I made a symlink for $(pwd)/node_modules -> /tmp/node_modules as suggested and this seems to have worked this time around, given that I added the NODE_PATH to .env.
It seems all the react-script stuff is working now, but when compiling the local source files under src/components/, I get errors about not resolving modules from /tmp/node_modules that do indeed exist.
After some tinkering and support from Estus, I found that having the following things works for me:
.env file with NODE_PATH=/tmp/node_modules
.yarnrc file with --modules-folder /tmp/node_modules
symlink for $(pwd)/node_modules -> /tmp/node_modules
I'm not sure why the symlink is necessary, and would love to know if anyone has a .env like solution. It seems my local source files are compiled against this folder regardless of having NODE_PATH set.
Other oddities I don't understand, but maybe someone will:
I could not just have NODE_PATH as a variable in my yarn command. It seems having the value in .env changed how modules within node_modules were compiled.
I could not use a name other than node_modules. No idea why.
As an aside:
For anyone landing here having the same issue (slow docker performance on windows or mac), you may need to make sure you're editor is not indexing node_modules. For example, I have set files.watcherExclude in my .vscode/settings.json to include "**/node_modules": true,".

How to resolve Node.js: "Error: ENOENT: no such file or directory"

I have a Node.js web application currently running on a server successfully. Now I'm trying to set up a local copy on my development server.
I currently have Node.js, NPM and MongoDB Installed just like what I have in production server. However, the error below occurs when I try to start the Node.js server.
What could be causing this issue?
cd ~/node/nodeapp
node app.js
Output:
fs.js:640
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open '/home/embah/node/nodeapp/config/c
onfig.json'
at Error (native)
at Object.fs.openSync (fs.js:640:18)
at Object.fs.readFileSync (fs.js:508:33)
at Object.<anonymous> (/home/embah/node/nodeapp/config/config.js:4:28)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/embah/node/glorby/app.js:13:16)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
Your app is expecting to find a file at /home/embah/node/nodeapp/config/config.json but that file does not exist (which is what ENOENT means). So you either need to create the expected directory structure or else configure your application such that it looks in the correct directory for config.json.
After going through so many links and threads and getting frustrated over and over again, I went to the basics and boom! it helped. I simply did:
npm install
I don't know, but it might help someone :)
Regarding:
92% additional asset processing scripts-webpack-plugin× 「wdm」: Error: ENOENT: no such file or directory, open....
If anyone faced to such an error, you should do followings:
you should check if the file path is correct in the angular.json file.
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.js"
],
you should press Ctrl + C and rerun the project.
olleh's answer worked, because npm install will create a node_modules directory in the current path where it is executed. So, while using the file server system module, the below declaration locate files from the top level directory of node_modules.
const fs = require('fs')
In my case the issue was caused by using a file path starting at the directory where the script was executing rather than at the root of the project.
My directory stucture was like this:
projectfolder/
├── package.json
├── scriptFolder/
│ ├── myScript.js
And I was calling fs.createReadStream('users.csv') instead of the correct fs.createReadStream('scriptFolder/users.csv')
If you have the same error while using Express.js in your project this worked for me:
In my project, when I ran an Express.js application, I noticed that the current working directory was the root directory of the project (while I was trying to read a file that was located in the script's directory).
It could not run the file since process.cwd() !== __dirname.
You can check it out and console log process.cwd() in the script you are trying to read the JSON file with.
I just changed the path to:
const fs = require('fs');
fs.readFileSync(`${__dirname}\\FILENAME`);
I also had this issue, because I had another console window open that was running the application and I was attempting to rerun yarn start in another console window.
The first Yarn executing prevented the second from writing. So I just killed the first process and it worked.
In my case, the issue occurred after I switched from a Git branch to another, references to some old files remained in the scripts inside the "node_modules/.cache" directory.
Deleting the "node_modules", "build" directories and "package-lock.json" file and then issuing the "npm install" command has fixed the issue for me.
I solved this error by simply creating a blank file at that location for which I got the error. If you are getting the error for a directory, you can try by also creating an empty directory.
If there isn't any layout folder, just use it like this in your routing:
app.get('/', (req, res) => {
res.render('something', { layout: false });
})
Here change something with your folder name.
I was facing this issue with ng-package.json file, while creating a plugin. I found out I was providing the wrong path in angular.json. Fixed my file path, issue was resolved.
May be helpful for someone.
Make sure your angular.json file has the "style" and "scripts" array as below (for Angular 12 and above):
"styles": [
"src/styles.css",
"./node_modules/bootstrap/dist/css/bootstrap.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.js"
]
Once this is done, press Ctrl + C and then ng serve.
I don't know if anyone would see this, but I'll say my answer.
First you need to be in the app directory that you created with the following command:
npx create-react-app [app-name]
Next run:
sudo npm install
to make it install all dependencies from
package.json
Then run:
sudo npm start
and make sure to run them with the sudo command, because sometimes it is absolutely necessary.
I added the PM2_HOME environment variable on the system level and now it works all right.
Running
npm run scss
npm run start
in that order in the terminal solved the issue for me.
If you're coding with TypeScript, remember that the transpiled-JS folder is where JavaScript would be searching for your file and will definitely not be able to find your html-file; hence such error.
Therefore, you'd need to copy the file into the transpiledJS folder for it to be located.
This is a helpful link on the multer GitHub page.
But for me, I also have to create a public folder in the server folder. It's like -cb(null,'public/').
Sometimes you see this issue in the following scenario.
Let's assume that you have a folder structure like node-projects (folder) → my-blog (folder) → my-blog (folder where the actual project exists), but you are in the my-blog directly, and it is an immediate child of node-project. Try to run your command in that directory.
As the project is present in my-blog that is an immediate child of my-blog. So, in this case, it searches your config.json file in my-blog (which is an immediate child of node-projects) rather than finding the config.json file in my-blog that is an immediate child of my-blog.
Just transfer your directory by hitting the command cd my-blog in your terminal and that's it. The problem is resolved!
Weirdly, in my project, I always get this error first time I add/remove a package, but then I run the same command again and it works on the second run.
My mistake was not adding / before the path. Correct path: /Users/mee/Documents/file_name.jpg
I had a Node.js version mismatch. Installing the right version of Node.js via nvm worked.
I had this error while uninstalling Bootstrap from the project, so I just deleted the script and style from file angular.json.
"styles": [
"node_modules/bootstrap/dist/css/bootstrap.min.css"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js"
]
Cleared this to
"styles" : [ ],
"scripts" : [ ]
This solved my issue.
I work with Visual Studio Code, Git and Nx and often have the problem that Visual Studio Code performs an auto staging.
In combination with Nx, it comes very often to problems. Simply unstaging the files in Visual Studio Code often helps.
In my case
import { Object } from '../config/env';
gave me the error.
I solved it with change the address like this:
import { Object } from './../config/env';
It’s happened with me. I deleted some CSS files by mistake and then copied back. This error appeared at that time. So I restarted all my Docker images and other servers and then it went away.
I tried something and got this error:
Error: ENOENT: no such file or directory, open 'D:\Website\Nodemailer\Nodemailer-application\views\layouts\main.handlebars'
The fix I got was to literally construct the directory as it is seen. This means labeling the items exactly as shown. It is weird that I gave the computer what it wanted.

Error: Cannot find module '.../webpack'

I'm just starting with node, and server stuff in general. I was following along with a well-reviewed youtube video and am at a loss here. I installed webpack and webpack-dev-server both globally and in my project folder. Now if I try to run webpack (typing nodejs webpack in the terminal) or webpack-dev-server, it just give me this error:
andrew#AndrewLaptop:~/Documents/mean-todo-app$ nodejs webpack
module.js:340
throw err;
^
Error: Cannot find module '/home/andrew/Documents/mean-todo-app/webpack'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
Shoudln't it be looking in mean-todo-app/node_modules/webpack instead of mean-todo-app/webpack?
Again, new to all of this so I could be missing something stupid, but I did search to no avail.
Thanks
edit: guess I could have mentioned I'm on Ubuntu if that matters
try using
npm install webpack
and do this in the root directory of your app.
var webpack = require('webpack');
you can use './webpack' only if you have something.js in the same directory
if you require modules installed from third party source like npm use only require('webpack');
Directly use webpack on your terminal. You don't need to refer nodejs here. Refer this documentation for executing webpack via command line as well as npm scripts: http://survivejs.com/webpack/developing-with-webpack/getting-started/#adding-a-build-shortcut
I had this problem as well. Struggled a few hours trying to figure this out. I'm on windows 10, so hopefully this will help you...
First, you need to run 2 separate processes. In Windows Power Shell, you run: node server
THEN... in another WPS, you run: webpack-dev-server
From Chrome, type in: localhost://8080
You don't want to run "node webpack-dev-server" in the second shell. That kept throwing me off.

Sudo node server return error, OsX

There is the case:
mac:~ username$: sudo node server
module.js:340
throw err;
^
Error: Cannot find module '/Users/username/server'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
So here is 2 questions:
How to specify modules folder in node.js(node on my machine is taking place on /usr/local/bin/node); may be u could give me a advice how I may organise my project files.
How could I prevent this type of errors in future? What could I read about it?
I think you don't get what exactly Node will do with your command.
With this command, Node is trying to load a script file (in your current folder) with no extension named server.
It looks like this file doesn't exist in your current directory.
Let's take a simple example: you have a file named app.js in your current folder which contains a simple output in the console.
// In the app.js file
console.log("Output in the console by NodeJS");
Now, run the command node app.js. You should have the output in your console.
Talking about organisation, it depends of your workflow and projects. You could find lot of informations about this by reading the source code of popular projects on Github.

Problems with running node module

So, I am getting the infamous error:
module.js:340
throw err;
^
Error: Cannot find module '/home/bc/projects/nodejs/pg-connect.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
here is the 2 line source causing it:
var pg = require('pg');
console.log("Hello world!");
Before asking for help here I have tried several things all of which didn't work:
1)I tried setting it to the absolute path, in my case:
var pg = require(/home/bc/node_modules/pg);
2)I tried copy/paste the above directory into my project directory.
3) I tried to install the module locally using:
npm install pg
However instead of the current directy it installed in the path /hone/bc/node_modules
I have no idea what more I can try to get it to work.
As I mention in my comment, it appears that you've successfully installed the module, but in the wrong place.
A quick fix would be to copy the contents of HOMEDIR/node_modules into CURRENTDIR/node_modules.
Then you should be able to
var pg=require('pg');
To find "require-able" modules, Node will check the current directory for its module directory, unsuprisingly named "node_modules", and if it cannot find the module there, Node will move back up the directory path until it either finds a node_modules directory containing the required module or it hits the root. So, assuming your current directory is /home/bc/projects/nodejs, Node will look for the pg module in the following directories (in order):
/home/bc/projects/nodejs/node_modules/pg
/home/bc/projects/node_modules/pg
/home/bc/node_modules/pg
/home/node_modules/pg
/node_modules/pg

Resources