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

[Bug?]: Unknown file extension ".json" in the PNP Loader (ESM, Import Assertions) #4681

Closed
1 task
samuelexferri opened this issue Jul 27, 2022 · 1 comment
Closed
1 task
Labels
bug Something isn't working

Comments

@samuelexferri
Copy link

samuelexferri commented Jul 27, 2022

Self-service

  • I'd be willing to implement a fix

Describe the bug

I'm using Node v18 with ESM and --experimental-json-modules flag active in order to use the import assertion for JSON files like this:

import genericERC20 from '../json/FILE.json' assert {type: "json"};

With Yarn v3.2.2 (Zero Install, PNP) I get this error:

Error: Unknown file extension ".json" for /Users/samuelexferri/GITHUB/PROJECT/dist/json/FILE.json

Solutions?

To reproduce

I'm trying to import into a .ts file a json file using Node v18 and --experimental-json-modules flag like this:

import genericERC20 from '../abi/GenericERC20.json' assert {type: "json"};

But when I try to run the script, I obtain this error:

You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
Error: Unknown file extension ".json" for /Users/samuelexferri/GITHUB/PROJECT/dist/json/FILE.json
    at getFileFormat (file:///Users/samuelexferri/GITHUB/PROJECT/.pnp.loader.mjs:129:13)
    at load$1 (file:///Users/samuelexferri/GitHub/PROJECT.pnp.loader.mjs:177:18)
    at ESMLoader.load (node:internal/modules/esm/loader:431:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:350:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:369:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:328:34)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:82:21)
    at async Promise.all (index 3)
    at link (node:internal/modules/esm/module_job:87:9)

So I cannot import a JSON file through the .pnp.loader.mjs.

I run the script with NO ISSUES (so JSON correctly imported) deleting lines 128:130 of .pnp.loader.mjs but every time I will run yarn install I will need to remove these lines again.

case `.json`: {
    throw new Error(`Unknown file extension ".json" for ${filepath}`);
}

Environment

System:
  OS: macOS 12.4
  CPU: (10) arm64 Apple M1 Pro
Binaries:
  Node: 18.2.0 - /private/var/folders/0n/cdtksrxx2cj3zvnnq066_hvh0000gn/T/xfs-55b42ae9/node
  Yarn: 3.2.2 - /private/var/folders/0n/cdtksrxx2cj3zvnnq066_hvh0000gn/T/xfs-55b42ae9/yarn
  npm: 8.9.0 - ~/.nvm/versions/node/v18.2.0/bin/npm

Additional context

My .yarnrc:

yarnPath: .yarn/releases/yarn-3.2.2.cjs

nodeLinker: pnp

pnpEnableEsmLoader: true

My .tsconfig:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "nodenext",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "typeRoots": [
      /* List of folders to include type definitions from */
      "./types",
      "./node_modules/@types",
      "./src/types"
    ]
  },
  "include": [
    "**/*"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}
@samuelexferri samuelexferri added the bug Something isn't working label Jul 27, 2022
@merceyz
Copy link
Member

merceyz commented Jul 27, 2022

Duplicate of #4245

@merceyz merceyz marked this as a duplicate of #4245 Jul 27, 2022
@merceyz merceyz closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants