4

Node.js v6.11.2, npm v3.10.10, selenium-webdriver 3.5.0, on Windows 7

Every time I try to npm install <--save> selenium-webdriver, I get the following warning:

F:\Program Files\nodejs>npm install --save selenium-webdriver
npm WARN saveError ENOENT: no such file or directory, open 'F:\Program Files\nodejs\package.json'
F:\Program Files\nodejs
`-- selenium-webdriver@3.5.0

npm WARN enoent ENOENT: no such file or directory, open 'F:\Program Files\nodejs\package.json'
npm WARN nodejs No description
npm WARN nodejs No repository field.
npm WARN nodejs No README data
npm WARN nodejs No license field.

It's right, there is no F:\Program Files\nodejs\package.json file. Multiple re-installs of node.js (I tried a couple of versions), and there is NEVER a package.json at that location.

Still, an npm list makes it look like selenium-webdriver is there:

...
| +-- validate-npm-package-name@2.2.2
| | `-- builtins@0.0.7
| +-- which@1.2.11
| | `-- isexe@1.1.2
| +-- wrappy@1.0.2
| `-- write-file-atomic@1.2.0
`-- **selenium-webdriver@3.5.0**
  +-- jszip@3.1.4
...

Still, whenever I try to run a test that requires selenium-webdriver (with a command like: 'node myfile.js'), module.js:471 throws the error in the title "Error: Cannot find module 'selenium-webdriver'". Grrrrr.

I'm new to this stuff (pretty obvious, eh?), but I have spent more than a day looking around the web, and have found no relevant information. There are similar questions here, but not quite the same. Anybody know how to fix this? (Please?)

mired
  • 51
  • 1
  • 1
  • 4
  • Where is myfile.js located? You might want to run `npm i` in the project root instead of the node.JS Folder, see more here: https://stackoverflow.com/questions/19578796/what-is-the-save-option-for-npm-install – LW001 Aug 31 '17 at 23:04
  • You're right; that fixed it! It seems counter-intuitive to me to install a tool in the tests directory, rather than with the other tools... But it worked. There's no more 'Can't find selenium-webdriver' error. Thank you very much!! – mired Aug 31 '17 at 23:17

1 Answers1

10

npm has two ways of installing packages

Global packages

You used npm install -g packagename for this. You use such installs for tools that you will be using commonly across multiple projects. Like yarn or babel etc.

Local packages

This is for packages that are related to your project. You want them to be download in you current project only. So that it doesn't impact any other project.

You use npm install <package> for this. You don't need a package.json file if you are installing the package this way. But when you use

npm install --save selenium-webdriver

This tells npm that you want to install the package and you also want to update your package.json with that package. The package is still installed locally, but for it to update package.json, it needs to exists

That is where npm init comes into picture to initialize your project and create a package.json file inside it.

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265