DEV Community

lamianbu
lamianbu

Posted on • Edited on

Say goodbye to ‘npm install’, no need ‘npm install’, throw away ‘npm install’.

Say goodbye to npm install, no need npm install, throw away npm install.

example

In your project,

  1. add npx -y ai-install to package.json->scripts->start/build.
{
  "name": "ai-install-demo",
  "scripts": {
    "start": "npx -y ai-install && xxx serve --open",
    "build": "npx -y ai-install && xxx build"
  },
  "devDependencies": {
    "xxx": "x.y.z"
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. add a new file npm-start.sh, the content is:
npm start 
Enter fullscreen mode Exit fullscreen mode

After git clone, just double click npm-start.sh to start, no typing command.

because

npx -y ai-install
Enter fullscreen mode Exit fullscreen mode

executing npm install if not installed.

npx run an arbitrary command from an npm package (either one installed locally, or fetched remotely).

think

Does it save a second of typing npm i? no!

After the installation of npm i, it was not seamlessly connected to npm start, which wasted 10 ~ 60 seconds.

If you use npx -y ai-install, then npm start will likely start a minute earlier.

yarn & pnpm

npx -y ai-install
Enter fullscreen mode Exit fullscreen mode

In yarn project, auto executing yarn install if not installed.

In pnpm project, auto executing pnpm install if not installed.

src

package.json defined a commond ai-install.

{
  "name": "ai-install",
  "version": "1.0.0",
  "description": "Say goodbye to `npm install`, no need `npm install`, throw away `npm install`.",
  "main": "index.js",
  "bin": {
    "ai-install": "index.js"
  }
}
Enter fullscreen mode Exit fullscreen mode

index.js executing npm/yarm/pnpm install if not installed.

#!/usr/bin/env node

var fs = require('fs');
var child_process = require('child_process');

if (!fs.existsSync('node_modules')) {
  if(fs.existsSync('yarn.lock')){
    child_process.execSync('yarn install', { stdio: 'inherit' });
  } else if (fs.existsSync('pnpm-lock.yaml')){
    child_process.execSync('pnpm install', { stdio: 'inherit' });
  } else {
    child_process.execSync('npm install', { stdio: 'inherit' });
  }
}
Enter fullscreen mode Exit fullscreen mode

ref

ai-install npm

ai-install github

Top comments (4)

Collapse
 
zirkelc profile image
Chris Cook

It doesn't handle Monorepos with multiple node_modules folder.

Collapse
 
lamianbu profile image
lamianbu

I think Monorepos is only one node_modules folder.

if there are many node_modules, you can add 'npx -y ai-install' in every scripts or run 'npx -y ai-install' at every package.json dir.

Collapse
 
moopet profile image
Ben Sinclair

This only checks for the presence of a node_modules directory, so you'll still have to manually run the install command after pulling changes or switching branches or whatnot.

Collapse
 
lamianbu profile image
lamianbu

It is suitable for small projects and package.json does not change frequently.