NodeJS Laravel Vite构建错误:__vite-browser-externaL未导出'resolve'

xam8gpfp  于 9个月前  发布在  Node.js
关注(0)|答案(1)|浏览(315)

我尝试在Laravel项目中构建和版本化生产资源。我使用laravel-vite-plugin版本0.6.1,节点版本v16.17.1,npm版本8.19.2。为此,当我运行npm run build时,我得到以下错误。

> 'resolve' is not exported by __vite-browser-external, imported by node_modules/vite/dist/node/constants.js
>     file: /var/www/html/node_modules/vite/dist/node/constants.js:1:15
>     1: import path, { resolve } from 'node:path';
>                     ^
>     2: import { fileURLToPath } from 'node:url';
>     error during build:
>     Error: 'resolve' is not exported by __vite-browser-external, imported by node_modules/vite/dist/node/constants.js
>         at error (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:1858:30)
>         at Module.error (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:12429:16)
>         at Module.traceVariable (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:12788:29)
>         at ModuleScope.findVariable (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:11440:39)
>         at Identifier.bind (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:7439:40)
>         at CallExpression.bind (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
>         at CallExpression.bind (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:8935:15)
>         at VariableDeclarator.bind (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:5269:23)
>         at VariableDeclaration.bind (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:5265:73)
>         at Program.bind (file:///var/www/html/node_modules/rollup/dist/es/shared/rollup.js:5265:73)

字符串

下面是我的Vite配置。

vite.config.js

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: [
                'resources/scss/style.scss',
                'resources/scss/theme.scss',
                'resources/js/bundle.js',
                'resources/js/scripts.js',
                'resources/js/charts/analytics-chart.js',
            ],
            refresh: true,
        }),
    ],
});

lrl1mhuk

lrl1mhuk1#

您遇到的错误可能是由于您使用的Node.js版本和Vite版本之间的兼容性问题。
Vite 2.7.0及更高版本已开始使用Node.js内置模块(如“node:path”),这些模块仅在Node.js 16.5.0及更高版本中可用。然而,这些内置模块在某些环境中不完全支持,这可能会导致您看到的错误。
要解决此问题,您可以尝试将Vite降级到不使用Node.js内置模块的版本。例如,您可以降级到Vite 2.6.14:

npm uninstall vite
npm install [email protected]

字符串
然后再次尝试运行npm run build
或者,如果您想使用最新版本的Vite,您可以将Node.js升级到最新版本(当前为17.3.0)或最新LTS版本(当前为16.13.1)。但是,请注意,如果您的环境不完全支持Node.js内置模块,这可能无法解决问题。

相关问题