Skip to content

[Node.js 12.11+ and Windows 10] Error: Cannot find module "node" #1205

@cedx

Description

@cedx

My use case: I have TypeScript projects that are coded as ES modules (for example: cedx/coveralls.js or cedx/which.js). They are intended to run on Node.js v12.x only and use the field "type": "module" in the package.json file (i.e. the used file extension is .js, not .mjs).

For the tests and code coverage, I'm using ts-node to avoid compiling the projects while I develop them. So, in test environment, the projects are compiled on the fly as CommonJS modules (ts-node does not support ES modules).

The command line used:
nyc --nycrc-path=etc/nyc.json node_modules/.bin/mocha --config=etc/mocha.json "test/**/*.ts"

This configuration worked fine on Node v12.10.x and below, but since v12.11.0 the tests don't run anymore on Windows and produce this error:

internal/modules/esm/default_resolve.js:82                                                                                                                                                                let url = moduleWrapResolve(specifier, parentURL);
            ^
Error: Cannot find module C:\Projets\js\enum.js\node imported from C:\Projets\js\enum.js\
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:82:13)                                                                                                                         at Loader.resolve (internal/modules/esm/loader.js:73:33)                                                                                                                                                at Loader.getModuleJob (internal/modules/esm/loader.js:147:40)                                                                                                                                          at Loader.import (internal/modules/esm/loader.js:131:28)                                                                                                                                                at internal/modules/cjs/loader.js:1015:27                                                                                                                                                               at processTicksAndRejections (internal/process/task_queues.js:93:5) {                                                                                                                                 code: 'ERR_MODULE_NOT_FOUND'                                                                                                                                                                          }

If I run the tests without code coverage, no error occurs:
mocha --config=etc/mocha.json "test/**/*.ts" => everything is OK.

Link to bug demonstration repository

https://github.com/cedx/enum.js

Expected Behavior

The tests succeed to run and the code coverage is collected.

Observed Behavior

The tests fail to run if code coverage is collected with nyc.

Troubleshooting steps

  • this issue still occurs when I put cache: false in my nyc config
  • this issue only appears on Windows platform. On a Linux platform, everything is OK.

Environment Information

System:                                                                                                                                                                                                   
    OS: Windows 10
    CPU: (4) x64 Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
    Memory: 2.41 GB / 7.92 GB
Binaries:                                                                                                                                                                                                 
    Node: 12.12.0 - C:\Program Files\Node.js\node.EXE
    npm: 6.11.3 - C:\Program Files\Node.js\npm.CMD
npmPackages:                                                                                                                                                                                              
    nyc: ^14.1.1 => 14.1.1
    source-map-support: ^0.5.13 => 0.5.13
    ts-node: ^8.4.1 => 8.4.1
    typescript: ^3.6.4 => 3.6.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions