Skip to content

Possible discrepancy with --experimental-strip-types  #54457

Closed
@cjihrig

Description

@cjihrig

Version

main (commit 561bc87)

Platform

macOS, but should reproduce on all platforms.

Subsystem

CLI

What steps will reproduce the bug?

Create three entrypoint files:

// main.js
'use strict';
async function main() {
  await import('./dep.js');
}

main();
// main.mjs
await import('./dep.js');
// main.ts
'use strict';
async function main() {
  await import('./dep.js');
}

main();

Create two dependency files (note that these files are exactly the same, but have different file extensions):

// dep.js
'use strict';
require('node:fs');
console.log('ok');
// dep.ts
'use strict';
require('node:fs');
console.log('ok');

Run all three entrypoints:

$ ./node --experimental-strip-types main.js
(node:62972) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ok
$ ./node --experimental-strip-types main.mjs
(node:63282) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ok
$ ./node --experimental-strip-types main.ts
(node:63489) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
ok

Update all three entrypoint files to import dep.ts instead of dep.js, and run the files again:

$ ./node --experimental-strip-types main.js
(node:69672) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:69672) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///private/tmp/repro/dep.ts is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /private/tmp/package.json.
file:///private/tmp/repro/dep.ts:2
require('node:fs');
^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///private/tmp/repro/dep.ts:2:1
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:482:26)
    at async main (/private/tmp/repro/main.js:3:3)

Node.js v23.0.0-pre
$ ./node --experimental-strip-types main.mjs
(node:71132) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:71132) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///private/tmp/repro/dep.ts is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /private/tmp/package.json.
file:///private/tmp/repro/dep.ts:2
require('node:fs');
^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///private/tmp/repro/dep.ts:2:1
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:482:26)
    at async file:///private/tmp/repro/main.mjs:1:1

Node.js v23.0.0-pre
$ ./node --experimental-strip-types main.ts
(node:71240) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:71240) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///private/tmp/repro/dep.ts is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /private/tmp/package.json.
file:///private/tmp/repro/dep.ts:2
require('node:fs');
^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///private/tmp/repro/dep.ts:2:1
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:482:26)
    at async main (/private/tmp/repro/main.ts:3:3)

Node.js v23.0.0-pre

How often does it reproduce? Is there a required condition?

It reproduces 100% of the time in my experience.

What is the expected behavior? Why is that the expected behavior?

I expect both scenarios to succeed because the docs state that .ts files have their module system determined the same way as .js files.

What do you see instead?

(node:71240) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///private/tmp/repro/dep.ts is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /private/tmp/package.json.
file:///private/tmp/repro/dep.ts:2
require('node:fs');
^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///private/tmp/repro/dep.ts:2:1
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:482:26)
    at async main (/private/tmp/repro/main.ts:3:3)

Node.js v23.0.0-pre

Additional information

The error is also confusing because it says that dep.ts does not parse as CommonJS, even though it has the same content as dep.js, which seems to be a CommonJS file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    strip-typesIssues or PRs related to strip-types support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions