91

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kinnard Hockenhull
  • 2,790
  • 5
  • 27
  • 34

17 Answers17

49

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):

Enter image description here

The fix for me was: npm init --yes

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
grepit
  • 21,260
  • 6
  • 105
  • 81
  • 19
    Can you explain what this does and why it might fix the issue? – ruffin May 11 '20 at 18:07
  • 2
    Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/37645798/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen Jan 01 '23 at 12:06
29

Tilde expansion is a shell thing. Write the proper pathname (probably /home/yourusername/Desktop/etcetcetc) or use
process.env.HOME + '/Desktop/blahblahblah'

hobbs
  • 223,387
  • 19
  • 210
  • 288
  • Hmm, I thought that was handled by `app.locals.basedir = '~/Desktop/BitBox/thenewbox';` I tried `app.set('home', process.env.HOME || '/Users/Kinnard/Desktop/BitBox/thenewbox');` But that didn't work, same error. – Kinnard Hockenhull Dec 24 '13 at 01:43
  • Ok, just changing `app.locals.basedir = '~/Desktop/BitBox/thenewbox';` to the absolute path worked. Thanks! – Kinnard Hockenhull Dec 24 '13 at 01:44
  • I'm having this error too. I have no path: ..node_module/rxjs. What am i doing wrong? my issue: ** ./node_modules/rxjs/_esm5/index.js Module build failed: Error: ENOENT: no such file or directory, open '..\ClientApp\node_modules\rxjs_esm5\index.js'** – pnet Jul 04 '18 at 18:55
  • You might want to look at this answer: https://stackoverflow.com/questions/21637099/app-base-path-from-a-module-in-nodejs – BraveNewMath Dec 21 '18 at 00:37
21

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
half of a glazier
  • 1,864
  • 2
  • 15
  • 45
20
  1. First try npm install. If the issue is not yet fixed, try the following one after the other.
  2. npm cache clean, then
  3. npm install -g npm , then npm install. Finally
  4. ng serve --o to run the project.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thisuri
  • 409
  • 4
  • 5
8
__dirname

Gives you the current Node.js application's root directory.

In your case, you'd use

__dirname + '/Desktop/MyApp/newversion/partials/navigation.jade';

See this answer:

How can I get the application base path from a module in Node.js?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BraveNewMath
  • 8,090
  • 5
  • 46
  • 51
4

I had that issue using the path module:

const path = require('path');

And also do not forget to create the uploads directory first period.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sanjay
  • 838
  • 1
  • 14
  • 21
2

For those running Laravel Mix with npm run watch, just terminate the script and run the command again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Earl Lapura
  • 165
  • 2
  • 12
  • How is that related to the question? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/52585763/edit), not here in comments (*********** ***without*** *********** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 01 '23 at 12:19
2

Specifically, rm yarn.lock and then yarn install fixed this for me.

Fillip Peyton
  • 3,637
  • 2
  • 32
  • 60
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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sfscs
  • 515
  • 4
  • 8
1

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.

Enter image description here

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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/
maxshuty
  • 9,708
  • 13
  • 64
  • 77
0

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

0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AnatuGreen
  • 579
  • 7
  • 14
0

Sometimes you are just not in the right directory. Check that once and try "npm start" again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sachin
  • 196
  • 2
  • 5
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

My problem was that I didn't have a package.json file in my working directory.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tolache
  • 168
  • 1
  • 1
  • 12