![](/img/trans.png)
[英]React Setup 'npm run build' error “unexpected token 'b' at 18:4”
[英]svelte - `npm run build` will get the "unexpected token <" error
完成開發后,我運行npm run build
來構建生產應用程序。 但我會得到:
未捕獲的語法錯誤:意外的標記“<”
來自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
零件的預處理有關。
Update2我只是用新的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
}
};
除了去掉評論,所有的代碼都是下載的全新代碼。
我該如何解決這個問題?
嘗試使用最新的節點版本。
例如:
nvm install 14.17.5
nvm use 14.17.5
nvm
github: https://github.com/nvm-sh/nvm
您也可以使用n
: https://github.com/mklement0/n-install
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.