Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: MODULE_NOT_FOUND (undefined) Error: Cannot find module 'dotenv' #2185

Closed
jtlimo opened this issue May 10, 2020 · 18 comments
Closed

Error: MODULE_NOT_FOUND (undefined) Error: Cannot find module 'dotenv' #2185

jtlimo opened this issue May 10, 2020 · 18 comments
Labels
🐛 Bug Something isn't working ☠ stale Marked as stale by the stale bot, will be removed after a certain time.

Comments

@jtlimo
Copy link

jtlimo commented May 10, 2020

Summary

Hi, I'm trying to use stryker with typescript. But when running stryker throws this error:
Error: MODULE_NOT_FOUND (undefined) Error: Cannot find module 'dotenv'

Stryker config

module.exports = function(config){
  config.set({
     mutator: 'typescript',
    mutate: [
      './components/<component-name>/**/*.ts',
      '!./components/<component-name>/_tests/*.ts',
      '!./components/<component-name>/**/*.unit.test.ts',
      '!./components/<component-name>/**/*.integration.test.ts',
      '!./components/<component-name>/index.ts',
      '!./components/<component-name>/infrastructure/**/*.ts',
      '!./components/<component-name>/lib/*',
      '!./components/<component-name>/<filename>.ts',
    ],
    packageManager: 'npm',
    reporters: ['clear-text', 'progress', 'html'],
    testRunner: 'mocha',
    // transpilers: ['typescript'], ---> I comment the transpiler because we currently use ts-node
    tsconfigFile: 'tsconfig.json',
    testFramework: 'mocha',
    coverageAnalysis: 'off',
    maxConcurrentTestRunners: 2,
  });
}

Test runner config

I'm using mocha configuration at package.json

"mocha": {
    "timeout": 50000,
    "reporter": "progress",
    "exit": true,
    "ignore": [
      "**/node_modules/**/*"
    ],
    "require": [
      "ts-node/register/transpile-only",
      "source-map-support/register",
      "./modules/test-utils/MochaBootstrap.js",
      "./modules/test-utils/mocha.env.js"
    ]
  }

Stryker environment

├─┬ @stryker-mutator/core@3.1.0
│ ├─┬ @stryker-mutator/api@3.1.0
│ ├── @stryker-mutator/util@3.1.0
├─┬ @stryker-mutator/mocha-runner@3.1.0
│ ├── @stryker-mutator/api@3.1.0 deduped
├─┬ @stryker-mutator/typescript@3.1.0
│ ├── @stryker-mutator/api@3.1.0 deduped
│ ├── @stryker-mutator/util@3.1.0 deduped

Test runner environment

+-- mocha@7.0.1

Test command

npx stryker run

Your Environment

software version(s)
node 10.14.1
npm 6.4.1
Operating System Linux - ArchLinux

Add stryker.log

stryker.log

@jtlimo jtlimo added the 🐛 Bug Something isn't working label May 10, 2020
@nicojs
Copy link
Member

nicojs commented May 14, 2020

Hi @jtlimo 👋 thanks for opening this issue.

I did a quick search in our repo, but we're not using dotenv anywhere in our codebase. I conclude that it must be something specific to your setup. Could you please try to find where it is used?

@jtlimo
Copy link
Author

jtlimo commented May 15, 2020

Hi @nicojs, dotenv is used at a specific file, called environmentVariables.ts. This file has the below code:

if (env !== 'production') {
  require('dotenv').config({ path: dotenvPath });
}

I thinking about stryker loading this file, probably I need to not load this file at mutate, right?

@HonzaMac
Copy link

This happen to me as well on first setup.

I think main problem could be, that this package was not copied to stryker temp directory. Please check if node_modules in sandbox contain this package.

@jtlimo
Copy link
Author

jtlimo commented May 27, 2020

