Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__dirname err - module build of jimp #1235

Open
Jacknq opened this issue May 9, 2023 · 2 comments · May be fixed by #1237 or #1285
Open

__dirname err - module build of jimp #1235

Jacknq opened this issue May 9, 2023 · 2 comments · May be fixed by #1237 or #1285

Comments

@Jacknq
Copy link

Jacknq commented May 9, 2023

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'

  1. 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]
});
  1. import jimp from importjimp.ts and use somewhere in code read from buffer await Jimp.read(Buffer.from(
  2. 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'
    }
  }
  1. create build using vite - vite build --ssr
  2. 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
  3. 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

https://stackabuse.com/bytes/fix-dirname-is-not-defined-in-es-module-scope-in-javascript-node/

@Javier-Rotelli Javier-Rotelli linked a pull request May 18, 2023 that will close this issue
4 tasks
@xavier-villelegier
Copy link

@Jacknq did you find any workaround while waiting for a fix ? 👀

@Jacknq
Copy link
Author

Jacknq commented Nov 30, 2023

@Jacknq did you find any workaround while waiting for a fix ? 👀

I did, not using jimp but other library that doesnt have that issue

@hipstersmoothie hipstersmoothie linked a pull request Mar 28, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants