版本有:
nvm --version 0.35.3
npm -v: 6.14.8
node -v: v14.10.0
重要的package.json部分:
{
"main": "index.js",
"scripts": {
"start": "./node_modules/mocha/bin/mocha index.js",
"install": "bash install.sh",
"clear": "rm -R -f node_modules && rm package-lock.json"
},
"dependencies": {
"chromedriver": "^84.0.1",
"dotenv": "^8.2.0",
"mocha": "^8.1.2",
"selenium-webdriver": "^4.0.0-alpha.7"
}
}
index.js:
require('dotenv').config()
const password = process.env.PASSWORD;
const selenium = require('selenium-selenium');
const chrome = require('selenium-selenium/chrome');
const chromedriver = require('chromedriver');
describe('Init driver and login', () => {
//... Should run through as an empty test but does not
});
以及我运行时遇到的错误
mocha index.js
或 npm run start
:
Error: Cannot find module 'selenium-selenium'
Require stack:
- /home/mts/Desktop/projects/selenium/index.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/mocha.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/one-and-dones.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/options.js
- /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/bin/mocha
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:893:15)
at Function.Module._load (internal/modules/cjs/loader.js:743:27)
at Module.require (internal/modules/cjs/loader.js:965:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/home/mts/Desktop/projects/selenium-moco/index.js:11:18)
at Module._compile (internal/modules/cjs/loader.js:1076:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:941:32)
at Function.Module._load (internal/modules/cjs/loader.js:782:14)
at Module.require (internal/modules/cjs/loader.js:965:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.exports.requireOrImport (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js:20:12)
at Object.exports.loadFilesAsync (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/mocha.js:427:19)
at singleRun (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run-helpers.js:156:15)
at exports.runMocha (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run-helpers.js:225:10)
at Object.exports.handler (/home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/lib/cli/run.js:366:11)
at /home/mts/.nvm/versions/node/v14.10.0/lib/node_modules/mocha/node_modules/yargs/lib/command.js:241:49
尽可能简单,但找不到模块 - 为什么?我应该尝试尽可能低的版本吗?我想避免兼容性地狱,所以我只使用可用的最新版本。
我认为您在代码中使用了不正确的包名称 -
selenium-selenium
。该软件包在 npmjs.org 上不可用。
尝试更改我们代码中的这一行:
const selenium = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');