IBM/openapi-validator

Ajv/dist/core and ajv/dist/compile/codegen modules not found

arichiardi opened this issue · 10 comments

Dear all,
we are using this validator and running into the following:

Error: Cannot find module 'ajv/dist/core'
Require stack:
- /home/cokap/git/cohesic/acuity/node_modules/ajv-draft-04/dist/index.js
- /home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-functions/dist/schema/ajv.js
- /home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-functions/dist/schema/index.js
- /home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-functions/dist/index.js
- /home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-ruleset-migrator/dist/transformers/rules.js
- /home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-ruleset-migrator/dist/transformers/index.js
- /home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-ruleset-migrator/dist/index.js
- /home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-cli/dist/services/linter/utils/getRuleset.js
- /home/cokap/git/cohesic/acuity/node_modules/ibm-openapi-validator/src/spectral/spectral-validator.js
- /home/cokap/git/cohesic/acuity/node_modules/ibm-openapi-validator/src/cli-validator/run-validator.js
- /home/cokap/git/cohesic/acuity/node_modules/ibm-openapi-validator/src/cli-validator/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    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)
    at Object.<anonymous> (/home/cokap/git/cohesic/acuity/node_modules/ajv-draft-04/dist/index.js:4:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/cokap/git/cohesic/acuity/node_modules/ajv-draft-04/dist/index.js',
    '/home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-functions/dist/schema/ajv.js',
    '/home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-functions/dist/schema/index.js',
    '/home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-functions/dist/index.js',
    '/home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-ruleset-migrator/dist/transformers/rules.js',
    '/home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-ruleset-migrator/dist/transformers/index.js',
    '/home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-ruleset-migrator/dist/index.js',
    '/home/cokap/git/cohesic/acuity/node_modules/@stoplight/spectral-cli/dist/services/linter/utils/getRuleset.js',
    '/home/cokap/git/cohesic/acuity/node_modules/ibm-openapi-validator/src/spectral/spectral-validator.js',
    '/home/cokap/git/cohesic/acuity/node_modules/ibm-openapi-validator/src/cli-validator/run-validator.js',
    '/home/cokap/git/cohesic/acuity/node_modules/ibm-openapi-validator/src/cli-validator/index.js'
  ]
}

This spectral commit might be the culprit.

Any advice on how to work around it?

@arichiardi What command are you running that results in this error? I have not been able to reproduce

We were running something like:

npx lint-openapi --rules=xyz -v -s -e openapi/openapi.yaml

Hmm. I still cannot reproduce. Can you reproduce the issue running Spectral on its own against your API definition? It's possible it is a Spectral bug.

Just to piggy back off this issue since we facing similar error when calling validator inside a node app:

code

const serviceAPI = '/file/path/to/openapi.yaml';
const fs = require('fs');
const yaml = require('js-yaml');

const data = yaml.load(fs.readFileSync(`${serviceAPI}`, { encoding: 'utf-8' }));
console.log(data);
const validator = require('ibm-openapi-validator');

validator(data)
    .then(validationResults => {
        console.log(JSON.stringify(validationResults, null, 2));
        throw (validationResults);
    });

error

Error: Cannot find module 'ajv/dist/compile/codegen'
Require stack:
- /Users/markkulube/node_app/node_modules/ajv-errors/dist/index.js
- /Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/validation/ajv.js
- /Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/validation/assertions.js
- /Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/validation/index.js
- /Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/ruleset.js
- /Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/spectral.js
- /Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/index.js
- /Users/markkulube/node_app/node_modules/ibm-openapi-validator/src/spectral/spectral-validator.js
- /Users/markkulube/node_app/node_modules/ibm-openapi-validator/src/lib/index.js
- /Users/markkulube/node_app/services/projects-api/api_validator.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    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)
    at Object.<anonymous> (/Users/markkulube/node_app/node_modules/ajv-errors/dist/index.js:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/markkulube/node_app/node_modules/ajv-errors/dist/index.js',
    '/Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/validation/ajv.js',
    '/Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/validation/assertions.js',
    '/Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/validation/index.js',
    '/Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/ruleset/ruleset.js',
    '/Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/spectral.js',
    '/Users/markkulube/node_app/node_modules/@stoplight/spectral-core/dist/index.js',
    '/Users/markkulube/node_app/node_modules/ibm-openapi-validator/src/spectral/spectral-validator.js',
    '/Users/markkulube/node_app/node_modules/ibm-openapi-validator/src/lib/index.js',
    '/Users/markkulube/node_app/services/projects-api/api_validator.js'

This same error is fired when invoking command line by exec inside mocha test suite. Approx code

 try {
            exec('lint-openapi /file/path/to/openapi.yaml', (error, stdout, stderr) => {
                if (error) {
                    throw (error.message);
                }
                if (stderr) {
                    console.log(`stderr: ${stderr}`);
                }
                console.error('stdout: ', stdout);
                done();
            }); 
        } catch (error) {
            console.log('stderr: ', error.message);
        }

@arichiardi

Could you maybe update issue title to ajv/dist/core and ajv/dist/compile/codegen modules not found?

Done! Apologies for not getting back to you but this one is unfortunately very low in our priority list at the moment. I am successfully using the (pinned) previous version though!

@arichiardi @markkulube are you still seeing the problem with the latest version of the validator (v1.0.5)?

I tried and it does not happen here anymore with version 1.0.5.

A side question - is there a tool to convert from validaterc.json to spectral.yaml semi-automatically?

Unfortunately, there is not. We talked about writing one during the development of v1.0 but didn't have the bandwidth to fit it in.

We do have a migration guide to assist in the manual conversion, though: https://github.com/IBM/openapi-validator/blob/main/Migration-Guide.md

It seems this issue has been resolved.