Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot find module './options' #2582

Closed
dastrobu opened this issue Nov 13, 2016 · 13 comments
Closed

Error: Cannot find module './options' #2582

dastrobu opened this issue Nov 13, 2016 · 13 comments
Labels
stale this has been inactive for a while...

Comments

@dastrobu
Copy link

when starting mocha the recommended way:

$(npm bin)/mocha

I get the following error:

module.js:474
    throw err;
    ^

Error: Cannot find module './options'
    at Function.Module._resolveFilename (module.js:472:15)
    at Function.Module._load (module.js:420:25)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> ([...]/node_modules/.bin/mocha:12:18)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3)

the error goes away if I start mocha via:

./node_modules/mocha/bin/mocha

Here some system info:

macOS Sierra 10.12.1
node v7.1.0
npm 3.10.9
@ScottFreeCode
Copy link
Contributor

What do you get from the following commands?

  • npm bin
  • ./node_modules/.bin/mocha
  • npm test (with "test": "mocha" in the "scripts" entry in package.json)

@dastrobu
Copy link
Author

npm bin yields the path to the .bin dir inside the node_modules dir of the app: [...]app/node_modules/.bin

./node_modules/.bin/mocha gives the error which I posted above:

module.js:474
    throw err;
    ^

Error: Cannot find module './options'
    at Function.Module._resolveFilename (module.js:472:15)
[...]

npm test with "test": "mocha" gives the same error.

Note that mocha is not installed as global package, it is only a dev dependency of the package and installing it globally is not an option.

@ScottFreeCode
Copy link
Contributor

Hmm... What if you uninstall Mocha and reinstall it, as in #2423?

Note that mocha is not installed as global package, it is only a dev dependency of the package and installing it globally is not an option.

Yeah, we don't recommend global installs anyway. They interfere with local installs and cause confusion as to which version of Mocha is being run.

@dastrobu
Copy link
Author

Actually the hint form #2423 did work. After calling:

npm uninstall -D mocha && npm i -D mocha

running tests via $(npm bin)/mocha works. Unfortunately, I have no idea why.

@ScottFreeCode
Copy link
Contributor

If you can figure out what triggers the options module to go missing in the first place, please let us know.

@colbyjax
Copy link

colbyjax commented Feb 7, 2017

I had the exact same problem. I found that my global mocha install worked correctly, but there was some kind of problem with my local install of mocha.

  • I ran npm test it gave the above './options' error.
  • I ran mocha it worked.
  • I changed into the local node_modules directory and ran mocha it gave the same './options' error appdir/node_modules/.bin/mocha

I resolved, by deleting 'mocha' and '_mocha' from my appdir/node_modules/.bin directory.
npm test now runs the global version of mocha and works as expected.

@sierranevadan
Copy link

I commented on the other issue #2423 ( my comment ) with some suggested fixes. Just wanted to let you know @ScottFreeCode

@drazisil drazisil added the type: bug a defect, confirmed by a maintainer label Mar 30, 2017
@ankur-anand
Copy link

ankur-anand commented May 8, 2017

I'm have the exact same issue. Can See the option file is there in the mocha bin folder still the error was coming.

greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication$ npm test

> node_authentication@1.0.0 test /home/greenarrow/Dropbox/Web DeveLopment/node_authentication
> mocha

module.js:472
    throw err;
    ^

Error: Cannot find module './options'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/greenarrow/Dropbox/Web DeveLopment/node_authentication/node_modules/.bin/mocha:12:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
npm ERR! Test failed.  See above for more details.
greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication$ cd node_modules/mocha/bin/
greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication/node_modules/mocha/bin$ ls
mocha  _mocha  options.js
greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication/node_modules/mocha/bin$ ls -ltr
total 36
-rw-rw-r-- 1 greenarrow greenarrow   710 Oct 24  2016 options.js
-rwxrwxr-x 1 greenarrow greenarrow 12575 Apr 24 14:16 _mocha
-rwxrwxr-x 1 greenarrow greenarrow  2179 Apr 24 14:16 mocha
greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication/node_modules/mocha/bin$ 

I uninstalled it completely from local and there was no global mocha install, too on my system. Cleared npm cache too.

greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication$ npm list -g mocha
/usr/lib
└── (empty)

greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication$ npm ls mocha
node_authentication@1.0.0 /home/greenarrow/Dropbox/Web DeveLopment/node_authentication
└── (empty)

Further even after uninstall the mocha and _mocha was there in the bin directory of node_modules. Which should not be there after uninstall.

greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication/node_modules/.bin$ ./mocha
module.js:472
    throw err;
    ^

Error: Cannot find module './options'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/greenarrow/Dropbox/Web DeveLopment/node_authentication/node_modules/.bin/mocha:12:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication/node_modules/.bin$ 

Seems like mocha var getOptions = require('./options'); here is trying to load the ./options from current directory bin itself.

Checking the long listing shows _mocha and mocha in .bin was present as regular file.

greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication/node_modules$ ls -ltr .bin
......
-rwxrwxr-x 1 greenarrow greenarrow 12575 Apr 24 14:16 _mocha
-rwxrwxr-x 1 greenarrow greenarrow  2179 Apr 24 14:16 mocha

After running npm install mocha again can see now symlink instead of the regular file and the test is running fine.

