I've been messing around with a new Node project and I can't get my modules to load correctly and every error points me in a direction that only gets me more errors.
I created a new project, here's the contents of the project's package.json
{ "name": "module-test", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "@types/boxen": "^3.0.1", "@types/chalk": "^2.2.0", "@types/node": "^18.15.0" } } here's the contents of the default tsconfig.json file generated using tsc init:
{ "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "strict": true, "skipLibCheck": true } } Here's my project's TypeScript code:
#!/usr/bin/env node import fs from 'fs'; import path from 'path'; import boxen from 'boxen'; import chalk from 'chalk'; const inputFile = path.join(process.cwd(), 'package.json'); const outputFile = path.join(process.cwd(), 'version.text'); // Write a box to the console console.log(boxen('This is a box', { padding: 1 })); // write some red text to the console console.log(chalk.red('This is red')); // Read the package.json file let rawData = fs.readFileSync(inputFile); // parse the file as JSON let packageDotJSON = JSON.parse(rawData.toString()); // get the version number let buildVersion = packageDotJSON.version; // write the version number to a file try { fs.writeFileSync(outputFile, buildVersion, 'utf8'); console.log('File written successfully'); } catch (err: any) { console.error(err); } The application compiles fine, but when I try to execute it, I get the following error:
C:\Users\john\dev\module-test>tsc C:\Users\john\dev\module-test>node index.js C:\Users\john\dev\module-test\index.js:9 const boxen_1 = __importDefault(require("boxen")); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\john\dev\module-test\node_modules\boxen\index.js from C:\Users\john\dev\module-test\index.js not supported. Instead change the require of C:\Users\john\dev\module-test\node_modules\boxen\index.js in C:\Users\john\dev\module-test\index.js to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (C:\Users\john\dev\module-test\index.js:9:33) { code: 'ERR_REQUIRE_ESM' } Node.js v18.15.0 A google search pointed me to articles like this: which tell me to do this:
const fsModule = await import('fs'); const fs = fsModule.default; const pathModule = await import('path'); const path = pathModule.default; const boxenModule = await import('boxen'); const boxen = boxenModule.default; const chalkModule = await import('chalk'); const chalk = chalkModule.default; But then two new error messages appear from the TypeScript compiler telling me to add:
export {} and change my tsconfig.json file to:
{ "compilerOptions": { "target": "eS2017", "module": "ES2022", "esModuleInterop": true, "strict": true, "skipLibCheck": true } } but when I do that, the boxen and chalk imports complain with the following error:
Cannot find module 'chalk'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option? What do I do here to make a simple node app operate with a few internal and external modules loaded? I spent hours yesterday poking and prodding at this respdindg to different errors and trying the different module and target settings in the project's tsconfig.json file.
This is supposed to be easy, right? Or is module loading completely borked here?
1 Answer
As usual, I find my answer immediately after posting a question here. I found the solution here: .
I added "type": "module to the package.json file and uncommented "moduleResolution": "node", in the tsconfig.json file.