Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm link not working on windows?

I'm using node v0.10.32. Windows 8.1.

My objective is to link a node application as a node_module in another main app.

I go to my-module folder and do

npm link

Then, I go to the main-app folder and do

npm link my-module

This is the result

c:\dev\main-app>npm link my-module
unbuild [email protected]
c:\dev\main-app\node_modules\my-module ->     C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module -> C:\dev\my-module

But, the linkage does NOT seem to work, require('my-module') throws the following error

    c:\dev\main-app>node app.js

    module.js:340
        throw err;
              ^
    Error: Cannot find module 'my-module'
        at Function.Module._resolveFilename (module.js:338:15)
  • my-module is indeed v0.0.2.
  • I can access it from main-app/node_module/my-module
  • This folder exists C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module
  • my-module package.json has "name": "my-module"

Moreover, %NODE_PATH% is correctly set:

  c:\dev\main-app>echo %NODE_PATH%
  C:\Users\Nizar\AppData\Roaming\npm\node_modules

Ideas?

like image 250
Nizar Blond Avatar asked Jan 15 '16 16:01

Nizar Blond


People also ask

Why npm link not working?

To fix this, use node -v in each project and check the running version of node. Use nvm install x.x.x & nvm use x.x.x to match them up. Afterward, delete your node_modules , delete your package-lock. json , re-run npm i , and re-run npm link in your dependency folder and npm link myDependency in your project folder.

Why npm is not working on Windows?

The Npm command not found error can appear when you install or upgrade npm. On Windows, the cause of this error could be that a PATH or system variable is not correctly set. The error can also occur if you do not have npm or Node. js installed, have an outdated version, or have permission issues.

How does npm link work?

npm link just creates two symbolic links. When you run npm link in a module's root directory, npm creates a symbolic link from your “global node_modules” directory to the local module's directory. The “global node_modules” directory is a special directory where all modules installed with npm install -g are stored.

How do I link npm locally?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.


1 Answers

There are a few things to try. On Windows, npm link is handled by creating junction points. Issuing a dir node_modules command should result in a line like:

01/15/2016 11:02 AM <JUNCTION> my-module [C:\Users\Nizar\AppData\Roaming\npm\node_modules\my-module]

Assuming that's there, then the problem is more than likely:

  1. A lack of an index.js file (which is the default filename node uses to resolve modules)
  2. You're using a different file than index.js as the main file of your module, in which case you need to tell node what that file is, by using the main key in your package.json file.

For example (taken from here):

{
  "name": "node-js-sample",
  "version": "0.2.0",
  "description": "A sample Node.js app using Express 4",
  "main": "index.js", // <-- LIKE THIS
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.13.3"
  },
  "engines": {
    "node": "4.0.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/heroku/node-js-sample"
  },
  "keywords": [
    "node",
    "heroku",
    "express"
  ],
  "author": "Mark Pundsack",
  "contributors": [
    "Zeke Sikelianos <[email protected]> (http://zeke.sikelianos.com)"
  ],
  "license": "MIT"
}
like image 184
rossipedia Avatar answered Sep 30 '22 18:09

rossipedia