NPM on Windows doesn't know the directory - node.js

I'm in a subfolder (D:\Alex\Desktop\Git\project), but even in that directory, running npm list says that there's no such project in D:\Alex\Desktop. I also can't install node_modules because none of the npm commands know the actual folder I'm in. I haven't run into this issue at all before, any ideas?

Suggested Info to Gather (and add to your post):
What version of Windows is installed (likely not the issue, but there are some known issues associated with Windows 7)?
What version of Node.js is installed (Use: node -v to check)?
What version of NPM is installed (Use: npm -v to check)?
When Node.js is installed using the Microsoft Windows installer package (.msi) download, NPM will also be installed, but the version lags behind the latest. So, for example, if you install the current LTS version of Node.js (6.11.3), NPM will be at version: 3.10.10, whereas the latest version of NPM is: 5.4.2.
Based upon your description of the problem, I am guessing that your version of Node.js is current (or fairly close), but that your version of NPM may be a bit out of date. Older versions of NPM would commonly generate a confusing and misleading error message associated with local modules that did not contain a package.json file; so running npm ls would produce error output similar to:
npm ERR! error in D:\Alex\Desktop\Git\project: ENOENT: no such file or directory
If I have guessed correctly, you should be able to resolve the problem by creating a package.json file in the D:\Alex\Desktop\Git\project directory using one of the following options:
Open a command prompt terminal window in the: D:\Alex\Desktop\Git\project directory/folder, run: npm init, and provide answers to the question prompts.
Open a command prompt terminal window in the: D:\Alex\Desktop\Git\project directory/folder and run: npm init --yes (which will suppress the prompts and apply default values).
Use a text editor to paste the following drop-in values into: D:\Alex\Desktop\Git\project\package.json, which declares the 2 required values: "name", "version", and includes a "description" because if there is no "description" field in the package.json, NPM uses the first line of the README.md or README (and your project may not include either):
{
"name": "my-desktop-git-project",
"version": "1.0.0",
"description": "Alex's Desktop Git Project"
}

Related

Why different version of NPM is being used?

