I’m getting this error from my Node.js application:
ENOENT, no such file or directory ‘~/Desktop/MyApp/newversion/partials/navigation.jade’
I know the file is there because when I try to open the file using the exact copied and pasted path, it works. I also know the application is using the right directory because, well, it outputs it in the error.
asked Dec 24, 2013 at 1:16
1
I believe the previous answer is the correct answer to this problem but I was getting this error when I tried installing npm package (see below):
The fix for me was: npm init --yes
answered Jun 5, 2016 at 19:16
2
Tilde expansion is a shell thing. Write the proper pathname (probably /home/
yourusername/Desktop/etcetcetc
) or use
process.env.HOME + '/Desktop/blahblahblah'
answered Dec 24, 2013 at 1:18
hobbshobbs
220k18 gold badges207 silver badges286 bronze badges
4
I was also plagued by this error, and after trying all the other answers, magically found the following solution:
Delete file package-lock.json and the node_modules folder, and then run npm install
again.
If that doesn’t work, try running these in order:
npm install
npm cache clean --force
npm install -g npm
npm install
(taken from @Thisuri’s answer and @Mathias Falci’s comment respectively)
And then re-deleting the above files and rerunning npm install
.
It worked for me!
answered Dec 11, 2019 at 13:33
2
- First try
npm install
. If the issue is not yet fixed, try the following one after the other. npm cache clean
, thennpm install -g npm
, then
npm install
. Finallyng serve --o
to run the project.
answered Sep 29, 2018 at 8:31
ThisuriThisuri
3893 silver badges5 bronze badges
2
I had that issue using the path module:
const path = require('path');
And also do not forget to create the uploads directory first period.
answered Apr 27, 2018 at 16:38
SanjaySanjay
8181 gold badge14 silver badges21 bronze badges
3
Specifically, rm yarn.lock
and then yarn install
fixed this for me.
answered Aug 28, 2020 at 1:46
Fillip PeytonFillip Peyton
3,6272 gold badges30 silver badges60 bronze badges
For those running Laravel Mix with npm run watch
, just terminate the script and run the command again.
answered Oct 1, 2018 at 6:40
Earl LapuraEarl Lapura
1652 silver badges12 bronze badges
1
For me, it had my code folder in Dropbox on Windows 10. During the build process Dropbox would flip out over having more than 500,000 files. I moved my folder out and now it builds fine!
answered Feb 20, 2020 at 6:23
sfscssfscs
5154 silver badges8 bronze badges
When this happened to me, it was when trying to run Karma tests in an Angular project. The tsconfig.spec.js
file turned out to be incorrect. It was basically pointing to the wrong directory, and so the error was simply trying to tell me this.
For example, we had ../tsconfig.json
instead of ./tsconfig.json
, so the runner was looking for tests in the wrong folder. This may be a different use case from the OP, but the same error message brought me here and led me down the rabbit hole of trying the npm install
solutions to no avail.
answered Dec 23, 2021 at 15:57
Adam HughesAdam Hughes
14.3k11 gold badges80 silver badges121 bronze badges
It usually occurs due to a mismatch in the npm
version used while creating the package-lock.json
that currently exist and the one you are using now.
Removing the package-lock.json and running npm install
worked for me.
answered Jul 25, 2022 at 20:20
1
Reason: I have the same issue, where a few guys work on one project and change package dependencies.
Solution: Just kill file package-lock.json
and run npm i
again
answered Sep 14, 2021 at 8:11
In my case, I was running the terminal in the wrong folder. Please make sure that you navigate to the folder containing your code (App.js and others) and then use a command prompt (for Windows) to open the code. I am using Visual Studio Code, so it is to type «code.» after I have opened the command prompt in the exact folder where my code is in.
answered Jan 2, 2022 at 0:08
AnatuGreenAnatuGreen
4697 silver badges13 bronze badges
1
Sometimes you are just not in the right directory. Check that once and try «npm start» again.
answered Jul 31, 2022 at 9:26
SachinSachin
1561 silver badge4 bronze badges
I ran into this upgrading a Phoenix app to 1.6, which does not use Node.js, so in fact it is not needed. However, elixir_buildpack.config
had a reference to phoenix_static_buildpack.config
, which defined node. Remove the reference, and the error goes away.
answered Aug 18, 2022 at 18:51
My problem was that I didn’t have a package.json file in my working directory.
answered Nov 2, 2022 at 8:27
tolachetolache
1681 gold badge1 silver badge12 bronze badges
Another possibility is that you are missing an .npmrc
file if you are pulling any packages that are not publicly available.
You will need to add an .npmrc
file at the root directory and add the private/internal registry inside of the .npmrc
file like this:
registry=http://private.package.source/secret/npm-packages/
answered May 17, 2019 at 16:13
maxshutymaxshuty
9,42813 gold badges61 silver badges75 bronze badges
In this tutorial, we will help you to resolve the Error: ENOENT: no such file or directory, open
issue.
'student.json'
Node.js Tutorial :
-
Install Node.js on Windows
- Node.js Chalk Color Example
- Node.js — Read command line arguments
- Node.js Read Write File Example
- Node.js Error: ENOENT: no such file or directory
Q: What is fs
module in Nodejs?
Ans:
Node.js provides an inbuilt component called FS (File System). The Node. js file system module enables
us to interact with our computer’s file system.
Error: ENOENT: no such file or directory
Error: ENOENT: no such file or directory, open 'student.json'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at Object.writeStudentData (D:nodejsfile-examplestudent-service-impl.js:24:21)
at Object.handler (D:nodejsfile-examplestudent-app.js:10:17)
at D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:8891
at j (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:4956)
at M.handleValidationAndGetResult (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:8860)
at M.applyMiddlewareAndGetResult (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:9502)
at M.runCommand (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:7231)
at Xt.[runYargsParserAndExecuteCommands] (D:nodejsfile-examplenode_modulesyargsbuildindex.cjs:1:57762) {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
Follow below steps to resolve Error: ENOENT: no such file or directory, open 'student.json'
issue.
- This problem occurs when a file does not exist; therefore, check to see if the file exists first.
- The suitable approach is to use
fs.openSync
, it
returns the file descriptor. - If the file does not exist, the
w
flag ensures that it is created, and if it does, it
is overwritten
with a new file, overriding its content. - If users don’t need the file descriptor, users can close the file by wrapping the call in a
fs.closeSync()
call.
//check if file exist
if (!fs.existsSync('student.json')) {
//create new file if not exist
fs.closeSync(fs.openSync('student.json', 'w'));
}
Recommendation for Top Popular Post :
Solution 1:[1]
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
.
Solution 2:[2]
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
Solution 3:[3]
92% additional asset processing scripts-webpack-plugin× ?wdm?: Error: ENOENT: no such file or directory, open….==> if anyone faced to such error, you should do followings:
1) you should check the if the file path is correct in angular.json file.
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.js"
],
2) you should press crtl+c and re run the project.
Solution 4:[4]
@olleh 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')
Solution 5:[5]
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')
Solution 6:[6]
I also had this issue because I had another console window open that was running the app and I was attempting to re-run 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
Solution 7:[7]
If there is no layout folder.Just use like this to your routing:
app.get('/', (req, res) => {
res.render('something', { layout: false });
})
Here change something with your folder name and Hope it will fix your error.
Solution 8:[8]
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 «node_modules/.cache» directory.
Deleting «node_modules», «build» directories and «package-lock.json» file then issuing «npm install» command has fixed the issue for me
Solution 9:[9]
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.
Solution 10:[10]
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 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 sudo command because sometimes it is absolutely necessary.
Solution 11:[11]
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 creating empty directory also. All the best.
Solution 12:[12]
I added PM2_HOME environment variable on a system level and now it works alright.
Solution 13:[13]
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
Solution 14:[14]
Running
npm run scss
npm run start
in that order in terminal solved the issue for me.
Solution 15:[15]
Solution 16:[16]
If you’re coding with typescript, remember that the transpiled-JS folder is where JS 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.
Solution 17:[17]
If you have the same error while using expres.js in your project this worked for me:
In my project when I ran an express app 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.
I just changed the the path to:
const fs = require('fs');
fs.readFileSync(`${__dirname}\FILENAME`);
Solution 18:[18]
I had a node version mismatch, installing the right version of node via nvm worked
Solution 19:[19]
I work with vsc, git and nrwl and often have the problem that vsc performs an auto staging. In combination with nrwl it comes very often to problems. Simply unstaging the files in vsc often helps.
Solution 20:[20]
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.
Solution 21:[21]
Its happened with me. I deletes some css files by mistake and then copied back. This error appeared at that time. So i restart all my dockers and other servers and then it went away, Perhaps this help some one
Solution 22:[22]
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';
Solution 23:[23]
I tried something and got this error Error: ENOENT: no such file or directory, open ‘D:WebsiteNodemailerNodemailer-applicationviewslayoutsmain.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 wants.
The “enoent” can appear because of some missing files or usage of the relative path that can be solved by creating an expected directory structure or using an absolute path, respectively. There are also many other reasons for getting this error. This article contains all those causes and solutions with developers’ tips to solve the error quickly. So keep reading this article till the end to learn what is necessary to fix this error.
Contents
- What Are the Causes of Getting “Enoent” Error Messages?
- – Starting the Node Server
- – Using a File Path Starting at the Directory Where the Script Was Executing
- – Having Another Console Windows
- – Having No Layout Folder
- – Sudo Command
- – Wrong Path in Angular.json
- – Switching Git Branch
- – The angular.json File Doesn’t Have the “Styles” and “Scripts” Array.
- – Typescript
- – Mismatched Node Version
- – Express.JS
- – Getting Script-webpack-Plugin Error Message Along With Enoent Error
- – Delete Some CSS Files
- – Using a Relative Path
- – Node Script
- How To Solve the “Enoent” Error
- – Starting the Node Server
- – Using a File Path Starting at the Directory Where the Script Was Executing
- – Having Another Console Windows
- – Having No Layout Folder
- – Sudo Command
- – Switching Git Branch
- – The angular.json File Doesn’t Have the “Styles” and “Scripts” Array
- – Typescript
- – Express.js
- – Getting Script-webpack-Plugin Error Message Along With Enoent Error
- – Delete Some CSS Files
- – Using a Relative Path
- – Node Script
- FAQs
- – What Is the Enoent?
- – What Does Enoent: No Such File or Directory Mean?
- Conclusion
What Are the Causes of Getting “Enoent” Error Messages?
To solve the “enoent” error message, we first need to understand why this error appears. Let’s find out some of the common causes of this error.
– Starting the Node Server
This error can appear while starting the node server. This can happen because of the no existence of some important files at the expected location.
– Using a File Path Starting at the Directory Where the Script Was Executing
You can get this error if you use a file path that starts at the directory where scripts are executing but not at the project’s root.
– Having Another Console Windows
You can face this error if you have another conarele windows open that is running the app, and you try to re-run yarn to start in another console window.
– Having No Layout Folder
Having no layout folder can also be one of the causes here.
– Sudo Command
Anything wrong is the Sudo command or not running the sudo command will not allow you to write to /private/var/folders on mac, and this error appears.
– Wrong Path in Angular.json
A prevalent cause of this error is providing the wrong file path in angular.json, where you can fix the path to remove the error.
– Switching Git Branch
This error can appear if you switch from one git branch to another. The latter happened because some old files remained in the scripts inside the “node_module/.cache” directory.
– The angular.json File Doesn’t Have the “Styles” and “Scripts” Array.
If you are working in Angular 12 or above, and your angular.json file doesn’t have the “styles” and “scripts” arrays, you can also get this error in that condition.
– Typescript
If you are working on typescript, the JS will find the HTML file in the transpiled-JS folder. And if your HTML file is not present there, you will see the error.
– Mismatched Node Version
If you have installed the mismatch node version, you would probably get the error. You just need to install the correct node version via nvm to eliminate this error.
– Express.JS
If you are working with express.js and getting the same error, this could be because the current working directory might be the project’s root directory. You might be trying to read the file located in the script’s directory, and in this case, you might not run the file as a process.cwd() !==__dirname. This out and do the console log process.cwd() in the script you attempt to read JSON file.
– Getting Script-webpack-Plugin Error Message Along With Enoent Error
In some cases, you might get the script-webpack-plugin error and enoent value error. That could be because of the wrong path. You can get both of these errors on node.js or angular.
– Delete Some CSS Files
If you are working in CSS, you can face this error, have mistakenly deleted some critical CSS files and then copied them back.
– Using a Relative Path
If you are working on an API and some request, you might be using a relative path if you get this error with a message that it can’t read a path that doesn’t exist.
– Node Script
You can get this error while working with Node script and trying to transfer the files in use. Though most of the time, the error appears because of the files and folders that do not exist, you might experience that when you are close the chrome, the error gets removed.
It is happening because there is a symbolic link named RunningChromeVersion whose target doesn’t exist. The Node script doesn’t give any special treatment to links, so the default action is to follow the link when you try to open them. Like’s target doesn’t exist because it is not supposed to point to a file. It’s a dummy link with unrelated information like the version number stored in the “target” field where a file goes typically.
We discovered many causes of this error; let’s find out the solutions
– Starting the Node Server
If you are getting this error while starting the enoent nodejs server, there might be a need for some essential files not present in the expected location. If this is the case, you would see that it complained about missing a file. That is why, you need to create the expected directory structure. You can also configure the application you are working on to look in the correct directory.
If it is still not solved, go to the basics, and install enoent npm. If the problem is not solved yet, go for “npm update,” and then try “npm install.”
Using the “npm install,” a “node_module” directory in the current path will be created, the location where it is executed. When you use the “file server system module,” the below declaration will locate files from the “node modules,” a top-level directory.
const fs = require(‘fs’)
– Using a File Path Starting at the Directory Where the Script Was Executing
The simple and quick solution, in this case, is to use the below path while calling.
fs.createReadStream(‘scriptFolder/users.csv’)
– Having Another Console Windows
If you are having another console open and facing this issue, what happens is that as the first yarn executes, it prevents the second yarn from writing. As a solution, you can simply kill the first process, and you see it will work.
– Having No Layout Folder
If you don’t have a layout folder for your routine, you can use the enoent code below.
app.get(‘/’, (req,res)=>{
res.render(‘FolderName’, {layout: false});
})
Just change “FolderName” with your folder name, and your problem will be fixed.
– Sudo Command
To solve this issue, you need to follow some steps.
In the first step, you need to be in the app directory created by the following command.
npx create-react-app [app-name]
Then run the following
sudo npm install
To make it install all dependencies from the following
package.json
And then run the following
sudo npm start
Here you need to ensure that you run them with Sudo commands, which sometimes becomes indispensable.
– Switching Git Branch
If the issue is created because of switching between git branches, then you should delete “node_modules,” “build” directories, and “package-lock.json” files, and then you can issue the “npm install” command to fix the issue.
– The angular.json File Doesn’t Have the “Styles” and “Scripts” Array
If you are facing this issue because your angular.json file doesn’t have the “styles” and “scripts” arrays, you can add them to fix the problem. Those arrays are as follows:
“Styles”: [
“src/styles.css,”
“./node_modules/bootstrap/dist/css/bootstrap.css”
],
“Scripts”: [
“node_modules/jquerry/dist/jquerry.min.js”,
“node_modules/bootstrap/dist/js/bootstrap.js”
]
In the end, press CTRL+C and then ng serve.
– Typescript
If you are getting the error because the JS is not finding the HTML file in the transpiled-JS folder, you need to copy the file into the transpiled-JS folder where it should be located so that the JS can see the HTML file.
– Express.js
If you are working with express.js and getting this error, you should change the path to the following:
Const fs = require(‘fs’);
fs.readFileSync(‘${__dirname}FILENAME’);
– Getting Script-webpack-Plugin Error Message Along With Enoent Error
If you are getting the script-webpack-plugin error message along with an enoent error, the cause for this error could be the wrong path. You first need to make sure 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”
],
And then copy this and rerun the project.
You should also check the file path relative to the angular.json configuration file. You might have to add the root folder name as well.
– Delete Some CSS Files
If you are getting this error because of deleting some CSS files first and then copying them, you should restart your docker and other servers to remove the error.
– Using a Relative Path
If you are choosing a relative path, in this case, you are going to get this error. So to avoid the error, you should select the absolute path. And if your problem is still not solved, you can check the body—path value.
– Node Script
As a solution, you can use the Node equivalent of the lstat() to make sure whether the path is a symlink avoid transferring it in the same way. If you want to read the ‘target’ field, you can use readlink(). You should skip the symlinks while recursive file researching because it is possible that if you follow the symlink, you might get into the infinite loop.
FAQs
– What Is the Enoent?
The enoent meaning error no entry. This is used for more than files or dictionaries.
– What Does Enoent: No Such File or Directory Mean?
No such files or directory means that the library is currently needed or the executable binary doesn’t exist.
Conclusion
Let’s review what we learned today:
- Some causes can lead you to this error, including starting the node server or using a file path starting at the directory where the script was executed.
- You must have all the essential files at the expected location; otherwise, you must create the expected directory structure to avoid this error.
- Don’t use the relative path rather than the absolute path.
This article covered all the causes that can lead you to face the “enoent” error and their solutions. It’s time to meet this error with the help of this article and remove all the hurdles that stop you from coding.
- Author
- Recent Posts
Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team
Update:
I think in my case it happened because the project was initially installed with pnpm, and I ran yarn add
thereafter.
Hitting this error consistently today.
Both locally on MacOS and on Ubuntu in CI: https://github.com/akd-io/create-next-stack/actions/runs/4772052085/jobs/8484361470?pr=172
local yarn-error.log:
Arguments:
/Users/akd/.nvm/versions/node/v18.13.0/bin/node /Users/akd/.nvm/versions/node/v18.13.0/bin/yarn add @emotion/react@^11.0.0 @emotion/styled@^11.0.0 @chakra-ui/icons@^2.0.0 @chakra-ui/react@^2.0.0 @mui/material@^5.0.0 react-hook-form@^7.0.0 formik@^2.0.0 framer-motion@^9.0.0 mrm@^4.0.0 mrm-task-lint-staged@^7.0.0
PATH:
/Users/akd/workspace/create-next-stack/packages/create-next-stack/node_modules/.bin:/Users/akd/Library/pnpm/global/5/.pnpm/pnpm@8.3.0/node_modules/pnpm/dist/node-gyp-bin:/Users/akd/workspace/create-next-stack/node_modules/.bin:/Users/akd/Library/pnpm:/opt/homebrew/opt/openjdk/bin:/Users/akd/.nvm/versions/node/v18.13.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/akd/Library/pnpm:/opt/homebrew/opt/openjdk/bin:/Users/akd/.nvm/versions/node/v18.13.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/akd/.yarn/bin:/Users/akd/.yarn/bin:/Users/akd/.nvm/versions/node/v18.13.0/bin
Yarn version:
1.22.19
Node version:
18.13.0
Platform:
darwin arm64
Trace:
Error: ENOENT: no such file or directory, copyfile '/Users/akd/Library/Caches/Yarn/v6/npm-globby-11.1.0-bd4be98bb042f83d796f7e3811991fbe82a0d34b-integrity/node_modules/globby/index.js' -> '/Users/akd/workspace/create-next-stack-tests/run-d4eb6dcc-9846-4222-a5ef-100d98053b1e/node_modules/@typescript-eslint/typescript-estree/node_modules/globby/index.js'
npm manifest:
{
"name": "run-d4eb6dcc-9846-4222-a5ef-100d98053b1e",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@types/node": "18.15.13",
"@types/react": "18.0.38",
"@types/react-dom": "18.0.11",
"eslint": "8.39.0",
"eslint-config-next": "13.3.1",
"next": "13.3.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "5.0.4"
}
}
yarn manifest:
No manifest
Lockfile:
No lockfile