npm not installing dependency of dev dependency when it's also listed as app dependency - node.js

This one has me stumped. Very easily reproducible.
package.json
{
"dependencies": {},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-less": "^4.0.1"
}
}
Put that in a folder and run npm install --only=dev. I'm using node v8.9.4 and npm v6.4.1.
Everything will install fine. Open the node_modules directory and verify that the less module is there, because it's a dependency of gulp-less. You can use this gulpfile.js to test:
var gulp = require('gulp');
var less = require('gulp-less');
gulp.task('default')
Okay, now add "less": "^3.8.1" to "dependencies" so your package.json looks like this:
{
"dependencies": {
"less": "^3.8.1"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-less": "^4.0.1"
}
}
Remove the node_modules folder and package lock file and re-run npm install --only=dev.
You will see that you cannot run the gulp file now, and if you open and look for the less package in node_modules it will not have been installed.
This is repeatable behavior for me. Any time I add less to the app dependencies, it is never installed when I do install --only=dev.
Am I missing something about how this is supposed to work or did I find a bug?

Related

Is is possible to install dependancies with single line command?

My Node.js
package.json
has following dependancies
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"mongoose": "^5.3.10",
"nodemon": "^1.18.5"
},
"devDependencies": {
"eslint": "^5.8.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0"
},
I am new to JS,so I want to know if we can somehow invoke package.json without going for
npm install
for every package.
When you use the command npm install or npm i, it will install all dependencies from your package.json.
As a result you get all the dependencies listed in the package.json from the current folder
You can see the documentation concerning this command here : npm install
Welcome to JS world, I'm fairly new to JS myself but I believe there two possible solutions to your problem:
npm install multiple dependencies
It is possible to install multiple dependencies at the same time with one npm install command, you may find more information that could be helpful in the documentation.
For example:
npm install got koa fs-extra
Here I have installed all 3 of my project dependencies at once.
Pre Populate package.json
You can manually add the package dependencies in the correct format to your package.json file. You might the npm documentation on the package.json file helpful.
You then will need to run:
npm install
This will take the dependencies in the package.json file and install them into the node_modules/ directory.

How to ignore scoped packages' node_modules/ directory during npm install?

