When I npm install my node application, my the packages list in the dependencies property of my package.json are installed. But, for some reason some of those dependencies are not installing their sub-dependencies. In other words, there is no node_modules folder with the dependencies of my dependencies.
myproject
- node_modules
- my-package
- node_modules (would expect this to be here, but it's not)
The strange thing is that is another project, the sub-dependencies are being installed for the same packages.
Even when I try to manually install a single package via npm install my-package, that packages node_modules are not installed.
Is there a reason why this might be the case? Or a way I can debug this?
Thanks
NPM tries to flatten your dependencies at the root level. If is a version that satisfies all of your dependencies(Either only one package the dependency, or the version satisfies all package requirements as defined in package.json) it will roll it up to the root of your node_modules. This is done intentionally, so that you do not installed the same dependency multiple times.
The exception to this rule occurs if there are conflicting versions of a module e.g. package1 has dependency a version 1.0.1 and package2 has dependency a version 2.
I opened a similar question and was brought back to this one where I see a lack of an actual solution.
I've currently managed to find it and the correct one to get your package to install its own node_modules is to add the following to your package.json:
"bundledDependencies": [
"npm-package"
]
To that array, add the packages you want to be installed in the node_modules folder of my-package:
myproject
- node_modules
- my-package
- node_modules <-- This folder will contain the packages of the array
So, an example of package json could be:
{
"name": "my-package",
"version": "1.0.0",
"dependencies": {
"cheerio": "^1.0.0-rc.10",
"jsdom": "^19.0.0",
"yargs": "13.2"
},
"bundledDependencies": [
"yargs",
"jsdom"
]
}
When you will install my-package inside one other project it will have it's own node_modules with the packages specified in bundledDependencies.
Related
Every time I create a create-react-app project it throws this error:
The react-scripts package provided by Create React App requires a dependency:
"babel-loader": "8.0.4"
Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-loader was detected higher up in the tree:
/Users/dalegrant/Desktop/hyberchat-dashboard/client/node_modules/babel-loader (version: 8.0.5)
I have tried downgrading to 8.0.4,
I've tried installing 8.0.5
I've even made sure all my dependencies for babel rely on a 8.0.5!
I have no idea what to do from here as the error persists for every create react app I now build!
This must be a common problem, does anybody have an idea on how to get around this?!
In your machine user folder there is a node_modules directory and package-lock.json remove them using rm -rf User/node_modules and rm -rf User/package-lock.json after that delete node_module directory and pack-lock.json file in your project run npm install again then it's works
Remove the babel-loader package (also globally):
npm uninstall babel-loader
npm uninstall babel-loader -g
Then run create-react-app again, it should install the correct dependency.
If you use the babel-loader somewhere else, pin the dependency there to ensure this problem does not happen again.
you probably installed node_modules in your main folders before. Search for them and remove.
In my case i have installed node_modules on my Home folder. When i removed files from this directory problem disapeared
I have two local npm projects, A and B.
Let's say A depends on react and has it listed as a dependency in the package.json file. I use npm link to make A available for B.
B depends on A and react. I use npm link A to use the local version of A. However, as I know that A already depends on react, I do not include react in my package.json file of B.
When I call npm install I would expect npm to include transitive dependencies in the node_modules folder. However, I cannot find react in it nor can I use it in B's code as long as I don't include the transitive dependency in B's own package.json file.
When I include other modules like react, I also get transitive dependencies like fbjs or encoding resolved into my node_modules folder.
So, where is the mistake? / Why are the dependencies of A not included in B's node_modules folder?
I got the same behaviour with npm 5.6. After a downgrade of npm to 4.6 version, "npm install" ran right. After this I figured out the problem was a package-lock.json committed by a teammates of mine and not synchronized to package.json
So i start off with writing my own node.js app and the only thing i want is to include a package for saml.
So i was wondering what is the minimal requirement for my app.
I was just creating a node.js file and then i installed the package by:
node install some-saml-passports-package.
I got this warning after installation:
npm WARN enoent ENOENT: no such file or directory, open '.../mynodeapp/package.json'
I removed the package and created a package.json file. Which results in a parsing error because there was no content inside.
I have read that I only need a package.json when I plan to create my own package. Also I read something about local and global installation of package files in npm. Well, i need a bit of clarification for my doing.
What is the use of package.json?
Do I need it when I only want to create a standalone app in node with one dependency to an existing package?
Is it recommended even if I don't need it at all?
There is no minimum requirement for node.js application. The package.json is only required to manage your application for dependencies, module descriptions , handle npm scripts etc. You can install packages without package.json, just don't use the --save flag with npm install. E.g.:
npm install <package_name> - you can still run your node application without any problem. But I will suggest you to add a package.json file and add following:
{
"name": "<package_name>",
"version": "<version>", //0.0.1 for starters
"description": "",
"main": "<entry point of application>", // example: app.js , index.js etc
,
"author": "<your name>",
"license": "ISC",
"dependencies": {}
}
You can create this by adding it manually or just execute npm init in your project root directory and answer some basic questions in console.
Here are some useful links:
npm init
how to use package.json
The minimal file is:
{
}
Now you can start using commands like npm install web3 --save and they will save onto this file.
Create package.json via npm init command.
Package.json contains data about your project and what is more important for standalone app - it contains dependencies list. You can install all dependencies from package.json with npm install.
If you want to install package and save it in package.json type npm install package-name --save.
When I use the npm install 6 month ago and install module it create new one folder under the node_modules, the folder with all files for this module, but now when I use it create multifile files for one module, how can I install a module to specific folder as before ?
I use webStrorm
Your dependencies will now be installed maximally flat. Insofar as is
possible, all of your dependencies, and their dependencies, and THEIR
dependencies will be installed in your project's node_modules folder
with no nesting.
Yes, in npm 3 your dependencies will be installed maximally flat, so it's all about npm versions.
Try running npm -v to make sure of that.
Here's an article about that.
Can anyone advise what I'm doing wrong here (i'm new to grunt) a colleage has supplied me with a base gruntfile setup
I have installed node,js, and grunt but I cant figure out how to install the various packages (uglify, concat etc) - which command should I use for this?
The first question you must ask yourself is, "what are the names of the grunt dependencies?"
The quick answer:
Simply execute the npm install <package-name> for each package you wish to install. For the most part, I consider the package-names to be equivalent to their github name. ( grunt-contrib-concat for example. )
npm install grunt-contrib-concat will install it, then go on to the next.
Improving Maintainability:
For optimal maintainability and cross developer/environment support you should consider using package.json to list your dependencies.
Once you have all of your dependencies listed in package.json, you can then run npm install and all of them will be installed should the need ever arise. (For development based dependency vs runtime/production based dependency management, read on!)
Best Practices:
The optimal workflow with node js is to use npm init first, so that your package.json file is created for you. then once you have a package.json, you can install each of your dependencies/devDependencies by either adding them to their respective lists in the json file or adding them to their respective lists on the fly by using npm install <package name> --save or --save dev respectivly.
NOTE: IF you are experimenting, and you'd rather NOT save a node module to your dependency list, then a simple npm install <package name> will do.
At this point, I do recommend searching for npm package.json best practices - as you will find some valuable information regarding your workflow for nodejs. Dependency management can be amazing, but only if you know how to really take advantage of the utilities provided.
Consider the following excerpt from a package.json file:
"dependencies": {
"backbone": "*",
"underscore": "~*",
"requirejs": "*"
"jquery": "*"
},
"devDependencies": {
"grunt": "*",
"grunt-contrib-uglify": "*",
"grunt-contrib-concat": "*",
"grunt-contrib-copy": "*"
},
The example we see is that the npm packages under dependencies will install when running npm install --production, the development packages will be installed automatically when running npm install --development,
NOTE: You can change your node environment as well which modifies the usage of a simple npm install with no flags.
For the most part, grunt is used for development, so I believe it would be more common to find anything related to grunt being in devDependencies
A sample package.json:
http://browsenpm.org/package.json
if you have package.json with devDependencies (or dependencies) defined, all you need to run is npm install then grunt
Otherwise run something like this to install and save them to your package.json
npm install --save-dev <package-name>