Skip to content

Windows-specific ERR_UNKNOWN_MODULE_FORMAT #1686

@brianjenkins94

Description

@brianjenkins94

Possibly related:

Search Terms

  • npx

Context

I run my typescript (served via npx) through ts-node so I don't have to maintain a build.

Additional Context: #1663

Expected Behavior

The program runs without error. In the case of the linked gist, it runs hello.ts, printing "Hello, world!".

Actual Behavior

On Windows (CMD or Git Bash, doesn't matter)

For some reason the error gets swallowed:

$ npx https://gist.github.com/brianjenkins94/d73578d7439427a59df28c1bc751e02d

npm WARN exec The following package was not found and will be installed: gist:d73578d7439427a59df28c1bc751e02d

> node --experimental-specifier-resolution=node --loader=file:///C:/Users/User/AppData/Local/npm-cache/_npx/caa40e90fbe6bfad/node_modules/ts-node/esm.mjs C:/Users/User/AppData/Local/npm-cache/_npx/caa40e90fbe6bfad/node_modules/sampleThatReproducesTheIssue/hello.ts

So I copy and paste what it was supposed to spawn:

$ node --experimental-specifier-resolution=node --loader=file:///C:/Users/User/AppData/Local/npm-cache/_npx/caa40e90fbe6bfad/node_modules/ts-node/esm.mjs C:/Users/User/AppData/Local/npm-cache/_npx/caa40e90fbe6bfad/node_modules/sampleThatReproducesTheIssue/hello.ts
(node:22652) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:22652) ExperimentalWarning: The Node.js specifier resolution in ESM is experimental.
(node:22652) ExperimentalWarning: The Node.js specifier resolution in ESM is experimental.
RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: null for URL file:///C:/Users/User/AppData/Local/npm-cache/_npx/caa40e90fbe6bfad/node_modules/sampleThatReproducesTheIssue/hello.ts
    at new NodeError (node:internal/errors:372:5)
    at ESMLoader.load (node:internal/modules/esm/loader:384:13)
    at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:282:11)
    at async link (node:internal/modules/esm/module_job:70:21) {
  code: 'ERR_UNKNOWN_MODULE_FORMAT'
}

It seems like since I've updated to Node.js v17.6.0 it's exclusively erroring with ERR_UNKNOWN_MODULE_FORMAT, but I was previously able to provoke a ERR_INVALID_MODULE_SPECIFIER by adding or removing file:/// for the file argument.

nodejs/node#42098 suggests that the code that throws ERR_UNKNOWN_MODULE_FORMAT is very new (less than 2 weeks old) and we're well into experimental territory here, but I just thought it was curious that this is only a problem on Windows.

Steps to reproduce the problem

Ended up just using the previous gist:

$ npx https://gist.github.com/brianjenkins94/d73578d7439427a59df28c1bc751e02d

Minimal reproduction

https://gist.github.com/brianjenkins94/d73578d7439427a59df28c1bc751e02d

Specifications

  • ts-node version: v10.7.0
  • node version: v17.6.0
  • TypeScript version: v4.6.2
tsconfig.json
{
	"compilerOptions": {
		"allowJs": true,
		"esModuleInterop": true,
		"forceConsistentCasingInFileNames": false,
		"incremental": true,
		"isolatedModules": true,
		"jsx": "preserve",
		"lib": [
			"dom",
			"dom.iterable",
			"esnext"
		],
		"module": "ESNext",
		"moduleResolution": "Node",
		"noEmit": true,
		"resolveJsonModule": true,
		"skipLibCheck": true,
		"strict": false,
		"alwaysStrict": true,
		//"exactOptionalPropertyTypes": true,
		//"strictNullChecks": true,
		"strictBindCallApply": true,
		"strictFunctionTypes": true,
		//"strictPropertyInitialization": true,
		//"noImplicitAny": true,
		//"noImplicitThis": true,
		//"useUnknownInCatchVariables": true,
		"noImplicitOverride": true,
		"noImplicitReturns": true,
		"noPropertyAccessFromIndexSignature": true,
		"target": "ES2020"
	},
	"include": [
		"next-env.d.ts",
		"**/*.ts",
		"**/*.tsx"
	],
	"exclude": [
		"node_modules"
	]
}
  • Operating system and version: Windows 10 21H2

Originally posted by @brianjenkins94 in #1663 (comment)

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