How to include options passed to npm installer when including dependencies in package.json - node.js

I am very new to node.js so please pardon my ignorance on a simple question. I am adding dependencies to package.json for a node.js application and am wondering if it's possible to specify command line arguments that would normally be passed to npm install. For example, when installing the mongodb package from the command line, you might need to pass an option:
npm install mongodb --mongodb:native
Is their a way with the package.json syntax to specify that a package should be installed with command line options?

It's not perfect, but I have been able to get around this problem by adding an explicit npm install into the preinstall script of my package.json file. In this way, the mongodb package is added as a binary before npm gets a chance to do it incorrectly. Hope this helps
"scripts": {
"preinstall" : "npm install mongodb '--mongodb:native'"
}

Related

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 install: Use global package if it exists, rather than installing twice

When using npm install with a package.json file, how do I get it to use a globally installed package that meets the criteria instead of downloading and installing the package locally again?
I know about link, but is there a way to do what I'm describing?
Yarn seems to work much better with repeated dependencies. So try yarn install instead of npm install.
One way of doing it for a specific set of modules is removing these modules from the dependencies section and creating a prestart script that contains all the modules that you prefer having installed globally.
A simple example might look something like this:
"scripts": {
"test": "mocha",
"prestart": "npm i -g mocha mysql bluebird"
},
Instead of prestart you can use one of the other hooks such as preinstall and prepare.
Note that this won't work as-is with packages you want to publish and would require a bit more hacking.
Help on running scriipts: https://docs.npmjs.com/misc/scripts

npm install --save, what is the use of not saving

I understand the differences between npm install something and npm install something --save (for anyone wondering, the first one will install the dependency only while the latter will install the dependency and add it to your package.json).
However I do not understand why there is a --save option in the first place. In other words, why would you ever want to install a dependency without adding it to your package.json file? Why is the --save option not default?
A lot of websites/npm modules/SaaS suggest installing their module using npm install something (newrelic is one of them for instance), am I missing something?
Edit: Starting from NPM 5, --save is now on by default.
You would have a scenario such as you need some module to install without adding dependency to package.json file, for ex. you just want to try some module, and not sure you would be really using that module in production or while deploying, so instead adding the module dependency to package.json, just give it a try without using --save. this is why npm install without --save exists.
But For most of your modules you might require using --save, for ex. npm install express --save,
in this case you surely know that you are going to use express for you application.
The other scenario, for not using --save, would be, npm install heapdump or npm install nodemon, I would use it for testing my apps performance, but not add a dependency in the package.json :)
Also, As #surajck said in comment below: when you are doing global installs, in that case adding dependencies using --save, to the package.json would not make sense.
I just learned a nice trick from Jonathan Mills' JavaScript Best Practices course on Pluralsight. From the terminal:
npm config set save=true
Now I don't need to remember --save anymore. And I also now use
npm config set save-exact=true
Because I want the exact version of the package not the ^ prefix.
By default with version npm 5.0+ npm install adds the module to the dependencies list in the package.json file; with earlier versions of npm, you must specify the --save option explicitly. Then, afterwards, running npm install in the app directory will automatically install modules in the dependencies list.

nodejs express npm install dependencies errors

I am very new to node.js and am following this tutorial
When I get to step 5, after completing step 1-4 successfully, and run the npm install command I get these errors.
http://i.stack.imgur.com/LPN49.png
Also when I run npm start command I get these errors in image 2
http://i.stack.imgur.com/TrOJz.png
Please help me rectify these errors.
I originally answered this question on Quora -- including an explanation about why the SO community frowns on this type of question -- but here's the technical portion if anyone comes to this question for a more general answer:
npm install goes through the list of dependencies in your package.json file, fetches each one from NPM, then installs it locally for you. If there was an error with that process then you will be missing one or more dependencies -- if you try to run node /path/to/node/server/file then Node and Express will start looking for dependencies that may not be there because your npm install errored out.
Additionally, you can only use npm start if the package.json file has a scripts property that tells node what start script to use. If it's not there, it falls back to node server.js, which won't start your server if it's called something other than server.js. (For more information: node.js express npm start)
You should try to confirm whether your package.json is actually at the file path on the first "ERR!" line after you ran npm install. I'm guessing it didn't find the file so it couldn't install dependencies, and then you're getting an error from npm start because you didn't install Express's body-parser dependency, which prevented it from starting your server.

Not able to run the code on Github : NPM Install and JSON package

I am trying to run the code present on the Github using the npm install
But when I am running the code it says that Error in installing npm modules and hence can't run the code.
I also asked this question to npm forum but they said that it is not based on NPM and hence they can't help. I am a newbee and is learning.
The link to the GItHub is
https://github.com/Operational-Transformation/ot.js/
I simply extracted the zip file and tried to run this code using Command Prompt. I want to somehow run this code. Please suggest me method to run this code.
I have Node.js along with socket.io
Just do
npm install ot
This will install the node package you linked to on github.
You can see the name of the package in the package.json file
// package.json
{
"name": "ot",
"description": "Collaborative editing using operational transformation",
// ...

Resources