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 a JSON file with import attributes #175

Open
gevalo1 opened this issue Jan 23, 2025 · 2 comments
Open

Error when importing a JSON file with import attributes #175

gevalo1 opened this issue Jan 23, 2025 · 2 comments

Comments

@gevalo1
Copy link

gevalo1 commented Jan 23, 2025

Expected Behavior

No error when using import attributes to import a JSON file (ESM).

Actual Behavior

node --trace-warnings --loader=import-in-the-middle/hook.mjs index.mjs

(node:40984) Warning: Error: 'import-in-the-middle' failed to wrap 'file:///.../iitm-test/index.mjs'
    at getSource (...\iitm-test\node_modules\import-in-the-middle\hook.js:431:21)
    at async load (...\iitm-test\node_modules\import-in-the-middle\hook.js:446:26)
    ... 2 lines matching cause stack trace ...
    at async handleMessage (node:internal/modules/esm/worker:199:18) {
  cause: Error: Failed to parse 'file:///.../iitm-test/index.mjs'
      at getExports (...\iitm-test\node_modules\import-in-the-middle\lib\get-exports.js:123:17)
      at async processModule (...\iitm-test\node_modules\import-in-the-middle\hook.js:198:23)
      at async getSource (...\iitm-test\node_modules\import-in-the-middle\hook.js:395:25)
      at async load (...\iitm-test\node_modules\import-in-the-middle\hook.js:446:26)
      at async nextLoad (node:internal/modules/esm/hooks:748:22)
      at async Hooks.load (node:internal/modules/esm/hooks:385:20)
      at async handleMessage (node:internal/modules/esm/worker:199:18) {
    cause: SyntaxError: Unexpected token (1:31)
        at pp$4.raise (...\iitm-test\node_modules\acorn\dist\acorn.js:3646:15)
        at pp$9.unexpected (...\iitm-test\node_modules\acorn\dist\acorn.js:772:10)
        at pp$9.semicolon (...\iitm-test\node_modules\acorn\dist\acorn.js:749:68)
        at Parser.parseImport (...\iitm-test\node_modules\acorn-import-attributes\lib\index.js:242:12)
        at pp$8.parseStatement (...\iitm-test\node_modules\acorn\dist\acorn.js:948:51)
        at pp$8.parseTopLevel (...\iitm-test\node_modules\acorn\dist\acorn.js:829:23)
        at Parser.parse (...\iitm-test\node_modules\acorn\dist\acorn.js:601:17)
        at Function.parse (...\iitm-test\node_modules\acorn\dist\acorn.js:651:37)
        at getEsmExports (...\iitm-test\node_modules\import-in-the-middle\lib\get-esm-exports.js:37:23)
        at getExports (...\iitm-test\node_modules\import-in-the-middle\lib\get-exports.js:105:14) {
      pos: 31,
      loc: [Position],
      raisedAt: 35
    }
  }
}

Steps to Reproduce the Problem

Create a project with the following contents

package.json

{
  "name": "iitm-import-attributes",
  "version": "1.0.0",
  "main": "index.mjs",
  "type": "module",
  "scripts": {
    "start": "node --trace-warnings --loader=import-in-the-middle/hook.mjs index.mjs"
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "import-in-the-middle": "^1.12.0"
  }
}

index.mjs

import json from './test.json' with { type: 'json' };

console.log(json);

test.json

{}

Run the project

npm run start

Specifications

NodeJS: 22.13.1
NPM: 10.9.2

@timfish
Copy link
Contributor

timfish commented Jan 23, 2025

I guess this is a duplicate of #105

@gevalo1
Copy link
Author

gevalo1 commented Jan 23, 2025

Is it possible to update acorn? It seems the latest version supports import attributes.

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