简体   繁体   中英

npm install : specify package.json?

How can I tell npm to use another package.json when running "npm install" ?

All I need is npm install -f packages-win32.json Or is there a trick or another approach to achieve the same?

Because not all npm modules are cross-platform and I'd like to use other packages per platform.

You cannot specify a different package.json file as the specs are literally only for a file called package.json .

If you have some issues with packages that only work on either os try them out with

try {
  thing = require('thing');
}
catch( error ) {
  thing = require('other');
}

You can also sniff out the os via:

const _isWin = /^win/.test( process.platform );

Or use os.platform() if you don't have to support node <= 5...

Maybe that helps?

The npm command doesn't allow specifying a specific package.json file but here's work-around to install specific or all package.json files:

Create npm-install.sh file with the source below and run with this command:

source npm-install.sh

or:

bash npm-install.sh

#!/bin/bash
set +ex;

cp -f package.json temp;
echo "Installing all package-*.json...";

for File in *.json; do
  echo -e "\nFile: $File";
  mv -f $File package.json;
  npm install;
done

cp -f temp package.json;
rm -f temp;
#EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM