package.json dependencies exact version not working - javascript

This is an excerpt from my package.json:
"dependencies": {
[...]
"jquery": "2.2.3",
[...]
}
According to the package.json documentation ("must match version exactly") it should get jQuery with version 2.2.3 when you do npm install.
So I was surprised when I found that in my node_modules/jquery/dist folder there is a jquery.js file which says: jQuery JavaScript Library v3.0.0 (Which, of course, broke some of my code)
What happened here? Is this a bug? Is this intended behaviour because some other dependency has a jQuery#3.0.0 sub-dependency?
Same occurs when I put the caret in front of the version number ("jquery": "^2.2.3"). Could not find anything about this in the docs, on SO or on google, so any help will be appreciated.

Do you have a npm-shrinkwrap.json file, which makes npm ignore all changes to your package.json dependencies (devDependencies are not affected) and has a hardcoded jquery#3.0.0 version?
Delete that file and run npm install again.
If you need to, run npm shrinkwrap again afterwards to re-create the shrinkwrap file with the new version numbers.
See documentation here: https://docs.npmjs.com/cli/shrinkwrap

Related

ParcelJS bundling error - favicon.ico: Invalid Version: undefined

This is the first time I am using ParcelJS. I npm it following their instruction everything looks good. So it would be the latest version. As soon as I run it against my project (a .html file with some js and css attached to it), I get this error.
favicon.ico: Invalid Version: undefined
I have tried to ask Google without much luck. Got any ideas?
If you are using Parcel version 1.12.4 this github issue is the reason why you got this error. It's not your fault.
You may try to pin your Parcel version as 1.12.3 in package.json:
"dependencies": {
"parcel": "1.12.3"
},
If your Parcel is installed globally, you may check your version of Parcel:
parcel --version
1.12.4
Uninstall 1.12.4 and reinstall 1.12.3:
npm uninstall -g parcel-bundler
npm install -g parcel-bundler#1.12.3
I had the same error and first I installed "#babel/preset-env": "^7.13.8" and it worked properly.
In my case, I had the quite similar error because my IDE accidentally imported a non-existent module. I had been suffering for a few hours, before I noticed that Т_Т So if decisions don't solve your problem, maybe it will be your case. Just check imports
TypeError: Invalid Version: undefined at new SemVer

Cannot find module that's been specified in babel.config.json

I'm new to using Babel but have installed it according to the Installation guide here https://babeljs.io/setup#installation
It says at the end that you need to install plugins in order for it to work:
Now, out of the box Babel doesn't do anything ... You will need to add plugins for Babel to do anything.
I want to use this plugin: https://babeljs.io/docs/en/babel-plugin-transform-async-to-generator to fix some Internet Explorer 11 issues as described here: async function declaration expects ';' in Internet Explorer
I've created a file in the root of my project called babel.config.json and added the following to it.
{
"plugins": ["#babel/plugin-transform-async-to-generator"]
}
My package.json contains this:
"scripts": {
"build": "babel webroot/js -d webroot/js/babel"
},
"devDependencies": {
"babel-cli": "^6.0.0"
},
When I execute npm run build from my project root it gives the following error messages.
Error: Cannot find module '#babel/plugin-transform-async-to-generator' from '/Users/andy/my-project'
Prior to creating babel.config.json I was able to run npm run build without getting any errors. As per the quote this doesn't really do anything other than output the same JavaScript as I had before transpiling it. But I knew that the process worked - in my case the input files are in webroot/js and it outputs the equivalent transpiled files to webroot/js/babel.
I don't understand how to fix this error. Is this some issue with my config file or is additional setup needed?
I've read the documentation on Babel Config Files at https://babeljs.io/docs/en/config-files and found this incredibly convoluted. My goal is to have a configuration file per project, which is why I opted for babel.config.json
npm version is 6.13.4, node version is 12.16.1.
babel-cli is outdated, you should install #babel/cli instead. It also has a peer dependency on #babel/core which you need to install as well. #babel/plugin-transform-async-to-generator plugin needs to be installed separately.
Summarizing:
Uninstall outdated babel-cli:
npm uninstall --save-dev babel-cli
Install the required packages:
npm install --save-dev #babel/cli #babel/core #babel/plugin-transform-async-to-generator
Regarding your problem with IE 11. #babel/plugin-transform-async-to-generator won't fix your issue. IE 11 doesn't support yield.
If you want to use the latest ECMAScript features and still support a wide range of browsers you should instead use #babel/preset-env. You can check out the docs on the official website: #babel/preset-env.
For async to work in IE 11 you will also need to setup polyfills. #babel/preset-env won't use them with default settings. Look under useBuiltIns option for instructions.
Additionally, there's a problem with your build script, specifically with this line: babel webroot/js -d webroot/js/babel.
With such a command babel will process whole webroot/js directory (including subdirectories) and place transpiled files to webroot/js/babel. This means that after you run this command again it will process not only your original source files but also transpiled source files because webroot/js/babel is a subdirectory of webroot/js.
I think you need to install #babel/plugin-transform-async-to-generator. More here: https://www.npmjs.com/package/#babel/plugin-transform-async-to-generator#install
npm install --save-dev #babel/plugin-transform-async-to-generator

