Basic NPM usage - install package error - node.js

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

Related

How to install local npm automatically?

I developed an application in NodeJS which use external npm which are not published on npm.
I've actually separated my code in components, so the app have this structure:
/app
index.js
/components
/componentA
index.js
/componentB
index.js
so essentially when I publish the app on my server, I did:
npm i
npm i ../components/componentA
npm i ../components/componentB
as you can see this could be problematic if the app is a big project and have hundreds of components. So my question is, how can I execute a script that check automatically if the components are installed and automatically install it?
I want avoid service like bit.dev or something like. I also used npm link but unfortunately when I install a new package my own npm are automatically removed.
If you have a package.json in your project, you can run npm i -S ../components/componentA which will add it as a dependency in your package.json.
Your package.json should look like
"dependencies": {
"componentA": "file:../components/componentA",
"componentB": "file:../components/componentB"
}
On server just run npm i to get these components installed

How to get npm to favor local linked dependency over its published install

I've searched through other questions such as this one, but they all seem to be about a local npm link stopping working for another reason than mine. I assume this is a common use-case issue, so if I'm doing something methodically wrong, I'm more than happy to take suggestions on how I should be doing it.
Principally, I have a private npm module that I'm working on called #organisation/module. When working locally, I'll run npm link on it, and use it within my 'host' project as npm link #organisation/module — this all works great with hot-reloading, etc. I'll also import it as import module from '#organisation/module.
However, since I also want to publish my local changes to npm (as #organisation/module) from time to time, for build testing and production code, I need to run npm install #organisation/module on the host project.
This then seems to break the implicit npm link I set up earlier... I assume mainly because they are the same name, and npm favors an install over a link?
When I want to make live, local changes again, the only way I can currently get it to work is via npm uninstall #organisation/module and then to re-link it.
Is there a way to keep the published module installed (in order to avoid careless mistakes, like forgetting to reinstall it for build testing), but always favour the local, linked instance?
Diagram for ref:
Have you tried locally installing with the other method npm provides.
npm install /absolute/path/packageName
I believe this will change your entry in package.json to look like this:
"dependencies" {
...
"packageName": "file:../../path/to/packageName",
...
}
Since npm link creates a symlink in the global folder, while npm install is local to the project npm install takes precedence. You can read about npm link here: https://docs.npmjs.com/cli/link
To avoid this, my suggestion would be to use npm install <path to local> and when you need to use the production code use npm install #organization/module. This would update your node_modules per code basis. Read about npm install here: https://docs.npmjs.com/cli/install
Hope this helps :)
Go to the directory where your local package is located open package.json change the name from original_name to "original_name_local".
write npm link on terminal at the same location.
After this go to your working directory and write npm install <path to local>
Now whereever you're requiring or importing update the name to "original_name_local"
for example if it's require('space-cleaner') then change it to require('space-cleaner_local')
Like this you can have both local as well as production package just change the name wherever required.
Otherwise you can remove package by removing it from package.json and deleting from node_modules.
if local is needed go to local package directory and on terminal write npm link and then on your working directory write npm install ./path/to/package
if production then again delete the package as told above and write npm install package_name

npm no such file or directory

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

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

How generate nodejs express dependencies package.json

As I started to develop my first nodejs express application, I added many packages with npm.
I would like to know if there is a way to generate a package.json file containing all the current dependencies or the list of the current packages under the nodes_modules directory.
Just run npm init in the same directory as your project.
You'll be asked a list of questions (name, version, description, etc.) and once complete, it will generate a package.json file with all of the dependencies as currently installed in the node_modules directory.
Run npm list to see what you have installed. Run npm shrinkwrap to build a npm-shrinkwrap.json file, which you can use as a starting reference to build a proper package.json. My workflow is always to update package.json and then run npm install. I never run npm install foo to get some package because it creates risk of forgetting to add it to package.json and then having your application fail to start on deployment.
Updated to add: These days I do run npm install --save foo or npm install --save-dev foo since I have now decided the ~0.4.3 version numbers it adds to package.json are better than my former preference for 0.4.x since the ~ gives you a more precise minimum version number.

Resources