Mrm TypeScript Task
mrm-task-typescript is an Mrm task designed to automate the initial setup and configuration of TypeScript within a JavaScript project. It currently stands at version 4.1.22. As part of the Mrm monorepo, its releases are often coordinated with other Mrm tasks, typically seeing version bumps (sometimes 'bump only') to ensure compatibility across the Mrm ecosystem. This task distinguishes itself by providing a declarative and automated way to standardize TypeScript configuration across multiple projects or within an organization, reducing manual setup errors and ensuring best practices are applied consistently. It handles the creation of `tsconfig.json`, integrates a type-checking script into `package.json`, and installs necessary TypeScript-related dependencies, streamlining the developer workflow for adopting TypeScript.
Common errors
-
command not found: mrm
cause The Mrm command-line interface tool is not installed globally or accessible via npx.fixUse `npx mrm typescript` or install Mrm globally with `npm install -g mrm`. -
Error: EPERM: operation not permitted, mkdir 'src'
cause Permission issues prevent Mrm from creating necessary directories or files in the project path.fixEnsure you have write permissions in the project directory. On some systems, you might need to run your terminal with elevated privileges (e.g., `sudo npx mrm typescript` on Linux/macOS or 'Run as Administrator' on Windows). -
Cannot find module 'typescript'
cause The TypeScript package, which is a dependency managed by the task, was not successfully installed in your project's `node_modules`.fixAfter running `npx mrm typescript`, ensure you run `npm install` (or `yarn install`) to install all project dependencies, including TypeScript. -
Missing script: 'typecheck'
cause The `typecheck` npm script was not correctly added to your `package.json` by the Mrm task.fixVerify your `package.json` for the presence of a `scripts.typecheck` entry. If missing, re-run `npx mrm typescript` or manually add `"typecheck": "tsc --noEmit"` to your `scripts`.
Warnings
- gotcha Ensure your Node.js environment meets the minimum requirement (>=10.13) as specified in the package's `engines` field. Using older Node.js versions may lead to unexpected errors.
- gotcha Mrm tasks can overwrite existing configuration files. If you have an existing `tsconfig.json` or related npm scripts, review the changes made by the task to ensure they align with your project's needs.
- gotcha The Mrm task runner (`mrm`) must be accessible in your PATH. While `npx` handles this for global installs, ensure `mrm` itself is installed or resolvable if you're not using `npx`.
- gotcha Mrm tasks are designed to be run in the root directory of your project where `package.json` resides. Running them from subdirectories can lead to incorrect file placements or configuration.
- gotcha The specific versions of TypeScript and `@types` packages installed by this task are managed by the task's internal logic. These might conflict with pre-existing versions in your `package.json` or require specific TypeScript versions for compilation.
Install
-
npm install mrm-task-typescript -
yarn add mrm-task-typescript -
pnpm add mrm-task-typescript
Imports
- mrm-task-typescript
import { typescript } from 'mrm-task-typescript';npx mrm typescript
- mrm-task-typescript
const typescript = require('mrm-task-typescript');npx mrm typescript
- mrm-task-typescript
import typescript from 'mrm-task-typescript';
npx mrm typescript
Quickstart
import * as path from 'path';
import * as fs from 'fs';
import { execSync } from 'child_process';
const projectName = 'my-ts-project';
const projectPath = path.join(process.cwd(), projectName);
console.log(`Creating project directory: ${projectPath}`);
fs.mkdirSync(projectPath, { recursive: true });
process.chdir(projectPath);
console.log('Initializing npm...');
execSync('npm init -y', { stdio: 'inherit' });
console.log('Running mrm typescript task...');
execSync('npx mrm typescript', { stdio: 'inherit' });
console.log('\nVerifying setup:');
console.log('Content of tsconfig.json:');
console.log(fs.readFileSync('tsconfig.json', 'utf8'));
console.log('\nContent of package.json scripts:');
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
console.log(packageJson.scripts);
// Create a sample TypeScript file to demonstrate compilation
fs.mkdirSync('src', { recursive: true });
fs.writeFileSync('src/index.ts', `
function greet(name: string): string {
return \`Hello, \${name}!\`;
}
console.log(greet("TypeScript User"));
`);
console.log('\nAttempting to run type check script...');
execSync('npm run typecheck', { stdio: 'inherit' });
console.log('\nTypeScript setup complete! To clean up, run:');
console.log(`rm -rf ${projectName}`);