Comments (14)
If i remove "exports" section from ts-rest's package.json everything works fine
from import-in-the-middle.
Looks like ts-rest has some weird bundling and "import" field in "exports" actually reexports cjs module as esm thus iitm fails to parse exported modules and proxies zero exports
from import-in-the-middle.
I found a workaround:
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const core = require('@ts-rest/core');
const { initContract } = core;
from import-in-the-middle.
Looks like the issue comes from parsing exported modules. If you launch without iitm hook it loads cjs version of package then mjs. iitm parses wrong export and produces wrong virtual proxy file for the library
from import-in-the-middle.
Looks like for some reason iitm treats package as cjs even though it has esm export
from import-in-the-middle.
for some reason iitm treats package as cjs even though it has esm export
Loader hooks (including iitm) load ESM and CJS loaded via ESM modules.
iitm v1.8.1 has been released. Have you tried that yet?
from import-in-the-middle.
I'll try later today
from import-in-the-middle.
Still error but this time with more details
Error
Error: 'import-in-the-middle' failed to wrap 'file:////temp/ts-rest-iitm-issue/node_modules/@ts-rest/core/index.cjs.mjs'
at getSource (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:304:21)
at async load (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:319:26)
at async nextLoad (node:internal/modules/esm/hooks:865:22)
at async Hooks.load (node:internal/modules/esm/hooks:448:20)
at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) {
cause: TypeError: emitImportAssertionWarning is not a function
at defaultLoad (node:internal/modules/esm/load:119:5)
at nextLoad (node:internal/modules/esm/hooks:865:28)
at getExports (\ts-rest-iitm-issue\node_modules\import-in-the-middle\lib\get-exports.js:80:27)
at processModule (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:131:29)
at getSource (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:269:31)
at load (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:319:32)
at nextLoad (node:internal/modules/esm/hooks:865:28)
at Hooks.load (node:internal/modules/esm/hooks:448:26)
at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:826:20)
}
file:////temp/ts-rest-iitm-issue/app.js:1
import { initContract } from "@ts-rest/core";
^^^^^^^^^^^^
SyntaxError: The requested module '@ts-rest/core' does not provide an export named 'initContract'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at async loadESM (node:internal/process/esm_loader:28:7)
at async handleMainPromise (node:internal/modules/run_main:113:12)
Node.js v20.11.1
Here is the reproduction brand https://github.com/TheAsda/iitm-ts-rest-issue/tree/v1.8.1
from import-in-the-middle.
The ".cjs.mjs" file reexports cjs module with export
from import-in-the-middle.
If i replace "import" section with actual esm file different error occurs.
Error
Error: 'import-in-the-middle' failed to wrap 'file:////ts-rest-iitm-issue/node_modules/@ts-rest/core/index.esm.js'
at getSource (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:304:21)
at async load (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:319:26)
at async nextLoad (node:internal/modules/esm/hooks:865:22)
at async Hooks.load (node:internal/modules/esm/hooks:448:20)
at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) {
cause: Error: Unexpected export statement in CJS module.
at @:461:8
at esmSyntaxErr (\ts-rest-iitm-issue\node_modules\cjs-module-lexer\lexer.js:1153:24)
at throwIfExportStatement (\ts-rest-iitm-issue\node_modules\cjs-module-lexer\lexer.js:1193:9)
at parseSource (\ts-rest-iitm-issue\node_modules\cjs-module-lexer\lexer.js:155:13)
at parseCJS (\ts-rest-iitm-issue\node_modules\cjs-module-lexer\lexer.js:43:5)
at getCjsExports (\ts-rest-iitm-issue\node_modules\import-in-the-middle\lib\get-exports.js:37:20)
at getExports (\ts-rest-iitm-issue\node_modules\import-in-the-middle\lib\get-exports.js:100:12)
at async processModule (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:131:23)
at async getSource (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:269:25)
at async load (\ts-rest-iitm-issue\node_modules\import-in-the-middle\hook.js:319:26)
at async nextLoad (node:internal/modules/esm/hooks:865:22) {
code: 'ERR_LEXER_ESM_SYNTAX',
loc: 15778
}
}
node:internal/modules/esm/translators:116
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
^
TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected string, array buffer, or typed array to be returned for the "source" from the "load" function but got undefined.
at assertBufferSource (node:internal/modules/esm/translators:116:9)
at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:163:3)
at callTranslator (node:internal/modules/esm/loader:285:14)
at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:30) {
code: 'ERR_INVALID_RETURN_PROPERTY_VALUE'
}
Node.js v20.11.1
from import-in-the-middle.
So the ".esm.js" file is an actual esm module that exports a bunch of properties
But it's being treated as cjs module for some reason
from import-in-the-middle.
And the behavior when i remove "exports" from the package's package.json and app works fine persists
from import-in-the-middle.
Currently i found a "fix" what temporarily fixes. I'm patching @ts-rest/core
package with yarn patch
and put cjs entrypoint to the "import" and using dynamic import()
.
It looks weird but works)
from import-in-the-middle.
I just tried the latest [email protected] with the latest @ts-rest/[email protected] and it works now with all the cases. Here's the updated branch of the repo.
from import-in-the-middle.
Related Issues (20)
- Use `es-module-lexer` to parse ESM
- Doesn't handle exports with invalid identifiers
- `getFullCjsExports` does not resolve re-exports from external dependencies
- IITM Throws Error When an NPM Package is Not Installed in the Root Folder HOT 7
- Are sequential patched supported? HOT 6
- parentResolve is not a function HOT 1
- Should fail gracefully when exports cannot be parsed
- Explicitly named exports should be exported over `export *` exports HOT 3
- Acorn parser does not support import attributes
- SyntaxError when importing a JSON file using { type: 'json' } HOT 3
- import-in-the-middle' failed to wrap 'file:///app/node_modules/@prisma/client/default.js' HOT 2
- iitm wrappers copy exported primitives HOT 1
- Update LICENSE and NOTICE
- Codify governance HOT 2
- Resolve exports for native modules
- Errors when importing vue/server-renderer
- Import in the middle breaks `drizzle-orm` exports HOT 3
- Unable to import from Vue HOT 1
- `release-please` GHA fails due to expired access token HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from import-in-the-middle.