简体   繁体   English

ESM 更改在我的 NestJS 项目中产生冲突

[英]ESM changes are making conflict in my NestJS project

I'm the author of nestjs-cashify .我是nestjs-cashify的作者。 I decided to update my package as well as the underlying packages.我决定更新我的包以及底层包。 I'm encountering some issues after updating one the core packages that I'm using it.在更新我正在使用的一个核心软件包后,我遇到了一些问题。 Since my package ts configuration is set to es6 not es2020 .由于我的包 ts 配置设置为es6而不是es2020 (In other words, the new version of package that I'm using is changed to es2020 recently) (也就是说我用的新版包最近改成了es2020

When I want to use my package in a regular NestJS project (in my example folder), I get these errors:当我想在常规 NestJS 项目(在我的示例文件夹中)中使用我的包时,我收到以下错误:

/home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js:21
const cashify_1 = require("cashify");
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/vahidnajafi/repos/nestjs-cashify/node_modules/cashify/dist/index.js from /home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js not supported.
Instead change the require of index.js in /home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/dist/cashify.module.js:21:19)
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/dist/index.js:18:14)
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/example/dist/app.module.js:12:26)
    at Object.<anonymous> (/home/vahidnajafi/repos/nestjs-cashify/example/dist/main.js:4:22) {
  code: 'ERR_REQUIRE_ESM'
}

This makes sense since my project is set to "es6" and when I build, it uses "require" for ES module (core module is now ESM from new version): This is the error: require() of ES Module /index.js from /cashify.module.js not supported.这是有道理的,因为我的项目设置为“es6”,并且当我构建时,它对 ES 模块使用“require”(核心模块现在是新版本的 ESM):这是错误: ES 模块 /index 的 require()。不支持来自 /cashify.module.js 的 js。

Then I decided to set my project to "es2020" as well.然后我决定将我的项目也设置为“es2020”。 Now, this error is gone (since in my build result it's not using require anymore, it's using import statement).现在,这个错误消失了(因为在我的构建结果中它不再使用require ,它正在使用 import 语句)。

In my example folder ( regular NestJS project ), which I'm testing my package, this config is set: "target": "es2017",在我正在测试我的包的示例文件夹(常规 NestJS 项目)中,设置了此配置: "target": "es2017",

Now I see the following error:现在我看到以下错误:

SyntaxError: Unexpected token 'export'

IMPORTANT NOTE:重要的提示:

Even though that's not a good idea to change my example project's configuration ( since all NestJS developers will have to do this ), I tried to fix this anyway.尽管更改示例项目的配置不是一个好主意(因为所有 NestJS 开发人员都必须这样做),但我还是尝试解决这个问题。 I changed it to es2020 as well.我也将其更改为es2020

This time, I get the following errors:这一次,我收到以下错误:

(node:89687) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/home/vahidnajafi/repos/nestjs-cashify/example/dist/main.js:1
import { NestFactory } from '@nestjs/core';
^^^^^^

SyntaxError: Cannot use import statement outside a module

I also tried to set "type": "module" in package.json file (as mentioned in the error message) and then I see this error:我还尝试在 package.json 文件中设置“type”:“module”(如错误消息中所述),然后我看到此错误:

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/vahidnajafi/repos/nestjs-cashify/example/dist/app.module' imported from /home/vahidnajafi/repos/nestjs-cashify/example/dist/main.js

Any idea?任何想法?

It looks like your modules are not resolving properly, probably because you are not using the new moduleResolution settings added in TS v4.7 .看起来您的模块无法正确解析,可能是因为您没有使用TS v4.7中添加的新 moduleResolution 设置。

You need to add the following to your TSConfig file.您需要将以下内容添加到您的 TSConfig 文件中。

    "module": "Node16", // Set your module Type to an ESM Module 
    "moduleResolution": "Node16", // Resolve your imports as an ESM Module
    "allowSyntheticDefaultImports": true, // Ease ESM support where the packages on the other-side need it.
Make sure you have added TS v4.7 using the following:确保您已使用以下内容添加了 TS v4.7:

npm i -D typescript@latest

Once you have done the above, rebuild your entire project, then try running it.完成上述操作后,重建整个项目,然后尝试运行它。 It should work, I have got the #74 error myself.它应该可以工作,我自己也遇到了 #74 错误。 The error stems from an improperly configured ESM-TS-Node project.该错误源于配置不正确的 ESM-TS-Node 项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM