devDependencies ignored in npm? - json

I am the author of two npm modules, both with devDependencies.
The first one is simpleDbLayer. Install it:
npm install simpledblayer
The downloaded package.js file has devDependencies matching what is in the git repository:
//...
"devDependencies": {
"simpleschema": "0.3.x"
},
//...
Unfortunately, nodeunit test.js will fail because simpleschema wasn't installed (as it should have been). From the manual:
By default, npm install will install all modules listed as dependencies. With the --production flag, npm will not install modules listed in devDependencies
Even weirder (and this is the really strange issue I am most affected by) is my other module, simpledblayer-mongo:
npm install simpledblayer-mongo
In he installed package.js file, I have:
"devDependencies": {},
Which doesn't match what was actually published, which contains:
"devDependencies": {
"simpleschema": "0.3.x",
"simpleschema-mongo": "0.3.x"
},
Needless to say simpleschema and simpleschema-mongo is not installed.
Am I missing something?

Yes, by default it will install the devDependencies but only for your project (devDependencies in your package.json), not for modules in the npm repository.
If you want the devDependencies of your dependencies, force it by passing --dev to the npm command:
npm install simpledblayer --dev
Also, when you have the environment variable NODE_ENV set to production, it won't install the devDependencies either (not even the ones in your package.json).

Related

npm install/uninstall modifies packages.json

I've recently upgraded to node8 (using nvm) and a weird things started to happen in one of the project I'm working on.
Node v8.11.1
Npm 5.6.0
I cd inside my project having a packages.json.
For dev purposes I wanted to remove some packages previously installed but I didn't want to modify my packages.json.
Whenever I run either npm uninstall package-name or npm install packages-name they both try to install all the missing packages listed in packages.json!
I am explicitly NOT passing the --save or --save-dev flag but still npm ignores that.
I think it might have to do with package-lock.json file?
Q: How to tell npm I do not want it to mess up with other packages or modify my packages.json?

npm install fails when package-lock.json is present

