diff --git a/src/index.ts b/src/index.ts index 5f9c5c9aa..7c3f6af07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -672,6 +672,15 @@ export function create(rawOptions: CreateOptions = {}): Service { }); } + /** + * True if require() hooks should interop with experimental ESM loader. + * Enabled explicitly via a flag since it is a breaking change. + */ + let experimentalEsmLoader = false; + function enableExperimentalEsmLoaderInterop() { + experimentalEsmLoader = true; + } + // Install source map support and read from memory cache. installSourceMapSupport(); function installSourceMapSupport() { @@ -1254,15 +1263,6 @@ export function create(rawOptions: CreateOptions = {}): Service { }); } - /** - * True if require() hooks should interop with experimental ESM loader. - * Enabled explicitly via a flag since it is a breaking change. - */ - let experimentalEsmLoader = false; - function enableExperimentalEsmLoaderInterop() { - experimentalEsmLoader = true; - } - return { [TS_NODE_SERVICE_BRAND]: true, ts, diff --git a/src/repl.ts b/src/repl.ts index 31429e4aa..0ef51017d 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -371,10 +371,7 @@ export function createRepl(options: CreateReplOptions = {}) { !name.includes('/') && !['console', 'module', 'process'].includes(name) ) - .map( - (name) => - `declare const ${name}: typeof import('${name}');declare import ${name} = require('${name}')` - ) + .map((name) => `declare import ${name} = require('${name}')`) .join(';')}\n`; } diff --git a/src/test/helpers.ts b/src/test/helpers.ts index 00b59d12f..245e0a924 100644 --- a/src/test/helpers.ts +++ b/src/test/helpers.ts @@ -40,6 +40,8 @@ export const CMD_ESM_LOADER_WITHOUT_PROJECT = `node ${EXPERIMENTAL_MODULES_FLAG} // `createRequire` does not exist on older node versions export const testsDirRequire = createRequire(join(TEST_DIR, 'index.js')); +export const ts = testsDirRequire('typescript'); + export const xfs = new NodeFS(fs); /** Pass to `test.context()` to get access to the ts-node API under test */ diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index 3307ae73d..730d1a3be 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -3,7 +3,7 @@ import * as expect from 'expect'; import { join, resolve, sep as pathSep } from 'path'; import { tmpdir } from 'os'; import semver = require('semver'); -import ts = require('typescript'); +import { ts } from './helpers'; import { lstatSync, mkdtempSync } from 'fs'; import { npath } from '@yarnpkg/fslib'; import type _createRequire from 'create-require'; diff --git a/src/test/repl/node-repl-tla.ts b/src/test/repl/node-repl-tla.ts index 210d22ec0..616237926 100644 --- a/src/test/repl/node-repl-tla.ts +++ b/src/test/repl/node-repl-tla.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import type { Key } from 'readline'; import { Stream } from 'stream'; import semver = require('semver'); -import ts = require('typescript'); +import { ts } from '../helpers'; import type { ContextWithTsNodeUnderTest } from './helpers'; interface SharedObjects extends ContextWithTsNodeUnderTest { diff --git a/src/test/repl/repl.spec.ts b/src/test/repl/repl.spec.ts index 2ea11cddb..2ef41d15d 100644 --- a/src/test/repl/repl.spec.ts +++ b/src/test/repl/repl.spec.ts @@ -1,4 +1,4 @@ -import ts = require('typescript'); +import { ts } from '../helpers'; import semver = require('semver'); import * as expect from 'expect'; import { @@ -414,7 +414,7 @@ test.suite( test.serial('REPL declares types for node built-ins within REPL', async (t) => { const { stdout, stderr } = await t.context.executeInRepl( - `util.promisify(setTimeout)("should not be a string") + `util.promisify(setTimeout)("should not be a string" as string) type Duplex = stream.Duplex const s = stream 'done'`,