lrwxrwxrwx 1 greenarrow greenarrow    19 May  8 19:13 _mocha -> ../mocha/bin/_mocha
lrwxrwxrwx 1 greenarrow greenarrow    18 May  8 19:13 mocha -> ../mocha/bin/mocha
greenarrow@Anku:~/Dropbox/Web DeveLopment/node_authentication/node_modules/.bin$ 

Both Mocha version is same for above case mocha version "mocha": "^3.3.0"

So i Tried checking in the old test that was written on some other system by other guys and saw there too it was the same regular file and it was not symlink and it too was throwing the same error when i ran it using npm test.

-rwxrwxr-x 1 hitman hitman 12435 Oct 10  2016 _mocha
-rwxrwxr-x 1 hitman hitman  2153 Oct 10  2016 mocha

mocha package.json has following at gthub

"bin": {
    "mocha": "./bin/mocha",
    "_mocha": "./bin/_mocha"
  },

Current install created Symlink and earlier there was regular file.

@umosys
Copy link

umosys commented Jul 4, 2017

After doing cd node_modules/.bin of my project and rm mocha _mocha, the error vanished.

Note: I also ensured I had mocha installed globally.

@ScottFreeCode ScottFreeCode removed the type: bug a defect, confirmed by a maintainer label Jul 5, 2017
@ScottFreeCode
Copy link
Contributor

@ankur-anand

Further even after uninstall the mocha and _mocha was there in the bin directory of node_modules. Which should not be there after uninstall.

I'd like to point out that this particular detail is an NPM issue rather than a Mocha issue, as Mocha does not control the removal of symlinks or scripts from node_modules/.bin.

@umosys

After doing cd node_modules/.bin of my project and rm mocha _mocha , the error vanished.

Note: I also ensured I had mocha installed globally.

You removed the entry point of the local install and used the global install instead. However, we don't recommend using a global install of Mocha in the first place (there's no significant benefit known to us at this time -- even in this case, reinstalling the local copy should suffice instead -- and there is non-negligible risk that it will result in use of an outdated Mocha version and/or confusion as to which version of Mocha is actually being used). Also, deleting the files from node_modules/.bin instead of uninstalling and/or reinstalling Mocha will basically leave it half installed, and I don't know the possible effects of that.


I've had the chance to confirm that this issue can result from using rsync with options that convert symlinks into regular files (and therefore, by extension, anything else that does the same). I've been thinking about how to address it.

So far I haven't come up with anything I think is a good idea. I've thought of a fairly wide variety of bad ones. Potential solutions vary in robustness, in helpfulness and in the assumptions they make about the package manager that's installing Mocha; I haven't come up with any that are more than marginally helpful, are robust and don't make any assumptions.

One thing that has crossed my mind is that this potentially affects any (especially locally installed) CLI Node program (excepting Windows-only ones, if there even are any of those). Mocha might get hit by it more than others due to greater popularity, but to really resolve the issue in general you'd have to either have every Node CLI program handle this in some way or else change NPM's use of symlinks. So I wonder: is there some expected convention that other programs follow and Mocha just happens to be out of the ordinary for missing it? Or is this something that arguably ought to be fixed in NPM instead of every CLI program having to come up with a way to handle it?

@stale
Copy link

stale bot commented Nov 2, 2017

I am a bot that watches issues for inactivity.
This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue.
Thanks for contributing to Mocha!

@stale stale bot added the stale this has been inactive for a while... label Nov 2, 2017
@stale stale bot closed this as completed Nov 16, 2017
@duhaime
Copy link

duhaime commented Nov 17, 2017

In case other poor souls find themselves here, I had this problem because I had globally installed mocha, and that globally installed module was missing its options companion for some reason.

To resolve this problem I changed the following line in package.json:

"scripts": {
    "test": "mocha ./tests/*.js"
  },

to:

"scripts": {
    "test": "./node_modules/mocha/bin/mocha ./tests/*.js"
  },

Because I could see node_modules/mocha/bin/ contained both mocha and options.js 🐡

@hckia
Copy link

hckia commented Apr 3, 2018

My problem was the same, but the solution was slightly different since it wasn't a global install. Just putting this hear in case the above solution still perplexes people.

I compared two of my existing apps (one that worked) to see what might be different about the .bin/mocha file...

$ ls -lah site-review/node_modules/.bin/mocha
-rwxr-xr-x  1 staff  staff   2.2K Apr  3 07:29 site-review/node_modules/.bin/mocha
$ ls -lah node-jwt-auth/node_modules/.bin/mocha
lrwxr-xr-x  1 staff  staff    18B Mar 31 13:15 node-jwt-auth/node_modules/.bin/mocha -> ../mocha/bin/mocha

notice the symlink?

so inside the first app...

$ cd site-review/node_modules/.bin/
$ rm mocha
$ ln -s ../mocha/bin/mocha mocha

this solved my problem, but also made me think that I had broken symlinks all over the place...

from the broken app...

$ ls -Rla | grep "\->" | wc -l
       1

from the working app...

$ ls -Rla | grep "\->" | wc -l
      27

Now I realized this was an ugly copy. So I started my copy over using...

cp -av node-jwt-auth/* site-review/

so its an exact replica. This is still a pretty ugly thing to do, but it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale this has been inactive for a while...
Projects
None yet
Development

No branches or pull requests

9 participants