使用chai-http进行测试时出现ERR_PACKAGE_PATH_NOT_EXPORTED错误的求助
使用chai-http进行测试时出现ERR_PACKAGE_PATH_NOT_EXPORTED错误的求助
我在使用chai-http做Node.js项目测试的时候碰到了个棘手的问题,不管是执行npm test还是直接运行mocha命令,都会抛出ERR_PACKAGE_PATH_NOT_EXPORTED的错误,具体报错信息如下:
Exception during run: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\...\node_modules\chai-http\package.json at exportsNotFound (node:internal/modules/esm/resolve:314:10) at packageExportsResolve (node:internal/modules/esm/resolve:605:13) at resolveExports (node:internal/modules/cjs/loader:638:36) at Function._findPath (node:internal/modules/cjs/loader:743:31) at Function._resolveFilename (node:internal/modules/cjs/loader:1230:27) at Function._load (node:internal/modules/cjs/loader:1070:27) at TracingChannel.traceSync (node:diagnostics_channel:322:14) at wrapModuleLoad (node:internal/modules/cjs/loader:217:24) at Module.require (node:internal/modules/cjs/loader:1335:12) at require (node:internal/modules/helpers:136:16) at Object. ...
我的测试代码如下:
/* eslint-disable no-undef */ const chai = require('chai') const routes = require('../src/config/enums/routes.enum') const env = require('../src/config/environments') const app = require('../App') const chaiHttp = require('chai-http'); chai.use(chaiHttp) const expect = chai.expect; const endpoint = env.url; const PORT = process.env.APP_PORT; describe('POST ' + routes.USERS, () => { it('Debería retornar lo que se le mandó al post', async () => { let server; before(() => { server = app.listen(PORT, () => console.log("Running server for test")) }); after(() => { server.close(); }); request(app) .post(endpoint + routes.USERS) .send({ nickname: "osmaldym", name: "Osmaldy", password: "secret", }) .end((err, res) => { expect(res.status).to.equal(200); expect(res.body).to.have.property('data'); done(); }); }) })
我自己琢磨了下,怀疑这个错误是因为chai-http的package.json里虽然指向了它的index.js文件,但它本身用的是ES模块规范,却没有做好CommonJS的兼容转换导致的?不过这只是我的猜测,有没有大佬能帮忙分析下问题出在哪,或者给个解决方向呀?
备注:内容来源于stack exchange,提问作者osmaldym
相关产品推荐
相关产品推荐

