如何在NPM中正确使用本地路径并在Node.js中使用require方法

时间:2016-01-07 18:16:33

标签: javascript node.js npm

我在尝试清理一些代码时遇到了问题。

以下是我尝试从pageObject目录导入某些文件的代码部分,这些LOC位于我的所有测试文件中:

var config = require('../../../../adminVariables');
var AdminLogin = require('../../../../pageObject/admin/login/index');
var HeaderProfile = require('../../../../pageObject/admin/login/common/headerProfile/index');
var AccountSettings = require('../../../../pageObject/admin/login/accountSettings/index');

adminVariables和index都是javascript文件。

它看起来非常难看,我希望通过减少所有../../../(也是我的老板不喜欢它)来最小化它以使其更具可读性和优雅性。

我已尝试按照此处找到的NPM文档:https://docs.npmjs.com/files/package.json#local-paths

但我对使用npm install --save有点困惑。我的页面对象目录中没有package.json文件。只是简单的js文件,它们创建我在测试脚本中引用的类和方法。我收到这个错误:

npm ERR! eisdir EISDIR: illegal operation on a directory, read
npm ERR! eisdir This is most likely not a problem with npm itself
npm ERR! eisdir and is related to npm not being able to find a package.json in
npm ERR! eisdir a package you are trying to install.

我想也许我可以手动完成,例如将package.json改为:

{
  "dependencies": {
      "admin-variables": "file:./qe/adminVariables",
      "admin-login": "file:./qe/pageObject/admin/login/index",
      "header-profile": "file:./qe/pageObject/admin/login/common/headerProfile/index",
      "account-settings": "file:./qe/pageObject/admin/login/accountSettings/index"
  } 
}

然后将之前的LOC更改为:

var config = require('admin-variables');
var AdminLogin = require('admin-login');
var HeaderProfile = require('header-profile');
var AccountSettings = require('account-settings');

但是当我尝试运行我的测试脚本时,我收到以下错误:

module.js:340
    throw err;
    ^

Error: Cannot find module 'admin-variables'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:289:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (/Users/username/go/src/bitbucket.org/companyname/platform/qe/test/admin/dashboard/adminUser/pdvi-2361.js:7:14)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at /Users/username/go/src/bitbucket.org/companyname/platform/qe/node_modules/mocha/lib/mocha.js:216:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/username/go/src/bitbucket.org/companyname/platform/qe/node_modules/mocha/lib/mocha.js:213:14)
    at Mocha.run (/Users/username/go/src/bitbucket.org/companyname/platform/qe/node_modules/mocha/lib/mocha.js:453:10)
    at Object.<anonymous> (/Users/username/go/src/bitbucket.org/companyname/platform/qe/node_modules/mocha/bin/_mocha:393:18)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:138:18)
    at node.js:974:3

npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/Cellar/node/5.1.1/bin/node" "/usr/local/bin/npm" "run" "env-production" "mocha" "test/admin/dashboard/adminUser/pdvi-2361.js" "--" "--reporter" "spec" "--slow" "0"
npm ERR! node v5.1.1
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! ui-tests@1.0.0 env-production: `env envVar='production' "mocha" "test/admin/dashboard/adminUser/pdvi-2361.js" "--reporter" "spec" "--slow" "0"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ui-tests@1.0.0 env-production script 'env envVar='production' "mocha" "test/admin/dashboard/adminUser/pdvi-2361.js" "--reporter" "spec" "--slow" "0"'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ui-tests package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     env envVar='production' "mocha" "test/admin/dashboard/adminUser/pdvi-2361.js" "--reporter" "spec" "--slow" "0"
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs ui-tests
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls ui-tests
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/username/go/src/bitbucket.org/companyname/platform/qe/npm-debug.log

npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/Cellar/node/5.1.1/bin/node" "/usr/local/bin/npm" "run" "p-2361:spec"
npm ERR! node v5.1.1
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! ui-tests@1.0.0 p-2361:spec: `npm run env-production mocha test/admin/dashboard/adminUser/pdvi-2361.js -- --reporter spec --slow 0`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ui-tests@1.0.0 p-2361:spec script 'npm run env-production mocha test/admin/dashboard/adminUser/pdvi-2361.js -- --reporter spec --slow 0'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the ui-tests package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     npm run env-production mocha test/admin/dashboard/adminUser/pdvi-2361.js -- --reporter spec --slow 0
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs ui-tests
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls ui-tests
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/username/go/src/bitbucket.org/companyname/platform/qe/npm-debug.log

这是我工作的目录结构(主要是qe /文件夹)。

platform/
  qe/
    node_modules/
    pageObject/
      admin/
        login/
            accountSettings/
              index.js
            common/
              headerProfile/
                index.js
            dashboard/
              index.js
            index.js
    test/
      admin/
        dashboard/
          adminUser/
            pdvi-2361.js
            pdvi-2372.js
            pdvi-2377.js
    adminVariables.json
    package.json

以下是我的页面对象的示例:

var webdriver = require('selenium-webdriver');
var config = require('../../../adminVariables');

var adminVar = process.env.envVar;
var login = config[adminVar].adminLogin;
var user = config[adminVar].role.adminUser.name;
var email = config[adminVar].role.adminUser.email;
var pw = config[adminVar].role.adminUser.password;


class AdminLogin {
  constructor(driver) {
    this.driver = driver;
    this.driver.manage().timeouts().implicitlyWait(2000);

    this.emailField = this.driver.findElement({
      css: 'input[name="email"]'
    });

    this.passwordField = this.driver.findElement({
      css: 'input[name="password"]'
    });

    this.loginButton = this.driver.findElement({
      css: 'button[type="submit"]'
    });
  }

  fillEmail(email) {
    this.emailField.sendKeys(email);
  }

  fillPassword(password) {
    this.passwordField.sendKeys(password);
  }

  signin() {
    this.loginButton.click();
  }
}

module.exports = AdminLogin;

在我的代码中需要这些本地json / javascript文件的正确方法是什么?

0 个答案:

没有答案