1

Some quite complex problem I am having here that I can't find a solution :

Context

I would like to use the ts version of the library algebrite : https://github.com/davidedc/Algebrite

But I required also some internal types and structure, so I forked it, and compiled it with tsc instead of bazel, because I was not able to make it generate the declaration files... Here my fork : https://github.com/hl037/Algebrite/tree/SO_question

The repo I am working on is a private one, so I can only post some excerp.

For testing, we use Mocha's testname.spec.ts, executed with mocha -r ts-node/register 'tests/**/*.spec.ts'

I use pnpm as package manager.

Problem

When I install algebrite locally, and use pnpm ln -g / pnpm ln -g algebrite to install it in the private repo, the Mocha test passes.

But when I use pnpm add github:hl037/Algebrite#SO_question (actually #master, but I created a branch to freeze the repo content for this question in case I try other things), then I get this error :

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /repo/tests/testname.spec.ts
    at new NodeError (node:internal/errors:371:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:105:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
    at async Loader.import (node:internal/modules/esm/loader:177:17)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async formattedImport (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/nodejs/esm-utils.js:38:28)
    at async Object.exports.loadFilesAsync (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/cli/run.js:370:5)

This error is not meaningful at all, and actually completely misleading. I had to inspect mocha execution to find the true one which is masked in node_modules/mocha/lib/nodejs/esm-utils.js:33 :

exports.requireOrImport = async file => {
  if (path.extname(file) === '.mjs') {
    return formattedImport(file);
  }
  try {
    return dealWithExports(await formattedImport(file));
  } catch (err) {
    if (
      err.code === 'ERR_MODULE_NOT_FOUND' ||
      err.code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
      err.code === 'ERR_UNSUPPORTED_DIR_IMPORT'
    ) {
      try {
        // Importing a file usually works, but the resolution of `import` is the ESM
        // resolution algorithm, and not the CJS resolution algorithm. We may have
        // failed because we tried the ESM resolution, so we try to `require` it.
        return require(file);
      } catch (requireErr) {
        if (
          requireErr.code === 'ERR_REQUIRE_ESM' ||
          (requireErr instanceof SyntaxError &&
            requireErr
              .toString()
              .includes('Cannot use import statement outside a module'))
        ) {
          // ERR_REQUIRE_ESM happens when the test file is a JS file, but via type:module is actually ESM,
          // AND has an import to a file that doesn't exist.
          // This throws an `ERR_MODULE_NOT_FOUND` error above,
          // and when we try to `require` it here, it throws an `ERR_REQUIRE_ESM`.
          // What we want to do is throw the original error (the `ERR_MODULE_NOT_FOUND`),
          // and not the `ERR_REQUIRE_ESM` error, which is a red herring.
          //
          // SyntaxError happens when in an edge case: when we're using an ESM loader that loads
          // a `test.ts` file (i.e. unrecognized extension), and that file includes an unknown
          // import (which throws an ERR_MODULE_NOT_FOUND). `require`-ing it will throw the
          // syntax error, because we cannot require a file that has `import`-s.
          throw err;
        } else {
          throw requireErr;
        }
      }
    } else {
      throw err;
    }
  }
};

The real error is :


/repo/node_modules/.pnpm/github.com+hl037+Algebrite@0c4b3796055f2766c4da408_l733cdhqtfuxjtcwx5qyxbl4jm/node_modules/algebrite/dist/index.js:8
import { caadr, caar, cadadr, caddddr, cadddr, caddr, cadr, car, cdadr, cdar, cdddr, cddr, cdr, CONS, Cons, DOUBLE, Double, isadd, iscons, isdouble, isfactorial, ismultiply, isNumericAtom, ispower, isrational, isstr, issymbol, istensor, NUM, Num, STR, Str, SYM, Sym, TENSOR, Tensor, version, cadar, cddar, caaddr, caddar, cdaddr, cddddr, cadaddr, cddaddr, caddadr, cdddaddr, caddaddr, } from './runtime/defs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .js] (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1608:43)
    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)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/repo/src/main/AstProcessing/Algebrite.ts:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1621:12)
    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)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/repo/src/main/Functions/Math.ts:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1621:12)
    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)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/repo/src/main/Functions/Functions.ts:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1621:12)
    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)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/repo/src/main/Functions/Runtime.ts:8:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1621:12)
    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)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/repo/tests/tests/testname.spec.ts:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Module.m._compile (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/repo/node_modules/.pnpm/ts-node@10.9.1_typescript@4.7.4/node_modules/ts-node/src/index.ts:1621:12)
    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)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.exports.requireOrImport (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/nodejs/esm-utils.js:49:16)
    at async Object.exports.loadFilesAsync (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/repo/node_modules/.pnpm/mocha@10.0.0/node_modules/mocha/lib/cli/run.js:370:5)

Important note : When algebrite is installed with pnpm ln, The first error actually pop up, but the require(file) in esm-utils.js does not throw anything.

What I tried so far

I tried to add "type" : "module" in the package.json of algebrite, but then I have this error :

 Error [ERR_REQUIRE_ESM]: require() of ES Module /repo/node_modules/.pnpm/github.com+hl037+Algebrite@0c4b3796055f2766c4da408_l733cdhqtfuxjtcwx5qyxbl4jm/node_modules/algebrite/dist/index.js from /repo/src/main/AstProcessing/Algebrite.ts not supported.
Instead change the require of index.js in /repo/src/main/AstProcessing/Algebrite.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.require.extensions.<computed> [as .js] (index.ts:1608:43)
    at Object.<anonymous> (Algebrite.ts? [sm]:1:1)
    at Module.m._compile (index.ts:1618:23)
    at Object.require.extensions.<computed> [as .ts] (index.ts:1621:12)
    at Object.<anonymous> (Math.ts? [sm]:2:1)
    at Module.m._compile (index.ts:1618:23)
    at Object.require.extensions.<computed> [as .ts] (index.ts:1621:12)
    at Object.<anonymous> (Functions.ts? [sm]:5:1)
    at Module.m._compile (index.ts:1618:23)
    at Object.require.extensions.<computed> [as .ts] (index.ts:1621:12)
    at Object.<anonymous> (Runtime.ts? [sm]:8:1)
    at Module.m._compile (index.ts:1618:23)
    at Object.require.extensions.<computed> [as .ts] (index.ts:1621:12)
    at Object.<anonymous> (SignatureMatching.spec.ts? [sm]:5:1)
    at Module.m._compile (index.ts:1618:23)
    at Object.require.extensions.<computed> [as .ts] (index.ts:1621:12)
    at Object.exports.requireOrImport (esm-utils.js:49:16)
    at async Object.exports.loadFilesAsync (esm-utils.js:91:20)
    at async singleRun (run-helpers.js:125:3)
    at async Object.exports.handler (run.js:370:5)

...Which seems logical since the code call require(file) as you can see in the portion of esm-utils.js I copy pasted before.

Questions

First, what could I do to make the mocha test working when algebrite is installed from git ?

Then, Why could make the require(file) work with pnpm ln and not work when installed from git ?

hl037_
  • 3,520
  • 1
  • 27
  • 58

0 Answers0