Search code examples
node.jsnpmnode-modulesnpm-installtruffle

Error: Cannot find module 'C:\...\node_modules\truffle\build\cli.bundled.js' after installation done


I installed the latest version of truffle by npm install -g truffle. The installation done and this is the last few lines of the result on PowerShell:

npm ERR! code 1
npm ERR! path C:\Users\lenovo\AppData\Roaming\npm\node_modules\truffle\node_modules\bufferutil
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp-build
npm ERR! The process cannot access the file because it is being used by another process.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\lenovo\AppData\Local\npm-cache\_logs\2023-03-10T14_51_51_312Z-debug-0.log

And I tried truffle --version to check if it was installed successfully or not, but this is the result:

PS C:\Users\lenovo> truffle --version
node:internal/modules/cjs/loader:1042
  throw err;
  ^

Error: Cannot find module 'C:\Users\lenovo\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
    at Module._load (node:internal/modules/cjs/loader:885:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v18.13.0

This error throwed because cli.bundled.js module can't be found in the given directory:

Error: Cannot find module 'C:\Users\lenovo\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js'

As I checked, There is not any truffle folder in that directory and so the mentioned module can't be found there. It seems Truffle installation should create truffle folder in that directory but it don't. Is the installation incomplete or it creates that folder in another directory?


Solution

  • The cause of the problem:

    1) As I said in the question, truffle folder doesn't exist in the both directories mentioned by the errors:

    C:\Users\lenovo\AppData\Roaming\npm\node_modules\truffle\node_modules\bufferutil
    

    And

    C:\Users\lenovo\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js
    

    And the installation is incomplete. Because we shouldn't install truffle by the latest version of npm, we should install it by version 6 of npm.

    2) There is an error in the last few lines of installation process (There is in the question):

    npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp-build
    

    That says node-gyp-build module doesn't work or doesn't exist. node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. node-gyp-build is a build tool and bindings loader for node-gyp that supports prebuilds. It seems the system that is installing truffle lacks the both them.

    Solution:

    1. Uninstall Truffle: npm uninstall -g truffle.
    2. Delete all Truffle's files and folders. Some directories like C:\Users\lenovo\AppData\Roaming\npm, C:\Users\lenovo\AppData\Roaming\npm\node_modules, C:\Users\lenovo\AppData\Roaming and so on, may have Truffle's files and folders.
    3. Delete all files associated to node-gyp and node-gyp-build if you have some of them. Some directory like C:\Users\lenovo\AppData\Roaming\npm may have them.
    4. Delete node_modules folder from C:\Users\lenovo\AppData\Roaming\npm.
    5. Change the version of npm to 6: npm install -g npm@6.
    6. Install node-gyp: npm install -g node-gyp.
    7. Install node-gyp-build: npm install -g node-gyp-build.
    8. Install canvas: npm install -g canvas.
    9. Install Solidity compiler: npm install -g solc.
    10. Install Truffle: npm install -g truffle.
    11. Change the version of npm to the latest: npm install -g npm@latest.