Search code examples
node.jstypescriptglobts-node

Run glob command in TypeScript (TS-Node)


A couple months earlier, I asked how to feed a glob into a Node pattern. I wanted to run tests in RITEway and the solution was to run the command with riteway instead of node.

"test": "NODE_ENV=test riteway -r @babel/register 'src/**/*.test.js'",

Now I converted the project to TypeScript. Running the command above (after changing .js to .ts), throws the following error:

$ NODE_ENV=test riteway -r @babel/register 'src/tests/**/*.test.ts' | tap-nirvana
/Users/user/my-proj/src/tests/fn.test.ts:1
import { describe } from 'riteway';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.newLoader [as .js] (/Users/user/my-proj/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at /Users/user/my-proj/node_modules/riteway/bin/riteway:54:5
    at Array.forEach (<anonymous>)

After Googling, I think I have to use ts-node, but that command has the same problem as just running node.

yarn run v1.22.10
$ NODE_ENV=test ts-node -r @babel/register 'src/tests/**/*.test.ts' | tap-nirvana
Error: Cannot find module '/Users/user/my-project/src/tests/**/*.test.ts'

How can I feed the command to RITEway, but make it work with TypeScript?

{
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": "./src",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "es5"
  },
  "exclude": ["node_modules"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}

Solution

  • You can run transpiled typescript by registering ts-node/register/transpile-only in your riteway command:

    "test": "NODE_ENV=test  riteway -r ts-node/register/transpile-only 'src/**/*.test.ts'",
    

    Also make sure that the module option in your tsconfig is set to commonjs:

    {
        "compilerOptions": {
            "allowJs": true,
            "baseUrl": "./src",
            "esModuleInterop": true,
            "forceConsistentCasingInFileNames": true,
            "isolatedModules": true,
            "jsx": "preserve",
            "lib": [
                "dom",
                "dom.iterable",
                "esnext"
            ],
            "module": "commonjs",
            "moduleResolution": "node",
            "noEmit": true,
            "resolveJsonModule": true,
            "skipLibCheck": true,
            "strict": true,
            "target": "es6"
        },
        "exclude": [
            "node_modules"
        ],
        "include": [
            "next-env.d.ts",
            "**/*.ts",
            "**/*.tsx"
        ]
    }