最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - chai-http error Exception during run: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main de

programmeradmin4浏览0评论

I have a problem using chai-http for testing, when I use npm test or mocha I get this error

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.
    ...

And here is the test code:

/* 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();
            });
    })
})

I think this error throws cause when I look the C:\...\node_modules\chai-http\package.json file points to his index.js, and uses ES Modules but it doesn't convert to commonjs, anyway I don't know

I have a problem using chai-http for testing, when I use npm test or mocha I get this error

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.
    ...

And here is the test code:

/* 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();
            });
    })
})

I think this error throws cause when I look the C:\...\node_modules\chai-http\package.json file points to his index.js, and uses ES Modules but it doesn't convert to commonjs, anyway I don't know

Share Improve this question asked 2 days ago OsmaldyOsmaldy 111 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your suspicion about ESM is right. In chai-http 5.0.0 moved to ESM and dropped CJS support.

You could import it instead of requiring it:

import chaiHttp from "chai-http";

However this might require reworking your code into a module, and might be more effort than it's worth.

If you want to stick to using require, you can explicitly install an older version of chai-http that still provides CJS:

npm install [email protected]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论