如何通过流星中使用的库从相对路​​径读取路径

时间:2018-01-04 08:00:02

标签: node.js meteor

我正试图在meteor项目中使用anyNpmPackage进行测试。

我已完成meteor npm install anyNpmPackage

在我的package.js中我有

Package.onTest(function(api) {
    api.use([
      'anyNpmPackage'
    ]);
    api.addFiles('tests/server/methods.js', 'server');
});

现在anyNpmPackage内部使用anyOtherNpmPackage

anyOtherNpmPackage的行是

myModule = fs.readFileSync(require.resolve("../include/module.js"));

当我进行流星测试时

我收到错误Error: Cannot find module '../include/module.js'

当我在该行上方进行调试时

console.log(fs.readdirSync('../../'))结果为[ 'server', 'web.browser' ]console.log(__dirname)产生/node_modules/jsonpath/lib

其中jsonpathanyOtherNpmPackage

此问题的解决方法是什么?

1 个答案:

答案 0 :(得分:0)

当Meteor套餐依赖于 npm 套餐时,您应该使用Npm.depends

修改 我在Meteor应用程序中创建了以下包,它可以工作:

package.js

Package.describe({
  name: 'npmtest',
  version: '0.0.1',
  // Brief, one-line summary of the package.
  summary: '',
  // URL to the Git repository containing the source code for this package.
  git: '',
  // By default, Meteor will default to using README.md for documentation.
  // To avoid submitting documentation, set this field to null.
  documentation: 'README.md'
});

Package.onUse(function(api) {
  api.versionsFrom('1.5.1');
  api.use('ecmascript');
  api.mainModule('npmtest.js');

});

Package.onTest(function(api) {
  api.use('ecmascript');
  api.use('tinytest');
  api.use('npmtest');
  api.mainModule('npmtest-tests.js');
});

Npm.depends({
    'jsonpath':'1.0.0'
})

npmtest-tests.js

// Import Tinytest from the tinytest Meteor package.
import { Tinytest } from "meteor/tinytest";

// Import and rename a variable exported by npmtest.js.
import { name as packageName } from "meteor/npmtest";
const jp = Npm.require("jsonpath");
// Write your tests here!
// Here is an example.
Tinytest.add('npmtest - example', function (test) {
  console.log("here")
  const cities = [
    { name: "London", "population": 8615246 },
    { name: "Berlin", "population": 3517424 },
    { name: "Madrid", "population": 3165235 },
    { name: "Rome",   "population": 2870528 }
  ];
  const names = jp.query(cities, '$..name');

  test.equal(names[0], "London");
});

<强> EDIT2: 注意:我从未使用npm install