privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
8.64k stars 132 forks source link

[ESM / Node v22] Broken import of a correctly bundled library #553

Closed damianobarbati closed 1 month ago

damianobarbati commented 1 month ago

Acknowledgements

Minimal reproduction URL

https://github.com/damianobarbati/tsx-bug

How to reproduce:

git clone https://github.com/damianobarbati/tsx-bug
cd tsx-bug
nvm install 
npm install -g yarn
yarn install
yarn start

Result:

$ yarn start
{
  default: [Function: MapsSearch],
  isUnexpected: [Function: isUnexpected]
} [Function: isUnexpected]

Expected:

$ yarn start
[Function: MapsSearch], [Function: isUnexpected]

Code is simple:

import MapsSearch, { isUnexpected } from '@azure-rest/maps-search';
console.log(MapsSearch, isUnexpected);

Tried both the following tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext"
  }
}

and

{
  "compilerOptions": {
    "target": "esnext",
    "moduleResolution": "NodeNext"
  }
}

Version

v4.10.2

Node.js version

v22.1

Package manager

yarn

Operating system

macOS

privatenumber commented 1 month ago

Your reproduction seems to show a different error than what you're reporting:

$ pnpm start

> tsx-starter@ start ~/Downloads/node-cfvrct
> NODE_ENV=development ENVK=.env tsx --no-warnings -r envk --watch ./index.ts

~/Downloads/node-cfvrct/node_modules/.pnpm/@esbuild-kit+cjs-loader@2.4.0/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1
"use strict";var F=require("path"),N=require("fs"),S=require("module"),r=require("@esbuild-kit/core-utils"),l=require("get-tsconfig");function f(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var m=f(F),y=f(N),d=f(S);const P=/^\.{0,2}\//,E=/\.[cm]?tsx?$/,O=`${m.default.sep}node_modules${m.default.sep}`,i=process.env.ESBK_TSCONFIG_PATH?{path:process.env.ESBK_TSCONFIG_PATH,config:l.parseTsconfig(process.env.ESBK_TSCONFIG_PATH)}:l.getTsconfig(),b=i==null?void 0:i.config,_=i&&l.createPathsMatcher(i),h=r.installSourceMapSupport(),j=r.compareNodeVersion([13,2,0])>=0||r.compareNodeVersion([12,20,0])>=0&&r.compareNodeVersion([13,0,0])<0;function v(e,s){process.send&&process.send({type:"dependency",path:s});let t=y.default.readFileSync(s,"utf8");if(s.endsWith(".cjs")&&j){const o=r.transformDynamicImport(s,t);o&&(t=h(o,s))}else{const o=r.transformSync(t,s,{tsconfigRaw:b});t=h(o,s)}e._compile(t,s)}const T=d.default._extensions;[".js",".ts",".tsx",".jsx"].forEach(e=>{T[e]=v}),Object.defineProperty(T,".mjs",{value:v,enumerable:!1});const x=r.compareNodeVersion([16,0,0])>=0||r.compareNodeVersion([14,18,0])>=0,u=d.default._resolveFilename;d.default._resolveFilename=function(e,s,t,o){if(!x&&e.startsWith("node:")&&(e=e.slice(5)),_&&!P.test(e)&&!(s!=null&&s.filename.includes(O))){const a=_(e);for(const c of a){const p=g.call(this,c,s,t,o);if(p)return p;try{return u.call(this,c,s,t,o)}catch{}}}const n=g.call(this,e,s,t,o);return n||u.call(this,e,s,t,o)};function g(e,s,t,o){const n=r.resolveTsPath(e);if(s&&E.test(s.filename)&&n)try{return u.call(this,n,s,t,o)}catch(a){const{code:c}=a;if(c!=="MODULE_NOT_FOUND"&&c!=="ERR_PACKAGE_PATH_NOT_EXPORTED")throw a}}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
TypeError: Cannot read properties of null (reading 'includes')
    at Function.d.default._resolveFilename (~/Downloads/node-cfvrct/node_modules/.pnpm/@esbuild-kit+cjs-loader@2.4.0/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:1284)
    at Function.Module._load (node:internal/modules/cjs/loader:1050:27)
    at internalRequire (node:internal/modules/cjs/loader:213:19)
    at Module._preloadModules (node:internal/modules/cjs/loader:1711:5)
    at loadPreloadModules (node:internal/process/pre_execution:744:5)
    at setupUserModules (node:internal/process/pre_execution:207:5)
    at prepareExecution (node:internal/process/pre_execution:160:5)
    at prepareMainThreadExecution (node:internal/process/pre_execution:54:10)
    at node:internal/main/run_main_module:13:19
Failed running './index.ts'
^C%
$ node -v
v22.1.

Is there a reason why you're using an outdated version of tsx (v3.12.1) when your report says v4.10.2? And are all the files and deps necessary? For example @types/node can definitely be removed.

Providing a minimal reproduction is important for saving each other time. Please let me know when it's ready and I'll re-open this issue.

damianobarbati commented 1 month ago

@privatenumber sorry for this, I created a proper repro on Github as I can't enforce node version on StackBlitz. I updated the description of the issue accordingly. You should be able to reproduce copy-pasting the commands assuming you have nvm. Let me know if I can help further.

privatenumber commented 1 month ago

Duplicate of https://github.com/privatenumber/tsx/issues/38