It is my understanding that npm gets installed alongside node when I use the node installer in Windows 10, and will be located in "D:\Program Files (x86)\nodejs" (I installed the 32 bit version to my D drive, C is my primary OS drive).
However, "npm's globally installed packages (including, potentially, npm itself) are stored separately in a user-specific directory (which is currently C:\Users<username>\AppData\Roaming\npm)." - https://docs.npmjs.com/try-the-latest-stable-version-of-npm
Initially, after I first installed nodejs, my Roaming/npm folder was empty since I did not install any packages globally. Afterwards, I wanted to upgrade my npm so I ran the following command - "npm i -g npm". This added the following items to my Roaming/npm folder.
Roaming/npm
Now, when I run npm -v, the updated version is shown (7.24.2).
My questions are:
1.) Since I only updated the global ( -g ) installed one ( %appdata%\npm\ ), the npm in "D:\Program Files (x86)\nodejs" should remain as the old version right? (6.4.13).
2.) If so, why is the newer version being used, if (refer to below)
According to https://docs.npmjs.com/try-the-latest-stable-version-of-npm, "it will always use the version of npm installed with node instead of the version of npm you installed using npm -g install npm#." This does not seem like the case for me.
I did a "echo %PATH%" in cmd and I have confirmed that "D:\Program Files (x86)\nodejs" is indeed placed before "C:\Users<username>\AppData\Roaming\npm". So why is the newer version being used instead?
I tried to open cmd in "D:\Program Files (x86)\nodejs" and ran ".\npm.cmd -v" and it still gave me the same result which I was not expecting (7.24.2). I looked at the date modified for the nodejs folder and it does not seem like any changes were made.
Another thing I tried was logging in as a different user on my PC and running "npm -v", when I did that, it was showing the old version (which is what I expected since npm was only updated for my admin account, (admin\Appdata\Roaming\npm). So why is it that when I'm logged in as admin, it seems like the npm in "D:\Program Files (x86)\nodejs" is updated as well?
Any help to enhance/correct my understanding would be greatly appreciated, I am still quite new to npm and node.
Update: I am aware that nvm exists. This question is asking about the behaviour mentioned above.

Is there any way to fix package-lock.json lockfileVersion so npm uses a specific format?

If two different developers are using different versions of node (12/15) & npm (6/7) in a project that was originally created using a package-lock.json "lockfileVersion": 1, when the developer using npm 7x installs new packages it seems that the package-lock.json is re-created using "lockfileVersion": 2.
This seems to cause issues for the developer using npm v6, as it tries to work with the lockfileVersion 2, but it ends up producing new diffs.
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion#1, but package-lock.json was generated for lockfileVersion#2. I'll try to do my best with it!
Is there any way to specify to newer versions of npm to only use "lockfileVersion": 1? Or do we just have to get all devs on the same version of npm?
Is there any way to specify to newer versions of npm to only use "lockfileVersion": 1? Or do we just have to get all devs on the same version of npm?
I will advise you to pin the Node/NPM version and align it across your environments (development, staging, and production).
you can leverage nvm for managing Node version by adding to your project .nvmrc file (don't forget to store it in your source control).
for instance, .nvmrc will look like:
$ cat .nvmrc
14.15.0
then, you can use nvm install && nvm use to use the pinned version of Node.
NPM also supports engines:
You can specify the version of node that your stuff works on:
{ "engines" : { "node" : ">=0.10.3 <0.12" } }
And, like with dependencies, if you don't specify the version (or if you specify "*" as the version), then any version of Node will do.
If you specify an "engines" field, then npm will require that "node" be somewhere on that list. If "engines" is omitted, then npm will just assume that it works on Node.
You can also use the "engines" field to specify which versions of npm are capable of properly installing your program. For example:
{ "engines" : { "npm" : "~1.0.20" } }
Unless the user has set the engine-strict config flag, this field is advisory only and will only produce warnings when your package is installed as a dependency.
When utilizing the engines field and make npm fail when the version constraints are unmet, set engine-strict=true (since it is false by default) in .npmrc file or as an npm_config_engine_strict=true environment variable
If set to true, then npm will stubbornly refuse to install (or even consider installing) any package that claims to not be compatible with the current Node.js version.
This can be overridden by setting the --force flag.
Another approach is to use a Docker container as a runtime environment for development and execution, which implies that you neither need to install Node, nor NPM. e.g.
$ mkdir my-project
$ cd my-project
$ docker run --rm -it -v $PWD:/app --entrypoint /bin/bash --workdir /app node:14.15.0
root#4da6ee3c2ac0:/app# npm init -y
Wrote to /app/package.json:
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
root#4da6ee3c2ac0:/app# npm install
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN app#1.0.0 No description
npm WARN app#1.0.0 No repository field.
up to date in 1.694s
found 0 vulnerabilities
root#4da6ee3c2ac0:/app# exit
exit
$ ls -x1
package-lock.json
package.json
As you can see, with neither Node, nor NPM:
Created a new directory for a fresh project
Spun up a Node Docker container, which comes with Node and NPM
Created a new project (npm init -y)
Exited the Docker container
Listed the files within the working directory, where the container was spun
Since the docker run command above is long, you might wish to leverage docker-compose for a more streamlined workflow.
npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion#1, but package-lock.json was generated for lockfileVersion#2. I'll try to do my best with it!
to overcome this issue, running the command
npm i -g npm#latest
globally and running the command
npm i npm#latest
in the project file helped me resolve the issue.
As far as I can see the npm docs say that npm v6 will work with version 2 lockfiles in spite of the warning, so you don't need to do any of the things suggested in the accepted answer and can safely ignore the warning message.
In the npm 7 release notes they said:
One change to take note of is the new lockfile format, which is
backwards compatible with npm 6 users. The lockfile v2 unlocks the
ability to do deterministic and reproducible builds to produce a
package tree.
In the npm docs it says (my emphasis):
lockfileVersion
An integer version, starting at 1 with the version number of this
document whose semantics were used when generating this
package-lock.json.
Note that the file format changed significantly in npm v7 to track
information that would have otherwise required looking in node_modules
or the npm registry. Lockfiles generated by npm v7 will contain
lockfileVersion: 2.
No version provided: an "ancient" shrinkwrap file from a version of npm prior to npm v5.
1: The lockfile version used by npm v5 and v6.
2: The lockfile version used by npm v7, which is backwards compatible to v1 lockfiles.
3: The lockfile version used by npm v7, without backwards compatibility affordances. This is used for the hidden lockfile at
node_modules/.package-lock.json, and will likely be used in a future
version of npm, once support for npm v6 is no longer relevant.
This is why they can automatically upgrade lockfiles from v1 to v2, which you mention, without breaking anything.
As of version 8.1.0 there is a flag --lockfile-version in npm with which you can override the default lock file version:
npm i --lockfile-version 3
Here is the link to the original PR.
I encountered the same problem today. I am working on a project with a developer having a different version of npm (>7) and i ran into the same issue. I simply upgraded my npm version to the latest version which was being used by the other developer as mentioned above.
Following are the steps to upgrade your npm (for windows):
First, ensure that you can execute scripts on your system by running the following command from an elevated PowerShell. To run PowerShell as Administrator, click Start, search for PowerShell, right-click PowerShell and select Run as Administrator.
Next execute following commands:
Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
Then, to install and use this upgrader tool, run the following command (also from an elevated PowerShell or cmd.exe). Note: This tool requires at least Node v8
npm install --global --production npm-windows-upgrade
npm-windows-upgrade
Want to just install the latest version? Sure:
npm-windows-upgrade --npm-version latest
Now you can select the version which you want to install from the command line.
https://github.com/felixrieseberg/npm-windows-upgrade
The above link is the tool which I've used. This tool is both for Linux/Windows. I hope it will help.
There is a much more simpler solution than using nvm:
npx npm#6.14.17 i --save
With this you can generate a new lockfile with version 1, use the latest node js version and you don't need to change anything on your machine.
lockfile construction depends on the npm version. v7+ will create lockfile 2, below will create lockfile v1.
Lockfile v2 is backwards compatible so people running npm < v6 will be ok to use it, but i am looking at a circleci build failure on my second screen which suggests some of the npm packages we use are not compatible with lockfile v2... i.e. old npm packages may not be as well maintained and compatible with lockfile v2.
Hence probably the best course of action is to dockerise and isolate your env.
This is why docker was invented! Get your project running in a docker container, then have a makefile command to build your project, maybe something like
.PHONY: up
up:
$(MAKE) down
docker-compose up -d
$(MAKE) logs
With a compose file to setup your project, and then rely on make <insert command> to run / build your project. One command could be make shell to enter a shell environment where all your devs have the same npm / node versions. npm i -g npm#latest is not an answer as that just installs npm on wherever it is run, so new devs will still have to run that command unless its part of the build.
Try to remove package-lock.json and run npm install again.

Why is "npm install" looking for a file that doesn't exist (package.json) on a fresh NodeJS installation?

Hello, World!
I am having trouble getting NPM working.
First, I installed node.js from https://nodejs.org/en/download/ (the 64-bit .msi, on Windows 10, Version 10.0.18362 Build 18362). Node -v is 12.16.3.
Using Powershell, I navigated to the nodejs directory created in the install. Then I attempted npm install, which ultimately drew a series of errors beginning with "ENOENT: no such file or directory, open '...\nodejs\package.json' ".
The nodejs directory contains a file called "package-lock.json", but no "package.json".
Renaming the "-lock" file did not fix the error.
I've read on this site that Node came with NPM pre-installed. I can run npm -v without a problem ("6.14.4" returned) but trying npm start gives me the same error as npm install (cannot find package.json).
I have uninstalled Node & reinstalled twice, same problem.
Thoughts?
You should create a package.json file for every project using npm libraries, whether or not you are going to publish your code anywhere.
The easiest way is to run npm init and answer the questions, then npm will create the package.json file.
(Or take a look at the docs for other ways to run init, like npm init -y to just generate a plain package.json that you can manually edit.)
BTW, package-lock.json is a different kind of file that's generated to say which versions of each transitive dependency were installed. It doesn't have the same format as package.json; don't mix them.

Why is the main file of my published npm package not accesible?

I published this pacakge to npm (here's the GitHub repo) and, as you see, the main file in package.json is "men". It looks like I'm missing something because once I download it with npm i -g real-men and type men into the terminal (even after restarting the terminal and doing source /etc/environment ), I get a "men: command not found".
I see, for instance, that the "ng" executable for the Angular CLI has a link in the /bin node folder. Then I thought "hey, maybe I'm missing a step which copies a link to that folder", but I'm still confused and haven't found anything by googling it.
OS: Manjaro x64 (Illyria 18.0.0-rc)
Node: placed in user home folder (tar.gz extracted there, bin folder manually added to PATH)
npm -v: 6.4.1
node -v: v8.12.0
EDIT: I've marked as deprecated the package, by the time being
Solved
Solved by adding this to package.json:
"bin": {
"men": "./men"
}
My bad, I was not aware of this property and its uses.
Maybe try:
npm install
You might have updated the file, but never actually downloaded the dependencies.

Node MODULE_NOT_FOUND

I just upgraded to node version 9.0.0 and am now getting this error in the command line when trying to use npm install
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module 'internal/util/types'
I'm using:
OSX 10.10.5
Node version 9.0.0
NPM version 5.5.1
Extra information: I am also trying to do this with a Laravel 5.5 project. This is how I update my version of node: How do I update Node.js?
run
rm -rf /usr/local/lib/node_modules/npm
and then re-install Node.js will work in most cases
Leaving this here for anyone using the n nodejs version manager:
$ n 6.12.0 # Go back to a stable release
$ npm install -g npm#latest # Update npm to latest
$ n lts # Get 8.9.1
$ npm install #Should work now.
The MODULE_NOT_FOUND error seems to happen when changing between node versions and some files are possibly still being cached. I am not sure exactly but the above sequence of commands work for me.
When I first got this, I solved just running "npm install" again to make sure everything was installed.
I got similar error also on Windows 8 after I have just upgraded node js. First: how I ran into the issue then the solution that worked for me.
How I ran to the issue:
When I did npm --version and node --version I discovered that I wass running npm v3.x and node 5.x. So I went to nodejs.org site from where I downloaded node-v8.11.3-x64.msi. After installing the msi package I confirmed that my nodejs version was now v8.11.3 via node --version command.
Then, when I ran "npm install http-server" (w/o the quotes) that's when I got the issue:
npm ERR!
node v8.11.3
npm ERR! npm v3.5.3
npm ERR! code MODULE_NOT_FOUND
My resolution:
I did some research including on the internet and found out that the npm version pointed to in my path was the one in my roaming profile C:\Users[myname.hostname]\AppData\Roaming\npm. In other words, the npm being used is not the one in the updated package I have just installed which is located in C:\Program Files\nodejs.
The resolution was to delete npm and npm-cache in the roaming folder. Note, I used cygwin as I was not able to delete these folders via Windows cmd prompt. With cygwin, I navigated to
cd "C:\Users[myname.hostname]\AppData\Roaming"
Then I removed the aforementioned folders like so
rm -rf npm-cache
rm -rf npm
After that, I opened a new Windows cmd prompt and was able to now successfully install http-server like so:
npm install http-server
Hope this works for you.
For me it was package installation issue, so I just write,
npm i or npm install in the root of the application.
to open the terminal in the root of the application, if you're using VS-code right click on the package.json and click on Open in integrated terminal.
I founded this problem too, so I found that I have imported wrong module instead of express module I had imported router module after I had replaced this two my code work as well
If all the above solutions doesn’t work check for any blank spaces in your folder/file where you copied the path
Make sure you are inside the project folder.
Rename the folder "node_modules" to any other name (for example: node_modules_old).
Run command: "npm i" (the command will build new the folder node_modules).
Try running your program again.
If the problem is resolved and your program is running correct, delete the old folder node_modules.
If you are using libraries make sure to install everything with npm or yarn before starting. And in cases of you files if you are going to use them make sure to do the export.module thing everytime.
If you are working with Local modules then don't have node_modules. All things go well in a easy way.
But if you want to work with both local and node_modules then use
.mjs (extension) - For modules
.cjs (extension) - For common scripts which you want to run with node
in which you can use require statements like
var http = require('http');
var fs = require('fs');
but if using .js extension then use
import http from "http"
import fs from "fs"
And also your package.json for type
Haa well, I have spent two days on this and have done everything I can to fix this issue even tried resetting the system but none of them reloved the issue.
And accidentally found out what was causing this issue, it is because of & in my parent folder name. File hierarchy R&D>remix>blog, When I was trying to run the blog server it was throwing module not found, require stack error.
code: ←[32m'MODULE_NOT_FOUND'←[39m,
requireStack: []
Solution: I have changed the parent folder name to RnD and it fixed the issue. If the file name contains any special characters(even parent folders) try updating it. In my case, it is &
The MODULE_NOT_FOUND error happened to me and even running npm install the error persisted.
Try to do this
For me, what worked was deleting the node_modules folder
rm -r -f node_modules/
After that, run the command to install the package.json dependencies
npm install
What happened to me was that when I ran npm install for the first time I had a very low internet connection and therefore I believe that the packages from package.json were not downloaded correctly and due to that the MODULE_NOT_FOUND error occurred. The funny thing is that just running the npm install command has no effect because it understands that the package is already there but it isn't. Similar as a corrupted data. In my case the npm update was without effect too.
If when you are using React And getting this error message. You can use this ,
NPM
npm install #reduxjs/toolkit
Yarn
yarn add #reduxjs/toolkit

Resources