I have a an application which is a web game server and say for example I have node_modules which I use in directory ./ and I have a proper package.json for those. it happens that in directory ./public/ I have a website being served which itself uses node_modules and also has a proper package.json for itself.
I know I can do this by navigating the directories. But is there a command or way to automate this so that it is easier for other developers to bootstrap the application in their system?
By default, npm install will install all modules listed as dependencies in package. json .
To create a default package. json using information extracted from the current directory, use the npm init command with the --yes or -y flag.
Installing multiple packages using package. When you run the npm install command without specifying any package name, then npm will look for an existing package. json file in the current working directory. npm will install all packages listed as dependencies of the project.
To install multiple packages, we need to use the npm install followed by the multiple package names separated by the spaces package1 package2 . This above command installs three packages, which are express, cors and body-parser. You can also checkout how to install the specific version of an npm package.
Assuming you are on Linux/OSX, you could try something like this:
find ./apps/* -maxdepth 1 -name package.json -execdir npm install \;
Arguments:
./apps/* - the path to search. I would advise being very specific here to avoid it picking up package.json files in other node_modules directories (see maxdepth below).
-maxdepth 1 - Only traverse a depth of 1 (i.e. the current directory - don't go into subdirectories) in the search path
-name package.json - the filename to match in the search
-execdir npm install \; - for each result in the search, run npm install in the directory that holds the file (in this case package.json). Note that the backslash escaping the semicolon has to be escaped itself in the JSON file.
Put this in the postinstall hook in your root package.json and it will run everytime you do an npm install:
"scripts": {
"postinstall": "find ./apps/* -name package.json -maxdepth 1 -execdir npm install \\;"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With