You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
IT shoud do No error when using bundlers like Vite
__dirname is not defined in ES module scope error
on this line:
const dir=process.env.DIRNAME||${__dirname}/../,print=()=>({constants:{measureText,measureTextHeight,FONT_SANS_8_BLACK:
im using typescript 4.9
syntethicdefaults import true
but importing es module version 'jimp/es'
create importjimp.ts with following:
import configure from "@jimp/custom";
import types from "@jimp/types";
import plugins from "@jimp/plugins";
export default configure({
types: [types],
plugins: [plugins]
});
import jimp from importjimp.ts and use somewhere in code read from buffer await Jimp.read(Buffer.from(
use Vite as bundler
with following configs
...
import path from 'path'
import {builtinModules} from 'module'
import {VitePluginNode} from 'vite-plugin-node';
import checker from 'vite-plugin-checker';
// Configure Vitest (https://vitest.dev/config/)
import {defineConfig, ViteDevServer, Connect, loadEnv} from 'vite'
... // relevant part of the vite config
ssr:{ noExternal: true },
plugins: [
checker({
typescript: true,
}),
...VitePluginNode({
adapter: 'express',
appPath: AppMainPath,
exportName: 'viteNodeApp',
tsCompiler: 'esbuild',
swcOptions: {}
}),
],
test: {
/* for example, use global to avoid globals imports (describe, test, expect): */
// globals: true,
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
build:
{
target: 'esnext',//or module
ssr: AppMainPath,
minify: true,// for-dev false
manifest: true,
commonjsOptions: {
transformMixedEsModules: true,
},
rollupOptions: {
input: AppMainPath, // in case we have problem with some deps,
// but rather remove them to make clean build!
external: builtinModules
},
outDir: './dist'
}
}
create build using vite - vite build --ssr
run vite preview - of that build - there are no externals so jimp gets compiled in bundle
or run the file build directly using node like: node dist/server.mjs
you get it...
Screenshots
using gimp in backend app, hosted via vite with typescript option, es module compilation fails although using jimp/es
Jimp Version: 0.22.7
Operating System: linux
Node version:18.3
Failure Logs
ReferenceError: __dirname is not defined in ES module scope
at file:///..app/dist/server.mjs:474:21033
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
sugested fix - in your source - quite straightforward
IT shoud do No error when using bundlers like Vite
__dirname is not defined in ES module scope error
on this line:
const dir=process.env.DIRNAME||
${__dirname}/../
,print=()=>({constants:{measureText,measureTextHeight,FONT_SANS_8_BLACK:im using typescript 4.9
syntethicdefaults import true
but importing es module version 'jimp/es'
await Jimp.read(Buffer.from(
with following configs
...
vite build --ssr
or run the file build directly using node like: node dist/server.mjs
Screenshots
using gimp in backend app, hosted via vite with typescript option, es module compilation fails although using jimp/es
Failure Logs
ReferenceError: __dirname is not defined in ES module scope
at file:///..app/dist/server.mjs:474:21033
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
sugested fix - in your source - quite straightforward
https://stackabuse.com/bytes/fix-dirname-is-not-defined-in-es-module-scope-in-javascript-node/
The text was updated successfully, but these errors were encountered: