What is the best way to run npm install for nested folders

0 votes

What is the most correct way to install npm packages in nested sub folders?

my-app
  /my-sub-module
  package.json
package.json

What is the best way to have packages in /my-sub-module be installed automatically when npm install run in my-app?

Jul 17, 2020 in Node-js by kartik
• 37,520 points
14,790 views

1 answer to this question.

0 votes

Hello @kartik,

If you want to run a single command to install npm packages in nested subfolders, you can run a script via npm and main package.json in your root directory. The script will visit every subdirectory and run npm install.

Below is a .js script that will achieve the desired result:

var fs = require('fs')
var resolve = require('path').resolve
var join = require('path').join
var cp = require('child_process')
var os = require('os')

// get library path
var lib = resolve(__dirname, '../lib/')

fs.readdirSync(lib)
  .forEach(function (mod) {
    var modPath = join(lib, mod)
// ensure path has package.json
if (!fs.existsSync(join(modPath, 'package.json'))) return

// npm binary based on OS
var npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm'

// install folder
cp.spawn(npmCmd, ['i'], { env: process.env, cwd: modPath, stdio: 'inherit' })
})

Note that this is an example taken from a StrongLoop article that specifically addresses a modular node.js project structure (including nested components and package.json files).

Hope it helps!!

Thank You!!

answered Jul 17, 2020 by Niroj
• 82,840 points

Related Questions In Node-js

0 votes
0 answers

What is the best way to trigger change or input event in react js?

With the help of code and example ...READ MORE

19 hours ago in Node-js by Nidhi
• 8,520 points
13 views
0 votes
0 answers

What is the best way to handle e and props in React TypeScript?

With the help of example  and code ...READ MORE

12 hours ago in Node-js by Nidhi
• 8,520 points
16 views
0 votes
1 answer

What is the process to parse JSON using Node.js?

You can parse JSON data using the ...READ MORE

answered Feb 10 in Node-js by Navya
37 views
0 votes
1 answer

What is the correct method to clone a JavaScript object?

Cloning a JavaScript object can be achieved ...READ MORE

answered Feb 10 in Node-js by Navya
55 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,840 points
4,390 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
3,431 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
1,260 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
1,138 views
0 votes
1 answer

How to install a previous exact version of a NPM package?

Hello @kartik, If you have to install an ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
2,883 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,840 points
10,202 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP