npmnode-modulesnpm-installnpm-link

npm link command is not working after I installed a package globally but want to run the command in the local project


  1. I installed a package "cowsay" globally using npm i -g cowsay.
  2. I ran the "cowsay" command in the terminal as cowsay helloooo and it ran without any error producing output:

< hellooooo >

    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||
  1. in my local Project javascript file, I typed the code const cowsay = require("cowsay"), when i try to run the node "index.js", again produced the error: Cannot find module cowsay

  2. Finally used the command node link cowsay, the error is:

*node:internal/modules/cjs/loader:1078
  throw err;
  ^

Error: Cannot find module 'F:\Web Development\JavaScript\Node\Jokester\link'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Module._load (node:internal/modules/cjs/loader:920: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.16.0*

I was trying to install cowsay package globally and run it in my local Project using npm link <module_name> but the following error is thrown:

*node:internal/modules/cjs/loader:1078
  throw err;
  ^

Error: Cannot find module 'F:\Web Development\JavaScript\Node\Jokester\link'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Module._load (node:internal/modules/cjs/loader:920: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.16.0*

Solution

  • The issue you are running into stems from incorrect link command.

    Instead of node link ... run npm link ....

    Here are the proper steps to link a globally installed module for use in a local project:

    1. Globally install the cowsay module:
    npm install -g cowsay
    
    1. In your local project directory, link the global cowsay:
    npm link cowsay
    

    NOTE linking is done using npm cli which uses nodejs

    This will symlink the global module into your local node modules folder, but usually you don't need to link global modules, they are automatically found. You might have to reinstall your nodejs.