How to install npm for the current directory, as well as subdirectories with package.json files?

I have an application that is a web game server, and say, for example, that I have node_modules that I use in the directory. /, and I have the appropriate package.json for them. it happens that in the directory. / public / I have a website that uses node_modules itself, and also has its own package.json for itself.

I know that I can do this by going through the directories. But is there a team or a way to automate this so that it is easier for other developers to download the application to their system?

+8
source share
2 answers

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 the search. I would advise being very specific here to avoid it collecting package.json files in other node_modules directories (see Maxdepth below).

-maxdepth 1 - move only at depth 1 (that is, the current directory - do not go to subdirectories) in the search path

-name package.json - file name to search in

-execdir npm install \; - for each search result, run npm install in the directory in which the file is stored (in this case package.json). Note that a backslash that resets a semicolon must be escaped in a JSON file.

Put this in the hook postinstall in your root.json and it will run every time you install npm:

 "scripts": { "postinstall": "find ./apps/* -name package.json -maxdepth 1 -execdir npm install \\;" } 
+13
source share

For cross-platform support (including Windows), you can try my solution. Clean Node.js

Run it as a "preinstall" npm script

 const path = require('path') const fs = require('fs') const child_process = require('child_process') const root = process.cwd() npm_install_recursive(root) function npm_install_recursive(folder) { const has_package_json = fs.existsSync(path.join(folder, 'package.json')) if (!has_package_json && path.basename(folder) !== 'code') { return } // Since this script is intended to be run as a "preinstall" command, // skip the root folder, because it will be `npm install`ed in the end. if (has_package_json) { if (folder === root) { console.log('===================================================================') console.log(`Performing "npm install" inside root folder`) console.log('===================================================================') } else { console.log('===================================================================') console.log(`Performing "npm install" inside ${folder === root ? 'root folder' : './' + path.relative(root, folder)}`) console.log('===================================================================') } npm_install(folder) } for (let subfolder of subfolders(folder)) { npm_install_recursive(subfolder) } } function npm_install(where) { child_process.execSync('npm install', { cwd: where, env: process.env, stdio: 'inherit' }) } function subfolders(folder) { return fs.readdirSync(folder) .filter(subfolder => fs.statSync(path.join(folder, subfolder)).isDirectory()) .filter(subfolder => subfolder !== 'node_modules' && subfolder[0] !== '.') .map(subfolder => path.join(folder, subfolder)) } 
0
source share

All Articles