sindresorhus / find-up

Find a file or directory by walking up parent directories
MIT License
582 stars 40 forks source link

yarn pnp incompatability #64

Closed xenoterracide closed 2 years ago

xenoterracide commented 2 years ago

if memory serves correctly this is due to an improper dynamic require...

*  Executing task: yarn run serve 

/Users/nqy642/IdeaProjects/E1-Http/.pnp.cjs:16411
        Error.captureStackTrace(err);
              ^

Error: require() of ES Module /Users/nqy642/IdeaProjects/E1-Http/.yarn/cache/find-up-npm-6.3.0-e5056fc655-9a21b7f924.zip/node_modules/find-up/index.js from /Users/nqy642/IdeaProjects/E1-Http/packages/test-server/dist/app.js not supported.
Instead change the require of index.js in /Users/nqy642/IdeaProjects/E1-Http/packages/test-server/dist/app.js to a dynamic import() which is available in all CommonJS modules.
    at Object.require$$0.Module._extensions..js (/Users/nqy642/IdeaProjects/E1-Http/.pnp.cjs:16411:15)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.require$$0.Module._load (/Users/nqy642/IdeaProjects/E1-Http/.pnp.cjs:16255:14)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/nqy642/IdeaProjects/E1-Http/packages/test-server/dist/app.js:31:19)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Object.require$$0.Module._extensions..js (/Users/nqy642/IdeaProjects/E1-Http/.pnp.cjs:16415:33)
    at Module.load (node:internal/modules/cjs/loader:981:32) {
  code: 'ERR_REQUIRE_ESM'
}
import dotenv from 'dotenv'
import express from 'express'
import { findUpSync } from 'find-up'
import _ from 'lodash/fp'
import log4js from 'log4js'
import process, { env } from 'node:process'

dotenv.config({ path: findUpSync('.env') })

const log = log4js.getLogger()
log4js.configure({
  appenders: { out: { type: 'stdout', layout: { type: 'pattern', pattern: '[%[%p%]] %m' } } },
  categories: { default: { appenders: ['out'], level: 'info' } },
})

const app = express()

app.get('/v1/test/test0', (_req, res) => res.send({ data: { test0: 'value0' } }))
app.get('/v1/test/test1', (_req, res) => res.send({ data: { test1: 'value1' } }))
app.get('/v1/test/test2', (_req, res) => res.send({ data: { test2: 'value2' } }))

app.get('/v1/:lockbox/:key', (req, res) => {
  const { key, lockbox } = req.params
  log.info('env', env)
  const value = env[`VAULT_${lockbox.toUpperCase()}_${key.toUpperCase()}`]

  if (value == null) {
    res.sendStatus(404)
    return
  }
  res.send(_.set(value, `data.${key}`, { data: {} }))
})

app.get('/*', (req, res) => res.send(req.url))

const listener = app.listen(3000, () => {
  log.info('listening on localhost', listener.address(), app.routes)
})

process.once('SIGTERM', () => process.exit(0))
├─ @cof/e1-test-server@workspace:packages/test-server [2a940]
│  └─ find-up@npm:6.3.0 (via npm:^6.3.0)
  System:
    OS: macOS 11.6.6
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 16.14.2 - /private/var/folders/xk/s3n8ww014pzbx2sn30ctz7000000gq/T/xfs-cbdcd3c2/node
    Yarn: 3.2.1 - /private/var/folders/xk/s3n8ww014pzbx2sn30ctz7000000gq/T/xfs-cbdcd3c2/yarn
    npm: 8.6.0 - ~/.asdf/plugins/nodejs/shims/npm
sindresorhus commented 2 years ago

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

xenoterracide commented 2 years ago

ok, but shouldn't this setting allow me to import it? I do believe yarn supports ESM now. If not which version do I need?

{
  "compilerOptions": {
    "esModuleInterop": true,
    "target": "ES2018",
    "moduleResolution": "node",
    "sourceMap": true,
    "lib": ["es2018"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declaration": true,
    "baseUrl": ".",
    "module": "CommonJS",
    "allowJs": false,
    "checkJs": false,
    "noImplicitAny": true,
    "declarationMap": true,
    "strict": true,
    "skipLibCheck": false,
    "strictNullChecks": true,
    "resolveJsonModule": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "noEmit": true
  },
  "exclude": ["node_modules", "dist"],
  "files": []
}
xenoterracide commented 2 years ago

I tried moving to ESM but had an issue with an eslint import rules, which I don't want to get rid of. I dislike typescripts support tbh. I kind of wish typescript would either allow import './foo.ts`` or just transpile import./footo./foo.jsbecauseimport 'foo.js' was what was screwing the rule because it couldn't find it, because output was not the same directory.

sindresorhus commented 2 years ago

ok, but shouldn't this setting allow me to import it?

No: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm