【问题标题】:svelte - `npm run build` will get the "unexpected token <" errorsvelte - `npm run build` 会得到“unexpected token <”错误
【发布时间】:2020-12-15 09:10:13
【问题描述】:

完成开发后,我运行npm run build 来构建生产应用程序。但我会得到:

Uncaught SyntaxError: Unexpected token '

来自bundle.js

我在svelte 3.0.0 上运行。我该如何解决这个问题?

更新1

下面是我的rollup.config.js

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';

import preprocess from 'svelte-preprocess';

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;
    
    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js'
    },
    plugins: [
        svelte({
            dev: !production,

            preprocess: preprocess(),
            css: css => {
                css.write('bundle.css');
            }

        }),
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),

        !production && serve(),

        !production && livereload('public'),

        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

我不太记得了,但我相信我所做的唯一更改是与 css 部分的预处理有关。

更新2 我只是用新的svelte 项目进行测试,没有碰任何东西。 我几乎按照网站上的说明进行操作:

npx degit sveltejs/template my-svelte-project
cd my-svelte-project

npm install
npm run build

唯一不同的是我运行build 而不是dev。并立即得到了同样的错误:

bundle.js:1 Uncaught SyntaxError: Unexpected token '

下面是rollup.config.js

import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import css from 'rollup-plugin-css-only';

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;

    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js'
    },
    plugins: [
        svelte({
            compilerOptions: {
                // enable run-time checks when not in production
                dev: !production
            }
        }),
        css({ output: 'bundle.css' }),

        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),

        !production && serve(),

        !production && livereload('public'),

        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

除了取出cmets外,所有代码都是下载的全新代码。

我该如何解决这个问题?

【问题讨论】:

  • 您是否正在尝试导入特殊文件,例如 .json、图像或 typescript/es6?
  • @johannchopin 我在开发过程中可能会遇到。我确实在 index.html 中导入了一些 js 包。有问题吗?
  • 请分享您的汇总配置以及有关错误的更多详细信息。
  • @johannchopin 我添加了我的汇总配置。问题是,我尝试使用全新的下载项目并构建它,已经从那里得到了问题。我想知道这是否只是我的问题。
  • @sooon 你运行的是什么版本的 node 和 npm?推荐使用 LTS 版本(14.15.2)

标签: javascript svelte


【解决方案1】:

尝试使用最新的节点版本。

例如:

nvm install 14.17.5
nvm use 14.17.5

nvmgithub:https://github.com/nvm-sh/nvm

您也可以使用nhttps://github.com/mklement0/n-install

【讨论】:

    猜你喜欢
    • 2021-03-03
    • 2022-08-17
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 2020-09-02
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多