How to use ts-node ESM with node modules? #1321
-
I am trying to make a simple script that reads a files from one location and processes it. Here is what I have so far, having followed #1007: Node: v12.22.1
package.json {
"name": "my-app",
"version": "1.0.0",
"type": "module",
"scripts": {
"lint": "eslint ./src --ext .js,.jsx,.ts,.tsx --max-warnings=0",
"start": "node --experimental-specifier-resolution=node --experimental-modules --no-warnings --loader ts-node/esm ./src/main.ts"
},
"author": "Kaustubh Badrike",
"devDependencies": {
"@types/node": "^15.3.0",
"@typescript-eslint/eslint-plugin": "^4.24.0",
"@typescript-eslint/parser": "^4.24.0",
"eslint": "^7.26.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
}
} tsconfig.json {
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
} main.ts import { readFileSync } from "fs";
const contents = readFileSync('../in/given.txt'); However, when I try
The error does not occur if I remove the import from fs. Importing items from helper.ts works as well. Another, possibly shorter way to reproduce:
import { foo } from './foo';
import { bar } from './bar';
import { baz } from './baz';
import { libfoo } from 'libfoo';
import { readFileSync } from 'fs';
console.log(`${foo} ${bar} ${baz} ${libfoo}`);
const content = readFileSync('package.json', { encoding: 'utf8' });
console.log(content);
What do I need to do to get it to work? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
@KaustubhBadrike-GEP I could reproduce your issue on NodeJS 12.22.1. I can also confirm that it work properly on NodeJS 14.16. Is updating your NodeJS version a possibility? Keep in mind that NodeJS support to ESM is experimental for a long time and changes are usually expected in this phase. Also, related to that fact you are using a version of NodeJS (12.X) that is approaching the end of it's maintenance life, my recommendation is that you update your NodeJS to at least version 14.x either to solve this issue and for you to benefit on the latest development and guarantee long term support. |
Beta Was this translation helpful? Give feedback.
-
Fixed by #1332. |
Beta Was this translation helpful? Give feedback.
Fixed by #1332.