question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

npm link hubot causes require of package.json dependencies to fail

See original GitHub issue

Imagine having a checkout of hubot at ~/github/hubot, and you are making a new hubot at ~/github/examplebot`. If you want to work on hubot’s core, and test it in a running bot, you’d run something like:

cd ~/src/hubot
npm link
cd ~/src/examplebot
npm link hubot

After doing this, bin/hubot in example fails. If you recently used the new generator, it’d fail at the first external-script:

$ bin/hubot 
Hubot> [Sun Oct 26 2014 10:23:19 GMT-0400 (EDT)] ERROR Error loading scripts from npm package - Error: Cannot find module 'hubot-diagnostics'
  at Function.Module._resolveFilename (module.js:338:15)
  at Function.Module._load (module.js:280:25)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Robot.loadExternalScripts (/Users/technicalpickles/src/hubot/src/robot.coffee:257:11, <js>:206:27)
  at /Users/technicalpickles/github/hubot/bin/hubot:119:11, <js>:123:26
  at fs.js:266:14
  at Object.oncomplete (fs.js:107:15)

I’ve run into this on and off over the years, but never really understood it. Then, I found this article on NODE_DEBUG: http://www.juliengilli.com/2013/05/26/Using-Node.js-NODE_DEBUG-for-fun-and-profit/

Running bin/hubot NODE_DEBUG=module gave some insight actually. Snipped line:

Module._load REQUEST  hubot-diagnostics parent: /Users/technicalpickles/github/hubot/src/robot.coffee
looking for "hubot-diagnostics" in ["/Users/technicalpickles/github/hubot/src/node_modules","/Users/technicalpickles/github/hubot/node_modules","/Users/technicalpickles/github/node_modules","/Users/technicalpickles/node_modules","/Users/node_modules","/node_modules","/Users/technicalpickles/.node_modules","/Users/technicalpickles/.node_libraries","/opt/boxen/nodenv/versions/v0.10.21/lib/node"]
[Sun Oct 26 2014 10:25:59 GMT-0400 (EDT)] ERROR Error loading scripts from npm package - Error: Cannot find module 'hubot-diagnostics'
  at Function.Module._resolveFilename (module.js:338:15)
  at Function.Module._load (module.js:280:25)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Robot.loadExternalScripts (/Users/technicalpickles/github/hubot/src/robot.coffee:257:11, <js>:206:27)
  at /Users/technicalpickles/github/hubot/bin/hubot:119:11, <js>:123:26
  at fs.js:266:14
  at Object.oncomplete (fs.js:107:15)

If you look at the paths being investigated, none of them include examplebot. I found this description of loading node_modules, which says it tries to find a node_module in the current directory, it’s parent, and keep going all the way up to the root. However, under the hood, npm link uses symlinks, and it looks node gets the real path, rather than the symlinked path, so ~/src/hubot and ~/src/examplebot aren’t in the same hierachy, so won’t ever work.

There are two work around I know of to work with a development hubot:

  • npm install ~/src/hubot to install directly from the checkout. Annoying because you have to run it every change, it’s easy to forget, and does take a few seconds every time
  • git clone hubot into node_modules. You get to see your changes immediately, but it’s risky because it’d be easy to accidentally throw away changes (if package.json of examplebot doesn’t match hubot’s version, it’d reinstall).

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
MrSaintscommented, Jun 29, 2015

I just ran into this issue while working on a local copy of Hubot and a script.

I resolved it by adding the following line to bin/hubot of my personal bot that is running Hubot:

export NODE_PATH=$NODE_PATH:./node_modules

or…

if [ "$NODE_ENV" = "development" ]
then
    export NODE_PATH=$NODE_PATH:./node_modules
fi

It seems to be working fine.

0reactions
technicalpicklescommented, Jul 17, 2015

Actually, the problem with a workaround being in bin/hubot is that is created by generator-hubot so it’s not as easy to update it to get new fixes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

npm install after npm link-ing local module causes error
Show activity on this post. Deleting package-lock. json and then running npm link <package_name><local_package_path> fixed the error.
Read more >
npm-link
Save installed packages to a package.json file as dependencies. When used with the npm rm command, removes the dependency from package.json . Will...
Read more >
4 reasons to avoid using `npm link` - Hiroki Osame
Error-prone with multiple Node.js versions; No fail-case and unexpected fallback to npm registry; Unexpected binary installation; Unexpected ...
Read more >
Why isn't the npm link command working? | Benjamin W Fox
If both your project & package use React (as dependencies or devDependencies in package.json ), you may encounter an error after linking: Error: ......
Read more >
Why Your npm link Commands Might Be Failing
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 ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found