Search code examples
javascriptnode.jsnpmnpm-installyarnpkg

`yarn add` and `npm install --save` both does not install packages to local project


I forked a React Native-derived framework project which the original author has recently stopped maintaining. The original author had made another project, which is an installer script for the framework. so I forked this too.
The latter worked perfect, but the former one had a little problem, so I updated the framework's React Native version to latest.

But after updating, installer doesn't work.
Below is core part of the installer's main code. Full Code

function installDesktopPackage() {
  let rndPackage = PACKAGE;

  console.log(`Installing ${rndPackage}...`);
  const pkgmgr = Common.isGlobalCliUsingYarn(process.cwd()) ? 'yarn add' : 'npm install --save';
  const execOptions = {stdio: 'inherit'}; // use {stdio: 'inherit'} for verbose
  execSync(`${pkgmgr} ${rndPackage}`, execOptions);
  console.log(chalk.green(`${rndPackage} successfully installed.`));
}

function runDesktopFilesGenerationScript() {

  const generateDesktop = require(REACT_NATIVE_DESKTOP_GENERATE_PATH());
  generateDesktop(process.cwd(), Common.getReactNativeAppName());
}

As you can see, installDesktopPackage should add framework files into local project, and runDesktopFilesGenerationScript should install files into right location.

But when I ran installer code, this error occurs:

Cannot find module '/home/jihoo/Desktop/test/node_modules/react-native-desktop-qt/local-cli/generate-desktop.js'                                                                                        
Require stack:                                                                                      
- /home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/src/cli.js              
- /home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/bin.js
Error: Cannot find module '/home/jihoo/Desktop/test/node_modules/react-native-desktop-qt/local-cli/generate-desktop.js'
Require stack:
- /home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/src/cli.js
- /home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/bin.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at runDesktopFilesGenerationScript (/home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/src/cli.js:34:27)
    at /home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/src/cli.js:44:5
    at Object.<anonymous> (/home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/src/cli.js:50:3)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/src/cli.js',
    '/home/jihoo/.npm/_npx/41097/lib/node_modules/react-native-desktop-qt-init/bin.js'
  ]
}

Yes, I found it that installDesktopPackage did not added any files, including {PROJECT_PATH}/node_modules/react-native-desktop-qt/local-cli/generate-desktop.js which is needed by runDesktopFilesGenerationScript

So I tried yarn add and npm install --save manually, but both doesn't add any files to {PROJECT_PATH}/node_modules/

I tried cleaning cache of npm and Yarn, and retried, but the result is same.

My specs are:
CentOS 8
Qt 5.12.2
Node.js 14.15.4 LTS
npm 6.14.10
Yarn 1.22.10
React Native 0.63.4
react-native-cli 2.0.1
JetBrains WebStorm 2020.3


Solution

  • I solved it. I accidently broke packages.json of my project so script was installing frameworks in wrong path. I fixed it.