bit icon indicating copy to clipboard operation
bit copied to clipboard

Error: `generateNodeEnvModulesPattern is not a function` with custom Node env

Open piotrkulpinski opened this issue 1 year ago • 4 comments

Hi,

I've created a custom Node Env in my workspace based on the default bitdev.node/node-env with the following command:

bit create node-env node --aspect bitdev.node/node-env --scope curiousleaf.envs

Now, if I set this env for a component, I can no longer test it correctly due to the following error:

✖ service "tester" of env "curiousleaf.envs/node" has failed. error: generateNodeEnvModulesPattern is not a function

Any ideas why this might be happening?

Specifications

  • Bit version: 1.5.29
  • Workspace type: (harmony/legacy) harmony
  • Node version: 20.9.0
  • pnpm version: 8.12.1
  • Platform: MacOS

piotrkulpinski avatar Dec 28 '23 12:12 piotrkulpinski

Can you please run the same command with --log=error and then paste the error stack trace?

benjgil avatar Dec 28 '23 13:12 benjgil

err: {
      "type": "TypeError",
      "message": "generateNodeEnvModulesPattern is not a function",
      "stack":
          TypeError: generateNodeEnvModulesPattern is not a function
              at Object.<anonymous> (/.../Sites/curiousleaf-design/node_modules/.pnpm/@[email protected]_@teambit+legacy@node_modules+@teambit+legacy_@testing-library+rea_pemsownk6rjdmc7hjsg2v2gosu/node_modules/@bitdev/node.node-env/dist/config/jest.config.js:24:60)
              at Module._compile (node:internal/modules/cjs/loader:1376:14)
              at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
              at Module.load (node:internal/modules/cjs/loader:1207:32)
              at Module._load (node:internal/modules/cjs/loader:1023:12)
              at module.constructor.require (/.../.bvm/versions/1.5.29/bit-1.5.29/node_modules/@teambit/bit/dist/hook-require.js:27:31)
              at require (node:internal/modules/helpers:176:18)
              at JestTester.<anonymous> (/.../Sites/curiousleaf-design/node_modules/.pnpm/@[email protected]_@teambit+legacy@node_modules+@teambit+legacy_@[email protected]/node_modules/@teambit/defender.jest-tester/dist/jest-tester.js:155:32)
              at Generator.next (<anonymous>)
              at /.../Sites/curiousleaf-design/node_modules/.pnpm/@[email protected]_@teambit+legacy@node_modules+@teambit+legacy_@[email protected]/node_modules/@teambit/defender.jest-tester/dist/jest-tester.js:8:71
              at new Promise (<anonymous>)
              at __awaiter (/.../Sites/curiousleaf-design/node_modules/.pnpm/@[email protected]_@teambit+legacy@node_modules+@teambit+legacy_@[email protected]/node_modules/@teambit/defender.jest-tester/dist/jest-tester.js:4:12)
              at JestTester.test (/.../Sites/curiousleaf-design/node_modules/.pnpm/@[email protected]_@teambit+legacy@node_modules+@teambit+legacy_@[email protected]/node_modules/@teambit/defender.jest-tester/dist/jest-tester.js:120:16)
              at TesterService.run (/.../.bvm/versions/1.5.29/bit-1.5.29/node_modules/@teambit/tester/dist/tester.service.js:176:34)
              at async /.../.bvm/versions/1.5.29/bit-1.5.29/node_modules/@teambit/envs/dist/runtime/runtime.js:97:31
              at async pMapSeries (/.../.bvm/versions/1.5.29/bit-1.5.29/node_modules/p-map-series/index.js:9:15)
    }
teambit.envs/envs, service "tester" of env "curiousleaf.envs/node" has failed. error: generateNodeEnvModulesPattern is not a function
✖ service "tester" of env "curiousleaf.envs/node" has failed. error: generateNodeEnvModulesPattern is not a function

piotrkulpinski avatar Dec 28 '23 14:12 piotrkulpinski

same thing happening to me; have you find a solution in the meantime? I created my own env because I wanted to customize the eslint config

/Edit

I found a solution for it to work on my side, but some correction should be done on the template.

So I noticed that even if I removed the call to generateNodeModulesPattern, its still appeared in the errors. And my change to my eslint config werent doing anything either. So its seems to me, that my custom env still link to the files of the original env.

in ts-env.bit-env.ts, I added a constructor and reinitialized the paths, referencing a custom react-env I had created some time ago :

  constructor() {
    super();
    this.tsconfigPath = require.resolve('./config/tsconfig.json');
    this.tsTypesPath = './types';
    this.jestConfigPath = require.resolve('./config/jest.config');
    this.eslintConfigPath = require.resolve('./config/eslintrc.js');
    this.eslintExtensions = ['.ts', '.tsx', '.js', '.jsx', '.mjs'];
    this.prettierConfigPath = require.resolve('./config/prettier.config.js');
  }

Im kinda new to bit, and Im not an expert on compilation either, so Im not entirely sure, but from what I understand, NodeEnv extends ReactEnv which declares and initialize those variables, and when you create a new React-env, your env.bit-env.ts initialize those variables, and theyre resolved correcly. But when you create a node-env, the variables are not reinitilized (still in the super class react-env, which is not in the same directory) so they do not resolve to the right emplacement.

Wanted to share my thought so it could help to correct the template!

MagnesiumDev avatar Jan 27 '24 23:01 MagnesiumDev

@MagnesiumDev Thanks for pointing this out - the pointers are indeed not portable between envs, and the env template should have its own local file references. @piotrkulpinski that's also what's causing your issue. The jest config in your custom env was actually correct, but as the env didnt have its own internal config paths it was resolving to the config from the base NodeEnv, which has a bug i'm just pushing a fix for.

Thanks to both of you for reporting

benjgil avatar Jan 28 '24 19:01 benjgil

This has been fixed from v 1.0.15 of bitdev.node/node-env closing

benjgil avatar Feb 25 '24 08:02 benjgil