npm no such file or directory - node.js

I am fairly new to node and, when trying to use a third party node template, I am receiving the following error:
npm WARN saveError ENOENT: no such file or directory, open '/Users/Ryan/package.json'
This is the error with the directory it is referring me to side-by-side.
Now The file it is referring me to simply does not exist, or I haven't found it.
I assume that that node_modules directory is presumably some sort of base directory so..
is it missing something?
Or is it not supposed to be there completely?
And, most importantly, how does that directory relate to this error?
Steps I have taken after initial research:
Uninstalling/Re-Installing Node with Homebrew
Installing Express in this directory
re-running the "npm install ..." command multiple times (apparently that helped some people at some points)

That error appears is because npm packages should be installed either inside an npm project, or for global usage specifing the -g flag after npm install.
You aren't inside a node project folder (which are characterized by a package.json file) and you're not specifing the -g flag so npm is throwing a warning
If you need to use that library inside a project install it from within its root directory,otherwise specify the -g flag if you need to use the library as a command line utility

Related

Node.js: module not found after installing it with npm -g

I have installed the tar-module using
npm install -g tar
When I type
npm list -g --depth=0
I can see the entry tar#6.1.0 in the module-tree, however when I try to require it in a js-file const tar = require("tar"), I get the error message
Uncaught Exception:
Error: Cannot find module 'tar'
What am I missing?
The issue here is that you're trying to use something installed globally in a local project. You should be able to use your libraries if you install them inside the project with npm i tar.
The reason we install something globally is for use during development on many projects. This way, we don't have to install a tool on every project. With something you want to use inside a projects code however, you should install it on a project level. This way everything that the project needs to work lives inside of the project itself. You should see all dependencies listed inside of your package.json file
Not gonna advocate you do this, but if you really want to include the globally installed library, you can do something like this:
require('./../../.npm-global/lib/node_modules/tar'); // Relative path to library
Where you go up the file directory to your $HOME directory into the default global install location for node and bring it in. This is poor practice, please don't do it, but heres the info none-the-less.

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.

npm link, without linking devDependencies

It appears that when I run npm link, it will install the project globally, and it seems to install devDependencies with it.
Is there a way to run npm link without devDependencies, perhaps with the --only=production flag?
In npm#4.x or lower
When you run npm link in other_module then you will get both dependencies and devDependencies symlinked.
The --production flag doesn't change anything, still creates a symlink to the whole directory
In npm#5.1.0
They fixed it!
If you remove node_modules and then do npm link --only=production, it runs an install before symlinking, and therefore devDependencies folder are indeed excluded.
This is currently not possible with npm link. The problem is, if you install only prod dependencies in that dependency, you're able to link it, but you're not able to develop on that dependency anymore (since missing devDependencies). And vice-versa: If you install devDependencies, you can't link anymore.
The solution: A package called npm-local-development at https://github.com/marcj/npm-local-development
It basically does the same thing as npm link, but works around the devDependency limitation by setting up a file watcher and syncs file changes automatically in the background, excluding all devDependencies/peerDependencies.
You install npm-local-development: npm i -g npm-local-development
You create file called .links.json in your root package.
You write every package name with its local relative folder path into it like so
{
"#shared/core": "../../my-library-repo/packages/core"
}
Open a console and run npm-local-development in that root package. Let it run in the background.
Disclaimer: I'm the author of this free open-source project.
A workaround I use is npm pack then point to the packed file in the example

Basic NPM usage - install package error

I want to install a package called tone so I can use this code
//create a synth and connect it to the master output (your speakers)
var synth = new Tone.Synth().toMaster();
//play a middle 'C' for the duration of an 8th note
synth.triggerAttackRelease("C4", "8n");
Here's what I've tried so far (as well as completely reinstalling Node):
C:\Users\HP\Desktop\tone-js>npm install tone
C:\Users\HP
`-- tone#0.8.0
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\HP\package.json'
npm WARN HP No description
npm WARN HP No repository field.
npm WARN HP No README data
npm WARN HP No license field.
So I'm guessing I need to create a package.json file. I've tried npm init, and it asks for a point of entry, which I don't understand.
I just want to be able to use the code above. I probably need to add a require statement at the top - what should it be please?
I've also tried installing tone globally with npm install tone -g but don't know how to require the module. I'm also guessing local is better practice.
Also, why when I run npm install tone is it looking for package.json at 'C:\Users\HP\package.json' when I'm in a different folder?
All very confusing! Any help appreciated.
When you run npm init, the entry file it is asking for is the main file of your application. For instance, if the starting point of your application is in app.js then your entry file would be app.js. By default, it will be set to index.js if you don't provide one.
Another trick to using npm init is, if you don't want to setup the package.json to be specific to your project at this point in time, use the -f flag which will force the use of all defaults for your package.json.
Without initializing npm in your project you cannot save any npm packages since there is no package.json which is mandatory to save installed packages.
There are a few things you should know when using Node.js and third party modules.
The package.json file describes your project. It lists all modules you've installed with the --save tag (so npm install --save tone)
You should also know that the package.json file is used to tell others who the author of the project is, optionally what dependencies it has during development (devDepenencies, mainly used for testing modules etc) and NPM uses it when you publish your own NPM package)
To use a module, you need to require it and assign it's exported object to a variable (const Tone = require('tone');)
The entry point is the same file you call node on, so if you normally execute node app.js, your entry point is app.js. It's for NPM to know which file it make public when you've published a module. (Tip: You can now also run npm start instead of node app.js
For quick tests, just executing npm install note still allows you to require('tone'); without having a package.json

How to install a npm module in current directory?

I am trying to install express into my current "directory".
However node installs this globally and I do not understand, how I can tell node to install it in my current directory.
I asked this back when I was a terminal noob.
The solution was simple:
cd (navigate using the command line) to the directory you want to install the module in and then it should work fine. It is a good idea to npm init first.
I had a similar issue with installing node modules in my project directory, even when I did not specify the "-g" global flag. On Linux any packages I installed when in my current directory would end up getting installed into ~/node_modules (i.e. /home/user/node_modules).
The reason and fix are explained in the thread at npm install module in current directory. Briefly, npm looks for a node_modules subdirectory in the directory where
npm install
was invoked. If not found, npm keeps moving upwards, searching that directory's ancestors till it finds node_modules. Assuming a Linux system, if not found in the uppermost level of the current user's home, i.e. /home/user, it will create node_modules in the current dir, which is the required behaviour. However I already had a ~/node_modules directory, which did not allow this to happen.
The fix is to first run
npm init
in the current directory, which interactively creates a package.json file that tells npm that we're creating a package in that directory, and any dependencies need to be local to the package, hence requiring that node_modules/ and thereby node packages be installed locally.
Following the creation of package,json, install commands run in that directory will install packages such as express locally.

Resources