Hi @HonzaMac , actually the dotenv package is not on node_modules at sandbox, but I don't have this dependency in my package.json.
In the project, we use lerna to create modules at our monolith system, and the dotenv is a dependency at one of modules.

The stryker probably does not recognize others package.json? It's possible to have a workaround for this situation? 🤔

@jtlimo
Copy link
Author

jtlimo commented May 27, 2020

Hi @HonzaMac , actually the dotenv package is not on node_modules at sandbox, but I don't have this dependency in my package.json.
In the project, we use lerna to create modules at our monolith system, and the dotenv is a dependency at one of modules.

The stryker probably does not recognize others package.json? It's possible to have a workaround for this situation?

I discover more about this, and our project basically do this:

  • in modules/api-config has a package.json with dotenv dependency.
  • we use lerna and they installs dotenv at api; although dotenv does not necessary to have on package.json global;

@jtlimo
Copy link
Author

jtlimo commented Jun 8, 2020

@nicojs Do you have some solution for this?

I put dotEnv into root package.json, but we have another modules/packages created by lerna... like this one:

@enterprise-name/package-name

and stryker doesn't recognize them. I'm really sad with that. 😿

@nicojs
Copy link
Member

nicojs commented Jun 8, 2020

As far as I know, the only way to let require('dotenv') not load from the node_modules directory (or one of its parent directories node_modules) is by setting the NODE_PATH environment variable. See https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

Could you please try to figure out the following things?

  • What does the test script in your package.json look like?
  • Can you find the modules directory in your sandbox?
  • Can you cd into your sandbox and run normal mocha (npm test) there? If not, try to figure out what's wrong.

Sorry, it's hard to debug these kinds of things from remote. If you can provide a small reproduction repository with a similar setup, I would be able to help more.

@jtlimo
Copy link
Author

jtlimo commented Jun 8, 2020

As far as I know, the only way to let require('dotenv') not load from the node_modules directory (or one of its parent directories node_modules) is by setting the NODE_PATH environment variable. See https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders

Could you please try to figure out the following things?

  • What does the test script in your package.json look like?
  • Can you find the modules directory in your sandbox?
  • Can you cd into your sandbox and run normal mocha (npm test) there? If not, try to figure out what's wrong.

Sorry, it's hard to debug these kinds of things from remote. If you can provide a small reproduction repository with a similar setup, I would be able to help more.

  • package.json test script --> "NODE_ENV=${NODE_ENV:=test} npm-run-all --silent lint test-integrity test-public-api test-private-api"

  • Yes, I can find the modules directory in my sandbox;

  • I run the npm test at sandbox, and throws some errors:

2:31 error Unable to resolve path to module '@enterprise-name/module-name' import/no-unresolved

The errors above are examples and I omit the others import errors from rest of modules.

@nicojs
Copy link
Member

nicojs commented Jun 8, 2020

Well, the only thing I can tell you is that it is not something directly related to Stryker.
You can either:

  1. Figure out why tests don't run in sandboxes, try. Hint: probably has something to do with NODE_PATH or a dependency setting NODE_PATH. (sorry :/)
  2. You can wait for Allow to mutate files in place #2163 to be solved, as that would most probably also solve your issue.
  3. Recreate the issue in a small example repository and attach that here, or publish that on GitHub, so I can take a look.

@jtlimo
Copy link
Author

jtlimo commented Jun 9, 2020

@nicojs I found a problem here, inside stryker sandbox the nested packages doesn't have yours node_modules.
In this image below, I point to missing node_modules at nested packages on stryker sandbox.

DeepinScreenshot_select-area_20200609165044

@nicojs
Copy link
Member

nicojs commented Jun 10, 2020

So if I understand correctly, the node_modules was supposed to be in storage, but wasn't?

Is tis your setup?

