简体   繁体   English

必须使用import加载ES Module.eslintrc.js

[英]Must use import to load ES Module .eslintrc.js

I am trying to fix this problem for hours.我试图解决这个问题几个小时。 I've read nearly every post about this, but still, I came to no solution.我几乎阅读了所有关于此的帖子,但仍然没有找到解决方案。

I am trying to deploy a firebase-function with the "https got-library" dependency, but no matter what I do, nothing works.我正在尝试部署具有“https got-library”依赖项的 firebase 函数,但无论我做什么,都无济于事。 I am not the best with node.js or typescript (usually a kotlin frontend-dev), so I have no clue what the error wants from me.我不是 node.js 或 typescript(通常是 kotlin frontend-dev)的最佳人选,所以我不知道错误要我做什么。

Tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": false,
    "esModuleInterop": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
}

.eslintrc.js .eslintrc.js

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "google",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: ["tsconfig.json", "tsconfig.dev.json"],
    sourceType: "module",
  },
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],
  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: {
    "quotes": ["error", "double"],
    "import/no-unresolved": 0,
    "linebreak-style": ["error", "windows"],
    "indent": "off",
    "object-curly-spacing": "off",
    "no-tabs": 0,
    "max-len": "off",
    "require-jsdoc": 0,
    "no-empty": [0, "allow-empty-functions", "allow-empty-catch"],
    "@typescript-eslint/no-explicit-any": ["off"],
    "@typescript-eslint/naming-convention": ["off"],
    "@typescript-eslint/explicit-function-return-type": "off",
    "@typescript-eslint/explicit-module-boundary-types": "off",
    "@typescript-eslint/no-var-requires": "off",
    "no-mixed-spaces-and-tabs": 0,
    "camelcase": 0,
  },
};

package.json package.json

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "type": "module",
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/functions-framework": "^2.1.0",
    "@types/stripe": "^8.0.417",
    "firebase-admin": "^10.0.1",
    "firebase-functions": "^3.14.1",
    "firebase-tools": "^10.0.1",
    "form-data": "^4.0.0",
    "got": "^12.0.0",
    "iso3166-alpha-converter": "^1.0.0",
    "mailgun.js": "^4.1.0",
    "stripe": "^8.193.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.8.0",
    "@typescript-eslint/parser": "^5.8.0",
    "eslint": "^8.5.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.3.3",
    "typescript": "^4.5.4"
  },
  "private": true
}

Error错误

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\ImMor\Documents\FirebaseFunctions\functions\.eslintrc.js
require() of ES modules is not supported.
require() of C:\Users\ImMor\Documents\FirebaseFunctions\functions\.eslintrc.js from C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\@eslint\eslintrc\dist\eslintrc.cjs is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename .eslintrc.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\ImMor\Documents\FirebaseFunctions\functions\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at Object.module.exports [as default] (C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\import-fresh\index.js:32:59)
    at loadJSConfigFile (C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:2531:47)
    at loadConfigFile (C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:2615:20)
    at ConfigArrayFactory.loadInDirectory (C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:2808:34)
    at CascadingConfigArrayFactory._loadConfigInAncestors (C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:3772:46)
    at CascadingConfigArrayFactory.getConfigArrayForFile (C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\@eslint\eslintrc\dist\eslintrc.cjs:3693:18)

Function I want to deploy Function 我要部署

// THIS IS MAKING THE PROBLEM
import got from "got";

export async function doOnDeletedUser(
    // SOME OTHER STUFF
) {
    const uid = user.uid;
    // SOME OTHER STUFF
}

Edit编辑

Changing .eslintrc.js to .eslintrc.cjs solves this problem, but then I get the following error:.eslintrc.js更改为.eslintrc.cjs解决了这个问题,但随后出现以下错误:

ReferenceError: exports is not defined
    at file:///C:/Users/ImMor/Documents/FirebaseFunctions/functions/lib/index.js:24:23
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async loadModule (C:\Users\ImMor\AppData\Roaming\npm\node_modules\firebase-tools\lib\deploy\functions\runtimes\node\triggerParser.js:16:20)
    at async C:\Users\ImMor\AppData\Roaming\npm\node_modules\firebase-tools\lib\deploy\functions\runtimes\node\triggerParser.js:34:15

Edit 2编辑 2

Removing "type":"module" then gives me this error again:删除 "type":"module" 然后再次给我这个错误:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\ky\distribution\index.js
require() of ES modules is not supported.
require() of C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\ky\distribution\index.js from C:\Users\ImMor\Documents\FirebaseFunctions\functions\lib\auth\onDelete\onDeletedUser.f.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\ImMor\Documents\FirebaseFunctions\functions\node_modules\ky\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\Users\ImMor\Documents\FirebaseFunctions\functions\lib\auth\onDelete\onDeletedUser.f.js:27:30)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\Users\ImMor\Documents\FirebaseFunctions\functions\lib\index.js:39:27)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)

Edit 3编辑 3

Following changes have been made:进行了以下更改:

  • Change "module": "commonjs" -> "module": "es6"更改“模块”:“commonjs”->“模块”:“es6”
  • Change "target": "es2017" -> "target": "es6"更改“目标”:“es2017”->“目标”:“es6”
  • Add "moduleResolution": "node" in tsconfig.js (inside compileroptions).添加 "moduleResolution": "node" in tsconfig.js (inside compileroptions)。
  • Add "type":"module" to package.json将“类型”:“模块”添加到 package.json

Now I am getting the following error:现在我收到以下错误:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\ImMor\Documents\FirebaseFunctions\functions\lib\utils\constants.f' imported from C:\Users\ImMor\Documents\FirebaseFunctions\functions\lib\index.js
    at finalizeResolution (internal/modules/esm/resolve.js:276:11)
    at moduleResolve (internal/modules/esm/resolve.js:699:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
    at Loader.resolve (internal/modules/esm/loader.js:86:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:56:40)
    at link (internal/modules/esm/module_job.js:55:36)

Here is the code (constants.f.ts)这是代码(constants.f.ts)

export const constants = { /** * Default firebase-functions region */ region: "europe-west1", } as const; export const constants = { /** * 默认 firebase-functions 区域 */ region: "europe-west1", } as const;

Edit 4编辑 4

Looks like I've fixed all my problems.看起来我已经解决了所有问题。 When using "module": "es6", one has to import its module like this:当使用 "module": "es6" 时,必须像这样导入它的模块:

import { constants } from "./utils/constants.js";

and not like this:而不是这样:

import { constants } from "./utils/constants";

The ending ".js" is important结尾“.js”很重要

Just try this one试试这个

add this into your package.json将此添加到您的 package.json

"type": "module" “类型”:“模块”

as I did below don't forget to restart the typescript server正如我在下面所做的那样,不要忘记重新启动 typescript 服务器

在此处输入图像描述

Quoting from the ESLint documentation :引用ESLint 文档

use .eslintrc.cjs when running ESLint in JavaScript packages that specify "type":"module" in their package.json .在 JavaScript 包中运行 ESLint 时使用.eslintrc.cjs ,在package.json中指定"type":"module" Note that ESLint does not support ESM configuration at this time.请注意,ESLint 目前不支持 ESM 配置。

Since you have "type": "module" in your package.json file you should be fine by renaming ".eslintrc.js" to ".eslintrc.cjs".由于 package.json 文件中有"type": "module" ,因此将“.eslintrc.js”重命名为“.eslintrc.cjs”应该没问题。

In my case, I had "type": "module" in package.json and .eslintrc.cjs (to force commonJS since ESM is not yet supported) and wanted to use ESLint with Standard .在我的例子中,我在 package.json 和.eslintrc.cjs中有"type": "module" (强制 commonJS 因为 ESM 还不被支持)并且想使用 ESLint 和Standard

I had to also install Standard separately as a dev dependency (which you don't need usually): npm i -D standard .我还必须单独安装 Standard 作为开发依赖项(通常不需要): npm i -D standard After VS Code restart, the issue is gone. VS Code 重启后,问题消失了。

暂无
暂无

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

相关问题 eslintrc.js:4 "parserOptions": { ^ SyntaxError: Unexpected token ':' functions predeploy error: 命令以非零退出代码 2 终止 - eslintrc.js:4 "parserOptions": { ^ SyntaxError: Unexpected token ':' functions predeploy error: Command terminated with non-zero exit code 2 "errorMessage": "require is not defined in ES module scope, you can use import instead" 使用 Node.js 18.x 时 - "errorMessage": "require is not defined in ES module scope, you can use import instead" When using Node.js 18.x es模块中的Twilio nodejs - Twilio nodejs in es module service worker 注册失败-Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1) - service worker registration fails-Uncaught SyntaxError: Cannot use import statement outside a module (at firebase-messaging-sw.js:1:1) SyntaxError:无法在具有共享 package 的模块外部使用导入语句 - SyntaxError: Cannot use import statement outside a module with shared package 无法导入模块“lambda_function”:没有名为“pymongo”的模块 - Unable to import module 'lambda_function': No module named 'pymongo' 不能在模块外使用 import 语句,TypeScript NodeJS Firebase Functions 项目 - Cannot use import statement outside a module, TypeScript NodeJS Firebase Functions project 如何在导入时为模块分配别名? - How to assign an alias to a module on import? AWS Lambda - 无法导入模块“lambda_function” - AWS Lambda - unable to import module 'lambda_function' NEXT AUTH + Firebase 无法在模块外导入语句 - NEXT AUTH + Firebase Cannot import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM