blakeembrey / change-case

Convert strings between camelCase, PascalCase, Capital Case, snake_case and more
MIT License
2.21k stars 92 forks source link

Package subpath './keys' is not defined by "exports" #325

Closed Mnigos closed 6 months ago

Mnigos commented 6 months ago

Error: Package subpath './keys' is not defined by "exports" in /Users/igormakowski/Documents/repositories/rigtch/rigtch-music-api/node_modules/change-case/package.json at exportsNotFound (node:internal/modules/esm/resolve:294:10) at packageExportsResolve (node:internal/modules/esm/resolve:584:13) at resolveExports (node:internal/modules/cjs/loader:591:36) at Function.Module._findPath (node:internal/modules/cjs/loader:668:31) at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1130:27) at Function.Module._load (node:internal/modules/cjs/loader:985:27) at Module.require (node:internal/modules/cjs/loader:1235:19) at require (node:internal/modules/helpers:176:18) at Object. (/Users/igormakowski/Documents/repositories/rigtch/rigtch-music-api/dist/modules/spotify/auth/spotify-auth.service.js:15:55) at Module._compile (node:internal/modules/cjs/loader:1376:14)

  import * as keys from 'change-case/keys'

Also tried

import { camelCase } from 'change-case/keys'
blakeembrey commented 6 months ago

This package only supports ESM now, so you can’t use a CJS loader. Based on the error you shared, you’re using CJS still (probably by transpiling TypeScript with module set to CommonJS).

If that’s not the case, do let me know, maybe something else is going on.

nixstrom commented 6 months ago

@blakeembrey

I am facing a similar problem. When importing from change-case/keys, I get a TS error.

import { camelCase } from 'change-case/keys'
// "Cannot find module 'change-case/keys' or its corresponding type declarations."

Importing from change-case works fine, however.

Both package.json and tsconfig.json is set to use ESM.

How can I help?

blakeembrey commented 6 months ago

@nixstrom can you share your tsconfig? Most likely you don’t have module resolution set to Node16 or NodeNext, but it’s hard to tell without anything to debug.

nixstrom commented 6 months ago

@blakeembrey Sorry, I was a little fast on the trigger. It was indeed due to the module resolution. All is good!

blakeembrey commented 6 months ago

Awesome, glad it works! It’s definitely confusing space to figure out what’s missing with the half dozen different things needing to toggle to support a feature.

5t33 commented 2 months ago

Also having this issue and I'm not able to solve it with exports. Or rather, I kind of am, but it requires me to set "type":"module" in my package.json which requires me to change every import to ".js" which I really don't want to do.

my tsconfig:

{
  "compilerOptions": {
    "resolveJsonModule":  true,
    "moduleResolution": "NodeNext",
    "module": "NodeNext",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2020",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "esModuleInterop": true,
  },
  "watchOptions": {
    "watchFile": "fixedPollingInterval"
  }
}

it also seems to be happening for https://github.com/helmetjs, which works with commonjs, but isn't an option here.