jasmine / jasmine-npm

A jasmine runner for node projects.
MIT License
378 stars 145 forks source link

Package subpath './lib/command.js' is not defined by "exports" using jasmine-xml-reporter #203

Closed mitaly closed 1 year ago

mitaly commented 1 year ago

I am using jasmine-xml-reporter that refers to command.js file of Jasmine package.

Command used for running jasmine test

nyc ./node_modules/jasmine-xml-reporter/bin/jasmine.js --junitreport --output=reports/unit-test --filePrefix=junit-report --config=./jasmine.json

Jasmine.json config file

{ "spec_dir": "./tests", "spec_files": [ "**/*[sS]pec.ts" ], "helpers": ["./tests/helpers/reporter.js"], "stopSpecOnExpectationFailure": false, "random": false }

Error while running tests

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/command.js' is not defined by "exports" in D:\Library\chargeragentcommunication\node_modules\jasmine\package.json at new NodeError (node:internal/errors:372:5) at throwExportsNotFound (node:internal/modules/esm/resolve:472:9) at packageExportsResolve (node:internal/modules/esm/resolve:753:3) at resolveExports (node:internal/modules/cjs/loader:482:36) at Function.Module._findPath (node:internal/modules/cjs/loader:522:31) at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27) at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (D:\Library\chargeragentcommunication\node_modules\@cspotcode\source-map-support\source-map-support.js:811:30) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) { code: 'ERR_PACKAGE_PATH_NOT_EXPORTED' }

File contents of jasmine-xml-reporter/bin/jasmine.js

var path = require('path'),
Command = require('jasmine/lib/command.js'),
Jasmine = require('jasmine/lib/jasmine.js');

var jasmine = new Jasmine({ projectBaseDir: path.resolve() });
var examplesDir = path.resolve('jasmine-core', 'example', 'node_example');
var command = new Command(path.resolve(), examplesDir, console.log);

require('jasmine-xml-reporter/boot.js');

command.run(jasmine, process.argv.slice(2));

Versions used:

"jasmine-xml-reporter": "^1.2.1" "jasmine": "^4.0.0"

sgravrock commented 1 year ago

Command isn't part of the public interface, and it can no longer be accessed as of 4.0. Jasmine is part of the public interface but needs to be accessed through the default export. We added the exports entry, which prevents subpath imports, partly for better compatibility with various tools and partly to reduce the risk of people accidentally depending on private APIs that might change at any time.

Unfortunately, while that change makes it less likely that packages will depend on internal implementation details in the future, it breaks packages like jasmine-xml-reporter that already did so.

(In fairness to the author of jasmine-xml-reporter, that wasn't a particularly unreasonable thing to do back in 2016. We didn't clearly document the public API of the jasmine package back then. And even if we had, the idea that packages should only depend on the documented public interfaces of other packages wasn't well established.)

You can hook up the JUnitXmlReporter from jasmine-reporters yourself in a few different ways:

I hope this helps.