How to include MathJax as a dependency

I'm trying to include MathJax as a dependency in my bower.json or package.json file. However, I can't find documentation anywhere on how to correctly include it.
I've tried including it like so:
"devDependencies": {
"mathjax": "2.7.5"
}
This does not work.
Does anyone know where I could find this information?
with that you are only including it as a devDependency, basically you installed the package using:
npm install mathjax -D
but what you should do is:
npm install mathjax --save
with that you will be able to use it.
The solution to this issue is to include MathJax in the bower.json file as follows:
"devDependencies": {
"MathJax": "2.7.5"//or replace version number with "latest"
}

Why is my "npm install" downloading a lot of excess libraries that I did not specify?

This is my package.json dependencies array:
{
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"express": "~3.5.0",
"socket.io":"1.0",
"mongodb":"2.2.21",
"request":"2.79.0",
"q":"1.4.1",
"bcryptjs":"2.4.0",
"jsonwebtoken":"7.2.1",
"lodash":"4.17.4",
"express-jwt": "5.1.0"
}
and then when i perform an NPM install this is the output:
http://imgur.com/fyEHUcI
Why am I getting all of this? On one PC I'm only getting the dependencies I requested but on my laptop I get all of this. I am very confused. I have the output as well but it looks ugly when pasted here.
The packages you're installing have dependencies - this means that when you install them, you're also installing their dependencies.
I went ahead and created a mock package.json with your dependencies and did an npm install.
Indeed, the packages actually displayed were the ones listed, but an npm ls will show all of the dependencies each of those dependencies installed, as noted by Pineda in a comment on your question.
You can use npm ls to view the dependency structure. Full documentation can be found here: https://docs.npmjs.com/cli/ls
To dispel any weirdness regarding packages (which happens to me too), it's best to delete node_modules folder and npm i again. After that, combination of dependencies in your package.json (both dev-dependencies and normal-ones), should match the output of the command: npm ls --depth="0".
Now, on both your PC and your laptop the npm ls --depth="0" will match the combined list of all dependencies in package.json. Both cannot mismatch, otherwise that would mean npm became self-conscious and went rogue 😁.
Now, the readers of SO down-voted your question assuming you don't understand that dependencies can have dependencies and that's where the folders are coming from.
I hope that's not the case and that you will not get discouraged by any negativity happening in SO.
There is another possible scenario which might explain your case.
If you forgot to add --save when installing dependencies, dependencies were just saved into node_modules folder, but not added to your package.json. Thus, you might have inadvertently installed more things that you use.
When you delete the node_modules folder and install again, all dependencies are wiped and only what was in package.json is installed. That's why I suggest to delete node_modules folder first. As a note, npm-publishing automation libraries like my preferred np tend to wipe and reinstall dependencies before publishing the npm packages because of this exact reason. There can be "rubbish" in node_modules and wipe/reinstall helps.
I suggest to refer to this brilliant answer on --save.
With regards of previous answers, Jonathan mentioned npm ls but I find it unwieldy on anything else than trivial libraries — too much scrolling. It's best to use npm ls --depth="0" to check just the topmost level of your dependencies.

Using specific version of npm shrinkwrap

I am deploying my node.js app to Appfog but since their install script cannot parse npm-shrinkwrap.json the whole deploy process fails.
An example dependency in shrinkwrap.json today looks like this
"async": {
"version": "0.2.10",
"from": "async#0.2.10", <--- This line breaks install script at appfog
"resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz"
},
I went through the whole shrinkwrap file and removed the "from" part from each dependency declaration and i managed to upload my application, and it works.
So my question is , how do i use an older version of npm shrinkwrap so i can get the version of shrinkwrap.json that i need?
Appfog support told me i need to use version 1.1.21 but i have not succeeded in installing it.
Please ask if some info is missing.
if you just want to use an older version of npm, you can install it via npm (i know that sounds strange, but its possible)
npm install npm#1.1.21
edit: so you try to install a version of npm which does not exist. just run
npm view npm
and take a look at the property version, to see which versions you could install via npm.
you will see that 1.1.21 does not exist in the registry, which means that you should try to install it via github (see answer by #sakai).
but then you see the next problem. you are using node#0.10.26, and npm 1.1.21 is probably not compatible with node#0.10.x.
so i for myself see basically 2 possible solutions:
Solution 1:
use n (or maybe nvm for switching node-versions back and forth. you could try to install a node#0.8.x version and try to install npm#1.1.21 there, and when done with shrinkwrapping switch back to your current node version.
Solution 2
you could setup some kind of grunt/gulp-task (i hope you use one of them) to run grunt/gulp shrinkwrap, which generates your shrinkwrap.json (via npm shrinkwrap) and when done cleans up your shrinkwrap.json
Try this:
npm install https://github.com/npm/npm/archive/v1.1.21.tar.gz
node ./node_modules/npm/bin/npm-cli.js shrinkwrap
Another—possibly simpler—solution is to just include node_modules into your repo.
Related: Should I check in node_modules to git when creating a node.js app on Heroku?

Categories