packages/
   package_under_test/
      .stryker-tmp/sandbox123
                             node_modules/ (symlink to packages/package_under_test/node_modules
      node_modules/
      storage/ (symlink to below)
   storage/
      node_modules

Sorry, but this keeps me guessing. A small reproduction repository would be helpful.

@jtlimo
Copy link
Author

jtlimo commented Jun 11, 2020

So if I understand correctly, the node_modules was supposed to be in storage, but wasn't?

Is tis your setup?

packages/
   package_under_test/
      .stryker-tmp/sandbox123
                             node_modules/ (symlink to packages/package_under_test/node_modules
      node_modules/
      storage/ (symlink to below)
   storage/
      node_modules

Sorry, but this keeps me guessing. A small reproduction repository would be helpful.

My setup:

project_folder/
    node_modules/
    components/
    modules/
         storage/
             node_modules/
    ...others_folders/

Right?

Okay, but inside .stryker-tmp/sandbox, in nested packages like storage/ stryker doesn't generate a symlink to storage/node_modules... This occurs for every lerna package at my project. I will try to reproduce this behavior on small repository as soon as possible.

And here has my .stryker-tmp/sandbox:

project_folder/
     .stryker-tmp/
           sandbox/
               node_modules ---> symlink to node_modules from project_folder root.
               components/
               modules/
                   storage/
                       example.ts
                       package.json
                       package-lock.json
                       ...other_files
                       **here is missing the node_modules for this lerna package**

@jtlimo
Copy link
Author

jtlimo commented Jun 22, 2020

So if I understand correctly, the node_modules was supposed to be in storage, but wasn't?
Is tis your setup?

packages/
   package_under_test/
      .stryker-tmp/sandbox123
                             node_modules/ (symlink to packages/package_under_test/node_modules
      node_modules/
      storage/ (symlink to below)
   storage/
      node_modules

Sorry, but this keeps me guessing. A small reproduction repository would be helpful.

My setup:

project_folder/
    node_modules/
    components/
    modules/
         storage/
             node_modules/
    ...others_folders/

Right?

Okay, but inside .stryker-tmp/sandbox, in nested packages like storage/ stryker doesn't generate a symlink to storage/node_modules... This occurs for every lerna package at my project. I will try to reproduce this behavior on small repository as soon as possible.

And here has my .stryker-tmp/sandbox:

project_folder/
     .stryker-tmp/
           sandbox/
               node_modules ---> symlink to node_modules from project_folder root.
               components/
               modules/
                   storage/
                       example.ts
                       package.json
                       package-lock.json
                       ...other_files
                       **here is missing the node_modules for this lerna package**

@nicojs this structure above helps, or it's best to you I create the repo?

I'm trying to create this repo but I does not have time yet, sorry.

@brodycj
Copy link

brodycj commented Oct 2, 2020

What is the status of the reproduction?

@jtlimo
Copy link
Author

jtlimo commented Oct 2, 2020

What is the status of the reproduction?

I'm does not have time yet to reproduce in a public repo. I wish to do in this quarter.

@nicojs
Copy link
Member

nicojs commented Jan 25, 2021

@jtlimo good news 🎉, version 4.4 just released with --inPlace mode. It will solve your issue since it will mutate your files in place. See https://stryker-mutator.io/docs/stryker/configuration/#inplace-boolean

I'm very curious to know what you think 🙌

@jtlimo
Copy link
Author

jtlimo commented Jan 29, 2021

@jtlimo good news tada, version 4.4 just released with --inPlace mode. It will solve your issue since it will mutate your files in place. See https://stryker-mutator.io/docs/stryker/configuration/#inplace-boolean

I'm very curious to know what you think raised_hands

Wow, I'm very happy to see this happen!
I'll try this version asap!

Thanks @nicojs !

@stale
Copy link

stale bot commented Jan 29, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the ☠ stale Marked as stale by the stale bot, will be removed after a certain time. label Jan 29, 2022
@stale stale bot closed this as completed Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working ☠ stale Marked as stale by the stale bot, will be removed after a certain time.
Projects
None yet
Development

No branches or pull requests

4 participants