简体   繁体   English

svelte - `npm run build` 将得到“unexpected token <”错误

[英]svelte - `npm run build` will get the "unexpected token <" error

After done with my development, I run npm run build to build the production app.完成开发后,我运行npm run build来构建生产应用程序。 but I will get:但我会得到:

Uncaught SyntaxError: Unexpected token '<'未捕获的语法错误:意外的标记“<”

from bundle.js .来自bundle.js

I am running on svelte 3.0.0 .我在svelte 3.0.0上运行。 How can I resolve this?我该如何解决这个问题?

Update1更新1

Below is my rollup.config.js :下面是我的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
    }
};

I don't quite remember, but I believe the only changes I made is all that related to preprocess for the css part.我不太记得了,但我相信我所做的唯一更改是与css零件的预处理有关。

Update2 I just test with the new svelte project without touching anything. Update2我只是用新的svelte项目进行测试,没有碰任何东西。 I practically follow the instructions from the website:我几乎按照网站上的说明进行操作:

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

npm install
npm run build

the only different is I run build rather than dev .唯一不同的是我运行build而不是dev and immediately already got the same error:并立即得到了同样的错误:

bundle.js:1 Uncaught SyntaxError: Unexpected token '<' bundle.js:1 Uncaught SyntaxError: Unexpected token '<'

below is the rollup.config.js :下面是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
    }
};

Other than taking out the comments, all the code is the brand new from the download.除了去掉评论,所有的代码都是下载的全新代码。

How can I resolve this issue?我该如何解决这个问题?

Try using latest node version.尝试使用最新的节点版本。

For example:例如:

nvm install 14.17.5
nvm use 14.17.5

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

You can use n also: https://github.com/mklement0/n-install您也可以使用nhttps://github.com/mklement0/n-install

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

相关问题 React Setup&#39;npm run build&#39;错误“意外的令牌&#39;b&#39;在18:4” - React Setup 'npm run build' error “unexpected token 'b' at 18:4” “npm run build”失败并出现 SyntaxError:Unexpected token - "npm run build" fails with SyntaxError:Unexpected token Vue CLI npm run build 抛出语法错误:意外令牌 - 但代码看起来不错 - Vue CLI npm run build throws Syntax Error: Unexpected token - but code looks fine npm启动时模块构建失败并出现意外令牌错误-react / webpack - Module Build Failure with Unexpected token error on npm start - react/webpack npm运行构建时出错 - Error on npm run build 如何使 index.html 出现在苗条 npm 运行构建中 - How to make index.html appear in svelte npm run build npm 运行 build-storybook 失败并显示“模块解析失败:意外令牌 (20:25)” - npm run build-storybook fails with “Module parse failed: Unexpected token (20:25)” JS 和 Svelte:将 JS 转换为 GeoJSON 时出错 - 解析失败意外令牌 (1:7) - JS and Svelte: Error converting JS to GeoJSON - Parse failure Unexpected token (1:7) 当我运行 npm 运行构建时出现此错误? - When I run npm run build I get this error? Angular通用意外令牌{在npm run start:ssr上 - Angular universal unexpected token { on npm run start:ssr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM