【发布时间】:2021-08-08 12:12:09
【问题描述】:
注意:我知道以下问题是关于实验性功能的。我创建了一个clone on the ts-node discussion forum。不过,我相信 StackOverflow 的曝光范围更广,并且会在更短的时间内找到解决方案。
我正在尝试制作一个简单的脚本,从一个位置读取文件并对其进行处理。这是我到目前为止所拥有的,关注#1007:
节点:v12.22.1
my-app
├── .eslintrc.cjs
├── .gitignore
├── in
│ └── given.txt
├── node_modules
├── out
├── package-lock.json
├── package.json
├── src
│ ├── helper.ts
│ └── main.ts
└── tsconfig.json
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');
但是,当我尝试 npm run start 时,我得到以下信息
TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected string to be returned for the "format" from the "loader getFormat" function but got type
object.
at Loader.getFormat (internal/modules/esm/loader.js:125:13)
at Loader.getModuleJob (internal/modules/esm/loader.js:247:20)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:49:17)
at async Promise.all (index 0)
at link (internal/modules/esm/module_job.js:53:9)
如果我从 fs 中删除导入,则不会发生该错误。从 helper.ts 导入项目也可以。
我需要做什么才能让它工作?
【问题讨论】:
标签: node.js typescript ts-node