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

Error when importing path-parser module #1102

Closed
Termina1 opened this issue Apr 2, 2021 · 1 comment · Fixed by #1103
Closed

Error when importing path-parser module #1102

Termina1 opened this issue Apr 2, 2021 · 1 comment · Fixed by #1103

Comments

@Termina1
Copy link

Termina1 commented Apr 2, 2021

Found strange behaviour: when I'm trying to import path-parser as ES6 module

import { Path } from 'path-parser'
const path = new p.Path('/users/:id')
path.test('/users/00123')

then after bundling I get runtime error

Uncaught TypeError: Cannot read property 'default' of undefined

Screenshot 2021-04-02 at 21 23 17

I think it is somehow connected to the way path-parser imports tslib. However, if I require path-parser as cjs module

const p = require('path-parser')
const path = new p.Path('/users/:id')
path.test('/users/00123')

then eveything works.
Or maybe because tslib has different entries for es6 and cjs.

@evanw
Copy link
Owner

evanw commented Apr 2, 2021

I can't reproduce this with the exact steps above, but I can reproduce this when the code is run in the global scope instead of in a module (note the change from new p.Path to new Path, which I assume is a typo):

$ cat entry.js
import { Path } from 'path-parser'
const path = new Path('/users/:id')
console.log(path.test('/users/00123'))
$ esbuild --bundle entry.js --outfile=out.cjs --platform=node
$ node out.cjs
{ id: '00123' }
$ esbuild --bundle entry.js --outfile=out.mjs --platform=node
$ node out.mjs
{ id: '00123' }
$ esbuild --bundle entry.js --platform=node | node
[stdin]:1216
var path = new import_path_parser.Path("/users/:id");
                                  ^

TypeError: Cannot read property 'Path' of undefined

The problem appears to be that the tslib library mutates the value of the __exportStar symbol in the global scope and overwrites esbuild's internal code with code that has different behavior. The difference between import and require is that import calls esbuild's __exportStar function while require does not.

Can you confirm whether you are running your code in the global scope or not? If so, one solution could be to not run your code in the global scope, either by saving it to a file and running it or by using --format=iife to wrap your code in a closure. Setting --platform=node implicitly sets --format=cjs which expects the code to be running inside a CommonJS closure, which is not in the global scope.

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