[英]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您也可以使用
n
: https://github.com/mklement0/n-install
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.