I have some strange issue regarding the package-lock.json.
We are using npm 5.6 and node 8.9.2.
We have some project with dependencies. The developers can install the deps and push the package-lock.json as recommended:
...
"dependencies": {
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
"integrity": "sha1-dHIq8y6WFOnCh6jQu95IteLxomM=",
"dev": true
}
}
...
They are getting their dependencies from https://registry.npmjs.org/..
Now we have some jenkins which gets its dependencies from some artifactory which serves as 'proxy' to https://registry.npmjs.org.
The build fails 75% of the times on some error like:
npm ERR! Unexpected end of JSON input while parsing near '...nalInternal" : {
Even when I clean the cache before with sudo npm clean cache --force (or remove .npm)
The error occurs always on a different place.
Now when I remove the package-lock.json before starting the build it works well every time.
A new package-lock.json is generated. It contains not the registry urls but the artifactory urls (https://artifactory/xxx). I thought it could maybe have to do with it. so I replaced the current package-lock.json in git with the one from jenkins. I tried again but again the same error and issue, only deleting the package-lock.json seems to solve it but as I read it's not recommended to ignore this file.
What could be an issue?

Does Webpack install -g, automatically install to nodeJS's package.json? Or is this only for local installs?

Does Webpack install -g, automatically install to nodeJS's package.json? Or is this only for local installs?
So I'm tired of trying to find workarounds for require() is not defined. Meaning I would need a module loader for my project to include modules client side. Well, I've downloaded the famous 'webpack' module loader, globally
npm install -g webpack
and I noticed it didn't install to "devDependencies" in my package.json file. But I also install webpack-dev-server, but locally,
npm install webpack-dev-server --save-dev
and it was saved into my package.json. Was this saved because I used --save-dev or because I installed locally?
I'm getting an error stating my webpack module I downloaded doesn't have a configuration file, so I'm assuming I install webpack wrong, and maybe it shouldn't have been installed globally. Please help with the understanding of globally and locally, as well as why this -g webpack install didnt get saved to the package.json?
The command npm install --gloabl will install a package in global scope and make it's bin command available to you globally. This has nothing to do with the folder or project your are in right now. That means a global install will not leave anything in any package.json files.
Learn more about npm install: https://docs.npmjs.com/cli/install
npm install -g webpack will save your files in your OS file system.
npm install webpack-dev-server --save-dev will save your package in your project directory inside a folder called node_modules.
The later one will make an entry in package.json file so that next time you can install all dependdencies with just npm install command. This command installs all your packages listed in package.json.
Missing Config file: Its looking for a file called webpack.config.js. More info can be found here: https://webpack.js.org/configuration/

npm install not creating the json file

I updated npm installed node express,even though it is not creating the jason file. can any one please let me know how to fix this.
ramesh#ramesh-PC MINGW32 ~
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
See npm help json for definitive documentation on these fields
and exactly what they do.
Use npm install <pkg> --save afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (ramesh)
ramesh#ramesh-PC MINGW32 ~
First initialize your project. Assuming your project lives in directory "sample" then:
cd sample
npm init // This will ask a bunch of questions. you can mostly just hit "enter". it will create the package.json file for you
npm i express --save
using npm init you can create package.json
To clarify a bit on the previous answers, npm install <package-name> and npm install <package-name> --save may fail if you try to run them from a directory that does not have an npm package.json file.
You might type something like npm install crud --save and get some confusing output like this:
npm install crud
npm WARN saveError ENOENT: no such file or directory, open '/Users/youruser/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/Users/youruser/package.json'
npm WARN youruser No description
npm WARN youruser No repository field.
npm WARN youruser No README data
npm WARN youruser No license field.
+ crud#0.0.28
That's not a very helpful error message -- the level is only "warn", and that last line makes it appear that the requested package got installed somewhere. But if you look in your directory, you will see it remains empty. The explanation of this "riddle" is that some package installers (like PHP's composer) will initialize a project and download the package, others (like Python's pip or npm) do not, so you have to initialize the directory and install packages separately.
Run npm init and answer some basic questions about your project, or copy a viable package.json file that follows the format below:
{
"name": "my-great-node-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}
Be careful with the package name! The name cannot match the name of a package you trying to install.
Once the directory has been initialize, you should be able to run your npm install <package-name> --save command and have the package installed into the node_modules directory and have your package.json file updated.
Try not to make any space between words in your project folder name. For instance, instead of "My Portfolio" write it like "MyPortfolio". This worked for me and created json file automatically in my project folder.

How can I use npm to download and update modules from a list in a package.json file?

I am using windows with node.js downloaded. I created this package.json.
{
"version": "0.0.0",
"name": "abc",
"devDependencies": {
"del": "^1.1.0",
"gulp-uglify": "^1.0.2",
"gulp-sourcemaps": "^1.2.8",
"gulp-typescript": "^2.3.0",
"less-plugin-clean-css": "^1.2.0",
"typescript": "^1.3.0"
}
}
Is there an npm command line task that I can run to fetch all of these modules and install / update these into a node_modules directory? If needed I can change my package.json so I would appreciate advice on that also.
Thanks
Doesn't
"npm install"
do the trick? (from your folder that contains the package.json)
It works on linux, haven't a window machine to try right now
try
npm install
npm update --save-dev
If you already had the package.json file (with dependencies entries), then you can use npm install to install dependencies on node_modules directory.
npm install
or you can update the dependencies to use latest version (will override currently installed dependencies on node_modules directory) using npm update
npm update
You don't have to put the dependency you want manually to package.json, you just use command npm install {package-name} with additional option like --save / --save-dev / --save-optional.
In example you want to add dependency of node-q to your application, you can do this:
npm install q --save
Juggling into different option --save-prefixed value will act as follows,
--save will add dependency to dependencies attribute of your package.json, it will be installed mainly for your application
--save-dev will add dependency to devDependencies attribute of your package.json, it will be installed for development phase of your application (usually it is testing dependencies)
--save-optional will installed for optional (nice to have) dependencies. I rarely used it for my applications or libraries anyway.
Don't forget you MUST run commands from your application directory (the one that node_modules directory resides).