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 'module' has already been declared #370

Closed
kolodziejczak-sz opened this issue Sep 6, 2020 · 4 comments
Closed

Comments

@kolodziejczak-sz
Copy link

kolodziejczak-sz commented Sep 6, 2020

Simplified version of code that can reproduce this error:

// file.ts

const { builtinModules } from 'module';
export const foo = () => builtinModules ;

// build.js

const { builtinModules } = require('module');
const { buildSync } = require('esbuild');

buildSync({
    entryPoints: ['./file.ts'],
    write: true,
    outdir: distPath,
    minify: false,
    bundle: true,
    format: 'cjs',
    external: builtinModules,
});

node build.js ends up with following error:

const module = __toModule(require("module"));
^
SyntaxError: Identifier 'module' has already been declared
at wrapSafe (internal/modules/cjs/loader.js:1054:16)
at Module._compile (internal/modules/cjs/loader.js:1102:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47

I guess "module" is restricted keyword when it comes to cjs format
node v12.18.0
esbuild v0.6.30,

@evanw
Copy link
Owner

evanw commented Sep 6, 2020

I can't reproduce this. The code esbuild generates on my end is this:

const module2 = __toModule(require("module"));

Here's a live link to the code: https://repl.it/@EvanWallace/FlimsyDetailedSequences#index.js

@kolodziejczak-sz
Copy link
Author

kolodziejczak-sz commented Sep 8, 2020

i created repo with code:
https://github.com/kolodziejczak-sz/esbuild-issue-reproduce
please clone and run npm start to reproduce error

@kolodziejczak-sz
Copy link
Author

kolodziejczak-sz commented Sep 8, 2020

it's really about the line: "const module = require("module")" - "module" is reserved keyword when it comes to node execution
As you can see in the repo:
const { builinModules }= require("module");
is being transpiled to:
const module = __toModule(require("module"));

although i find it hard to explain why your repl doesn't have this problem

@evanw evanw closed this as completed in 9cf70be Sep 9, 2020
@evanw
Copy link
Owner

evanw commented Sep 9, 2020

The fix was just released in version 0.6.33.

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

No branches or pull requests

2 participants