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

SyntaxError: Identifier '__filename' has already been declared #150

Closed
bashmish opened this issue Jun 5, 2023 · 3 comments · Fixed by #153
Closed

SyntaxError: Identifier '__filename' has already been declared #150

bashmish opened this issue Jun 5, 2023 · 3 comments · Fixed by #153

Comments

@bashmish
Copy link
Contributor

bashmish commented Jun 5, 2023

I'm using the esbuild plugin @chialab/esbuild-plugin-commonjs to transpile named exports from CJS to EMS, the esbuild config is pretty much the following (but you can find all details in bashmish/storybook-builder-wds#2):

import { build } from 'esbuild';
import esbuildCommonjsPlugin from '@chialab/esbuild-plugin-commonjs';

// ...

await build({
  entryPoints: modules,
  outdir: './my-dir',
  bundle: true,
  format: 'esm',
  splitting: true,
  sourcemap: true,
  alias: {
    assert: require.resolve('browser-assert'),
    lodash: dirname(require.resolve('lodash-es/package.json')),
    path: require.resolve('path-browserify'),
  },
  plugins: [esbuildCommonjsPlugin()],
});

I run into the issue with the following syntax:

const require = __moduleCreateRequire(import.meta.url);
const __filename = new URL(import.meta.url).pathname;
const __dirname = __pathDirname(__filename);

found in 2 package:

resulting in the following error:

ERR! /path/to/node_modules/@chialab/estransform/dist/index.js:62
ERR! const __filename = new URL(__esbuild_register_import_meta_url__).pathname;
ERR!       ^
ERR!
ERR! SyntaxError: Identifier '__filename' has already been declared
ERR!     at internalCompileFunction (node:internal/vm:73:18)
ERR!     at wrapSafe (node:internal/modules/cjs/loader:1176:20)
ERR!     at Module._compile (node:internal/modules/cjs/loader:1218:27)
ERR!     at Module._compile (/path/to/node_modules/esbuild-register/dist/node.js:2258:26)
ERR!     at extensions..js (/path/to/node_modules/esbuild-register/dist/node.js:4814:15)
ERR!     at Module.load (node:internal/modules/cjs/loader:1117:32)
ERR!     at Module._load (node:internal/modules/cjs/loader:958:12)
ERR!     at Module.require (node:internal/modules/cjs/loader:1141:19)
ERR!     at require (node:internal/modules/cjs/helpers:110:18)
ERR!     at Object.<anonymous> (/path/to/node_modules/@chialab/cjs-to-esm/lib/index.js:1:118)
ERR!  /path/to/node_modules/@chialab/estransform/dist/index.js:62
@bashmish
Copy link
Contributor Author

bashmish commented Jun 5, 2023

My workaround is just to remove these 3 lines from both packages:

None of these 3 const is used anywhere below, so these patches were OK.

Renaming __filename to filename and __dirname to dirname works too.

I think __filename and __dirname are reserved in Node and you can't redefine them, especially not with const. But this code is maybe targeting browser for some reason to polyfill/browserify them? Maybe some config of the bundler/compiler should be fixed.

@edoardocavazza
Copy link
Member

Hi @bashmish, thanks for the report!

It seems that the problem is due to the fact that the packages are meant to be used as ESM modules.
As long as we can, we would like to avoid distributing the commonjs compatible version as well.

However, actually those variables are not used in the distribution files, so we have removed them in this PR. We hope it will solve the problem. Alternatively, we will consider distributing the package in the two formats.

@bashmish
Copy link
Contributor Author

bashmish commented Jun 8, 2023

@edoardocavazza thanks for a quick fix!

Indeed I use it in the CommonJS module. Looking forward to the release and gonna test it right away.

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