javascript 构建SvelteKit时出现SyntaxError:请求的模块'intl-messageframe'是CommonJS模块

c0vxltue  于 2023-09-29  发布在  Java
关注(0)|答案(2)|浏览(94)

我在为生产构建时遇到以下错误。下面是在Node v16.20.1上运行的npm run build的输出

npm run build

> [email protected] build
> vite build
vite v4.4.9 building SSR bundle for production...
✓ 123 modules transformed.

node:internal/event_target:1011
  process.nextTick(() => { throw err; });
                           ^
file:///Users/luca/Projects/personal/fpv-finder/.svelte-kit/output/server/chunks/runtime.esm.js:3
import { IntlMessageFormat } from "intl-messageformat";
         ^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'IntlMessageFormat' not found. The requested module 'intl-messageformat' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'intl-messageformat';
const { IntlMessageFormat } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async analyse (file:///Users/luca/Projects/personal/fpv-finder/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:60:16)
    at async MessagePort.<anonymous> (file:///Users/luca/Projects/personal/fpv-finder/node_modules/@sveltejs/kit/src/utils/fork.js:22:16)
Emitted 'error' event on Worker instance at:
    at Worker.[kOnErrorMessage] (node:internal/worker:298:10)
    at Worker.[kOnMessage] (node:internal/worker:309:37)
    at MessagePort.<anonymous> (node:internal/worker:205:57)
    at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:736:20)
    at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)

这就是导致问题的代码。

import { d as derived, w as writable } from "./index.js";
import deepmerge from "deepmerge";
import { IntlMessageFormat } from "intl-messageformat"; // <- This is the issue
function delve(obj, fullKey) {
  if (fullKey == null)
    return void 0;
  if (fullKey in obj) {
    return obj[fullKey];
  }
// [...]  Other code here

有什么办法可以弥补吗?
这里是package.json

{
    "name": "fpv-finder",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "dev": "vite dev",
        "build": "vite build",
        "preview": "vite preview",
        "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
        "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
        "lint": "prettier --plugin-search-dir . --check . && eslint .",
        "format": "prettier --plugin-search-dir . --write ."
    },
    "devDependencies": {
        "@sveltejs/adapter-auto": "^2.1.0",
        "@sveltejs/adapter-netlify": "^2.0.8",
        "@sveltejs/kit": "^1.25.1",
        "@typescript-eslint/eslint-plugin": "^6.7.3",
        "@typescript-eslint/parser": "^6.7.3",
        "autoprefixer": "^10.4.16",
        "eslint": "^8.50.0",
        "eslint-config-prettier": "^9.0.0",
        "eslint-plugin-svelte": "^2.33.2",
        "postcss": "^8.4.30",
        "prettier": "^3.0.3",
        "prettier-plugin-svelte": "^3.0.3",
        "svelte": "^4.2.1",
        "svelte-check": "^3.5.2",
        "tailwindcss": "^3.3.3",
        "tslib": "^2.6.2",
        "typescript": "^5.2.2",
        "vite": "^4.4.9"
    },
    "type": "module",
    "dependencies": {
        "svelte-i18n": "^3.7.4"
    }
}

我所做的步骤只是运行npm run build,我希望项目能够构建。

0md85ypi

0md85ypi1#

有一个svelte-i18n issue with module resolution发生在一些套件适配器上。解决方法似乎是安装较新版本的intl-messageformat
您也可以降级svelte-i18n

esyap4oy

esyap4oy2#

好吧,我找到了解决这个问题的方法。我迈出的第一步就是逃跑

npm ls intl-messageformat
[email protected] /Users/luca/Projects/personal/fpv-finder
└─┬ [email protected]
  └── [email protected]

我发现intl-messageformat不是最新版本。所以我在package.json中添加了这个依赖项。
运行npm install,然后运行npm run build。问题解决了
基本上我做了评论中建议的同样的事情。

相关问题