我在Svelte项目中的.ts文件中同时使用了import
和require
语法,但不知道为什么会出现以下错误:
require is not defined
我想知道是否有我所不知道的东西,或者是我错过了一些简单的东西?
tsconfig.json:
{
"compilerOptions": {
"moduleResolution": "node",
"module": "commonjs",
"lib": ["dom", "es2015", "esnext"],
"target": "es5",
/**
svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
to enforce using \`import type\` instead of \`import\` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
To have warnings/errors of the Svelte compiler at the correct position,
enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"checkJs": true,
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"]
}
sveltekit 配置:
import preprocess from 'svelte-preprocess'
import ssr from '@sveltejs/adapter-static'
/** @type {import('@sveltejs/kit').Config} */
export default {
preprocess: [
preprocess({
defaults: {
style: 'postcss'
},
postcss: true
})
],
kit: {
adapter: ssr(),
target: '#svelte'
}
}
require
,只有import
是有效的语法。 - Stephane Vanraesaxios
),使用vite/Sveltekit构建将创建一个在浏览器中抛出"require未定义"的脚本。 - Clément Renaud