I have a repository containing a package.json which contains scoped dependencies. I also have an .npmignore file intended to whitelist all files and subdirectories in dist/. The problem is all of the scoped dependencies are included when running npm install #private/a another repository. This includes both private npm packages and public packages such as #uirouter.
package.json:
{
"name": "#private/a",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git#bitbucket.org/private/a.git"
},
"author": "",
"license": "ISC",
"homepage": "https://bitbucket.org/private/a#readme",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-angular-embed-templates": "^2.3.0",
"gulp-concat": "^2.6.1",
"gulp-jshint": "^2.0.4",
"gulp-rename": "^1.2.2",
"gulp-sass": "^3.0.0",
"gulp-uglify": "^2.0.0",
"jshint": "^2.9.4"
},
"dependencies": {
"#private/b": "^1.0.0",
"#private/c": "^1.0.0"
}
}
.npmignore
**
!dist/**
Despite these two files when I run npm install #private/a --save within another repository it is installing the dependency along with all it's scoped dependencies:
/node_modules/#private/a/dist/index.js
/node_modules/dist/css/styles.css
/node_modules/#private/a/node_modules/#private/b
/node_modules/#private/a/node_modules/#private/c
package.json
It should only be this:
/node_modules/#private/a/dist/index.js
/node_modules/dist/css/styles.css
package.json
How can I achieve this? I have tried different variations of the .npmignore but have not had any luck.
.npmignore is irrelevant to what you are trying to do. This file only decides which parts of your npm package code ends up in npm registry. So it is working as advertised.
Your problem must be in your npmconfig or because of using an older version of npm. The latest version installs stuff as so:
/node_modules/#private/a/dist/index.js
/node_modules/#private/b/...
/node_modules/#private/c/...
package.json
I have verified that this is happening with latest npm. But there used to be a time when npm installed dependencies into a nested structure. See this for example. So I suggest:
Making sure you have latest node and npm.
Making sure your npm config is not forcing legacy bundling. Run npm get legacy-bundling. Make sure this is false.
There are few cases where the nesting of dependencies happens legitimately even with the latest npm. See this. But I am guessing your problem is not due to this. You can test by simply doing npm install #private/a in an empty folder.
Node will install your package files along with all the dependencies declared under dependencies field.
How the dependencies tree is build, depends on which version of npm do you use.
If your package doesn't need those dependencies to run, it means they are just dev dependencies and you can safely list them under devDependencies field.
Dev dependencies are only installed when you run an npm install inside the plugin directory.
You need to lock your dependency. You might want to check out npm shrinkwrap.

Wrong package.json path when npm install in react native project

I found when I run npm install at the root of the react native project, it always show me the warn:
npm WARN enoent ENOENT: no such file or directory, open
'/Users/chen/Documents/react-native/project/node_modules/node_modules/package.json'
but we know the package.json should under the node_modules folder
and this is my package.json in the project root
{
"name": "project",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"bundle-ios": "node node_modules/react-native/local-cli/cli.js bundle --entry-file index.ios.js --platform ios --dev false --bundle-output ./ios/bundle/index.ios.jsbundle --assets-dest ./ios/bundle"
},
"dependencies": {
"react": "~15.4.0-rc.4",
"react-native": "0.40.0",
"react-native-elements": "^0.9.0",
"react-native-scrollable-tab-view": "^0.7.0",
"react-native-swiper": "^1.5.4",
"react-native-vector-icons": "^4.0.0"
},
"devDependencies": {
"babel-jest": "18.0.0",
"babel-preset-react-native": "1.9.1",
"jest": "18.1.0",
"react-test-renderer": "~15.4.0-rc.4"
},
"jest": {
"preset": "react-native"
}
}
and everything ok with this json, after run npm install, the library will append in dependencies.
and also has another package.json in node_modules foloder, and this json wont update when I run npm install, the error said it point to node_modules/node_modules/package.json,of course not exist this file
{
"dependencies": {
"react-native-scrollable-tab-view": "^0.7.0",
"react-native-swiper": "^1.5.4",
"react-native-vector-icons": "^4.0.0",
"react-native-elements": "^0.9.0"
}
}
So how's npm install find that wrong path??
package.json module should be present in the root directory of your project. This is, if you want to install all the npm dependencies for your project.
Actually when you runnpm install, it searches for package.json file, and installs all the dependencies written in that file. So, wherever you run npm install , there should be one package.json file in that directory.
So, for simplicity we keep the package.json file at the root of the project, so that npm installs all the dependencies required for that project at one go.
Update:
every node module has its own package.json file and node_modules folder, thats the way it works. package.json contains all the dependencies required for that module,and the node_modules folder will have those modules inside it. Again if you go one more level deep, you will find another package.json file. So, all such package.json contains the dependencies required for that particular module.
Try to go inside the node_modules folder and explore it, you will understand it better.
There is a package.json in the project root.
Then each module under node_modules has one.
But I don't think you should have a package.json in the node_modules folder
/
package.json
/node_modules
<-- no package.json here
/module1
package.json
/module2
package.json

Install and gulp plugins

I have a little trouble while installing Gulp.
In my gulpfile.js I have these require:
var gulp = require('gulp');
var sass = require('gulp-sass');
var browserSync = require('browser-sync');
var del = require('del');
var notify = require("gulp-notify");
var htmlExtended = require("gulp-html-extend");
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var bower = require('gulp-bower');
And in my package I have my Dependencies:
{
"name": "PROJECT",
"version": "0.0.1",
"private": true,
"devDependencies": {
"browser-sync": "^2.11.0",
"del": "^1.2.0",
"gulp": "^3.9.0",
"gulp-bower": "^0.0.11",
"gulp-html-extend": "^1.1.4",
"gulp-imagemin": "^2.3.0",
"gulp-load-plugins": "^1.0.0-rc.1",
"gulp-notify": "^2.2.0",
"gulp-sass": "^2.0.1",
"imagemin-pngquant": "^4.2.0",
"lodash": "^3.9.3"
},
"scripts": {
"install": "bower install"
}
}
The problem is when I start a npm install or npm install --save-dev it installs tons of plugins I didn't ask for. Here is my "node_modules" folder after the installation:
Too many gulp plugins
How do I install only the plugins I asked for in my package.json?
Looks like you are running npm version 3. In version 3, npm changed to have all child dependencies in the top level folder under node_modules. In version 2, npm had a module's child dependencies in another node_modules folder.
To make sure you do not have unnecessary items in that folder, remove all folders within node_modules and run npm install again. This will flush out any folders that have not been added to package.json.
By the way, you can check your npm version with npm -v or npm --version while within your project folder.
Sample npm 2 folder structure:
README.md
node_modules/
dependency_a/
node_modules/
dependency_of_a/
package.json
Sample npm 3 folder structure:
README.md
node_modules/
dependency_a/
dependency_of_a/
package.json

NPM not creating .bin directory

I'm using npm v1.4.4 and node v0.10.25 on Mac OS X 10.9.2.
I've recently upgraded node and npm, and now npm install is no longer creating the .bin directory in node_modules.
I've deleted node_modules, tried npm install again, but the directory and binaries are never created.
Does anybody have any ideas as to why this is happening?
Here is my package.json:
{
"name": "redacted",
"author": {},
"description": "redacted",
"dependencies": {
},
"devDependencies": {
"karma": "*",
"karma-coverage": "0.1.2",
"karma-junit-reporter": "*",
"karma-coffee-preprocessor": "~0.1",
"grunt": "^0.4.2",
"grunt-contrib-requirejs": "^0.4.3",
"grunt-contrib-concat": "^0.3.0",
"grunt-contrib-sass": "^0.7.2",
"grunt-contrib-htmlmin": "^0.2.0",
"grunt-contrib-cssmin": "^0.7.0",
"grunt-contrib-coffee": "^0.10.1",
"grunt-contrib-uglify": "^0.3.3",
"grunt-contrib-jst": "^0.5.1",
"grunt-contrib-qunit": "^0.4.0",
"grunt-contrib-jshint": "^0.8.0",
"grunt-contrib-watch": "^0.5.3",
"grunt-contrib-jasmine": "^0.6.1",
"grunt-contrib-compress": "^0.6.1",
"grunt-contrib-handlebars": "^0.6.1",
"grunt-contrib-less": "^0.9.0",
"grunt-contrib": "^0.9.0"
}
}
I know this is an old post but I experienced the same issue recently. I had copied files from an existing project including package.json and package-lock.json. The package-lock.json was what prevented the node_module/.bin directory from being created.
The solution was to delete the node_modules directory and package-lock.json and run npm install again
The ./node_modules/.bin directory is where npm creates links to a node package's binary. From https://docs.npmjs.com/files/folders#executables
Executables
When in global mode, executables are linked into
{prefix}/bin on Unix, or directly into {prefix} on Windows.
When in local mode, executables are linked into ./node_modules/.bin so
that they can be made available to scripts run through npm. (For
example, so that a test runner will be in the path when you run npm
test.)
The package.json you pasted above do not have a bin section. Take a look at this example from npm's package.json
{
"version": "1.4.9",
"name": "npm",
"publishConfig": {
"proprietary-attribs": false
},
"description": "A package manager for node",
...
...
"main": "./lib/npm.js",
"bin": "./bin/npm-cli.js",
"dependencies": {
"abbrev": "~1.0.4",
"ansi": "~0.2.1",
...
...
Specifically the line "bin": "./bin/npm-cli.js" will tell npm to create a link at ./node_modules/.bin/npm to node_modules/npm/npm-cli.js
Seems that all your dependencies are dev dependencies.
Could you see if your NODE_ENV environment variable is set to production now? If yes you will need to change it back.
Also, any error happened during installation?
If all packages installed, only the .bin is gone.
you can just run npm rebuild
In my case I had webpack running in watch mode in another console window. I did not get any errors during npm install so it took me a moment to notice.
Ensure the dependencies are not in use, such as karma running tests or webpack running in watch mode
Delete the dependency folders, such as node_modules/karma, or the entire node_modules folder. NPM does not seem to create symlink files in .bin folder if dependency folder already exists.
Retry npm install
With NPM 6.7.0.
Not really an answer to your question, but because I had a similar situation: I run npm with the --no-bin-links option on my VM so my windows host doesn't complain. And then later I don't find the bin links folder... duh!
This could happen because of the broken npm. Try following command from the npm troubleshooting and it should just work fine.
curl -L https://www.npmjs.org/install.sh | sh

Resources