ts-node: `npx` invocation yields `ERR_INVALID_MODULE_SPECIFIER `

(@cspotcode This is from a while back on Discord)

Search Terms

  • npx

Context

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

For an example of how I had this working pre-ESM, see this gist.

Expected Behavior

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

Actual Behavior

$ npx https://gist.github.com/brianjenkins94/d73578d7439427a59df28c1bc751e02d
Need to install the following packages:
  gist:d73578d7439427a59df28c1bc751e02d
Ok to proceed? (y) y

> node --experimental-specifier-resolution=node --loader=/Users/bjenkins/.npm/_npx/caa40e90fbe6bfad/ts-node/esm /Users/bjenkins/.npm/_npx/caa40e90fbe6bfad/node_modules/sampleThatReproducesTheIssue/hello.ts

(node:35543) 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:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/bjenkins/.npm/_npx/caa40e90fbe6bfad/ts-node/esm' imported from /Users/bjenkins/Desktop/foo/
    at new NodeError (node:internal/errors:371:5)
    at finalizeResolution (node:internal/modules/esm/resolve:405:11)
    at moduleResolve (node:internal/modules/esm/resolve:932:10)
    at defaultResolve (node:internal/modules/esm/resolve:1044:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:422:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:222:40)
    at ESMLoader.import (node:internal/modules/esm/loader:276:22)
    at initializeLoader (node:internal/process/esm_loader:74:49)
    at loadESM (node:internal/process/esm_loader:87:11)
    at runMainESM (node:internal/modules/run_main:47:21) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Steps to reproduce the problem

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

Minimal reproduction

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

Specifications

  • ts-node version: v10.4.0
  • node version: v16.13.2
  • TypeScript version: v4.5.4
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",
		"scripts/prev.ts"
	]
}
  • Operating system and version: macOS Monterey 12.1 (21C52)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20

Most upvoted comments

Thanks, I can take a closer look tonight. In the interim, I see that you’re using ts-node 10.4.0. It may be worth confirming that 10.5.0 exhibits the same behavior.