[英]Unable to import ESM .ts module in node
I have been trying to import an ESM module written in typescript in nodejs.我一直在尝试在nodejs中导入一个用typescript写的ESM模块。 But I am getting the following error:
但我收到以下错误:
An import path cannot end with a '.ts' extension.
Util.ts工具.ts
export class Util {
constructor ( ) {
}
log(msg) {
console.log(msg)
}
}
index.ts索引.ts
import {log} from './Util.ts'
log(task.query.criteria, payload.parameters)
I have also added "type":"module"
inside package.json
我还在
package.json
中添加了"type":"module"
I changes.ts to.js just to see if it works and then I got:我将 .ts 更改为 .js 只是为了看看它是否有效然后我得到:
Object.defineProperty(exports, "__esModule", { value: true }); ^
ReferenceError: exports is not defined
at file:///C:/Users/abc/NestJsPOC/NestPOC/dist/main.js:2:23
tsconfig.json tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
}
}
EDIT编辑
I have also tried:我也试过:
var log = require('../utility/util.js');
Util.js工具.js
function log(msg) {
console.log(msg)
}
module.exports= { log}
index.ts索引.ts
log('hello')
Error :错误:
TypeError: log is not a function
I've created a repo with the necessary settings to run esm directly into Node via ts-node or using tsc and running the transpiled esm file with NodeJS.我已经创建了一个带有必要设置的存储库,可以通过 ts-node 或使用 tsc 将 esm 直接运行到 Node 中,并使用 NodeJS 运行转译的 esm 文件。 https://github.com/felipeplets/esm-examples
https://github.com/felipeplets/esm-examples
Keep in mind that you need to use ts-node 10+ and NodeJS 12+ in order to use the setup below.请记住,您需要使用ts-node 10+和NodeJS 12+才能使用以下设置。
It seems you are looking to use ESM with Node and TS.您似乎希望将 ESM 与 Node 和 TS 一起使用。
The updated settings to make it by the time I'm answering your question (updated on 29.05.2022) is:在我回答您的问题时(于 29.05.2022 更新)的更新设置是:
On your tsconfig.json file make sure to have the following settings:在您的 tsconfig.json 文件中确保具有以下设置:
{
"compilerOptions": {
"lib": ["ES2022"], // ES2020 in earlier versions
"module": "ES2022", //ESNext
"moduleResolution": "Node",
"target": "ES2022", // ES2020 in earlier versions
"esModuleInterop": true,
...
},
...
}
Make sure to have the following attribute on your package.json
file.确保您的
package.json
文件具有以下属性。
{
...
"type":"module",
...
}
Don't forget that you can't import a TS file direct in NodeJS, you need to first transpile it using tsc
to then import it in the NodeJS in runtime, so in the end you will be importing the js and not the ts file.不要忘记,您不能直接在 NodeJS 中导入 TS 文件,您需要先使用
tsc
对其进行转译,然后在运行时将其导入 NodeJS 中,所以最后您将导入 js 而不是 ts 文件. To run it as TS please make sure to read the next section of my answer.要将其作为 TS 运行,请务必阅读我的答案的下一部分。
Also when running it make sure to use the flag --experimental-specifier-resolution=node
as it will enable you to have imports with no extensions as it happens in TypeScript:此外,在运行它时,请确保使用标志
--experimental-specifier-resolution=node
,因为它将使您能够像在 TypeScript 中那样进行没有扩展的导入:
node --experimental-specifier-resolution=node index
For more details for the flag please read https://nodejs.org/api/esm.html#esm_customizing_esm_specifier_resolution_algorithm有关标志的更多详细信息,请阅读https://nodejs.org/api/esm.html#esm_customizing_esm_specifier_resolution_algorithm
TS Node is a runtime transpiler so it allow you to import typescript files in runtime when using node. TS Node 是一个运行时转译器,因此它允许您在使用 node 时在运行时导入 typescript 文件。
There is a feature in TS Node that allows you to run it using ESM, to use it you will need to install TS Node 9.1+. TS Node 中有一个功能允许您使用 ESM 运行它,要使用它,您需要安装 TS Node 9.1+。 For more details on the implementation and possible issues check: https://github.com/TypeStrong/ts-node/issues/1007
有关实施和可能问题的更多详细信息,请查看: https ://github.com/TypeStrong/ts-node/issues/1007
To install it you will need to run:要安装它,您需要运行:
npm i -D ts-node
And to run the service supporting the new resolution mode and the TSNode loader you will need to run:要运行支持新分辨率模式的服务和 TSNode 加载器,您需要运行:
You can add esm
and experimentalSpecifierResolution
in your tsconfig file.您可以在 tsconfig 文件中添加
esm
和experimentalSpecifierResolution
。
{
...
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": true
}
}
And you then will only need to run然后你只需要运行
ts-node index
Alternatively you could set ts-node --esm
or ts-node-esm
to achieve the same without setting it on the the tsconfig.json
file.或者,您可以设置
ts-node --esm
或ts-node-esm
来实现相同的效果,而无需在tsconfig.json
文件中进行设置。
node --experimental-specifier-resolution=node --loader ts-node/esm index.ts
After this you will be able to use TS and ESM in your project in runtime and this statement will be possible:在此之后,您将能够在运行时在您的项目中使用 TS 和 ESM,并且可以使用以下语句:
import { log } from './Util'
Where log is an exported member from Util.ts其中 log 是从 Util.ts 导出的成员
Important!重要的! I don't recommend using ts-node in production, so this is very handy and useful for a development environment but make sure to transpile and use the resulting code in production to avoid possible memory issues with TS Node.
我不建议在生产环境中使用 ts-node,因此这对于开发环境非常方便和有用,但请确保在生产环境中转译和使用生成的代码,以避免 TS Node 可能出现的内存问题。
I've tried every solution I can find or think of multiple times over the past several years and I've just found the following package which worked out of the box without needing any additional setup:在过去的几年里,我已经多次尝试了我能找到或想到的所有解决方案,我刚刚找到了以下 package,它开箱即用,不需要任何额外的设置:
https://www.npmjs.com/package/tsx https://www.npmjs.com/package/tsx
It just works:它只是工作:
npm i -D tsx
npx tsx src/index.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.