unsplash / unsplash-js

🤖 Official JavaScript wrapper for the Unsplash API
https://www.npmjs.com/package/unsplash-js
MIT License
2.15k stars 157 forks source link

Cannot use node-fetch for unsplash-js in typescript #192

Open NEARworld opened 2 years ago

NEARworld commented 2 years ago

Steps to Reproduce

src/app.ts

import "dotenv/config";
import express, { Request, Response, NextFunction } from "express";
import { createApi } from "unsplash-js";
import nodeFetch from "node-fetch";
import * as config from "./config";

const app = express();

const unsplash = createApi({
  accessKey: config.accessKey,
  fetch: nodeFetch as unknown as typeof fetch,
});

-

Observed Behaviour

C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\node-fetch\src\index.js from C:\Users\1\Desktop\portfolios\gallery\proxy\src\app.ts not supported.
Instead change the require of index.js in C:\Users\1\Desktop\portfolios\gallery\proxy\src\app.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.require.extensions.<computed> [as .js] (C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\index.js:851:20)
    at Object.<anonymous> (C:\Users\1\Desktop\portfolios\gallery\proxy\src\app.ts:32:38)
    at Module.m._compile (C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\index.js:857:29)
    at Object.require.extensions.<computed> [as .ts] (C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\index.js:859:16)
    at phase4 (C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\bin.js:466:20)
    at bootstrap (C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\bin.js:54:12)
    at main (C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\bin.js:33:12)
    at Object.<anonymous> (C:\Users\1\Desktop\portfolios\gallery\proxy\node_modules\ts-node\dist\bin.js:579:5) {
  code: 'ERR_REQUIRE_ESM'
}

-

Image or video please.

Expected Behaviour

works fine

Technical Notes

node -v
v16.13.2

package.json

  "scripts": {
    "dev": "nodemon --exec npx ts-node src/app.ts",
    "build": "tsc",
    "start": "node ./bin/app.js"
  },
  "dependencies": {
    "@types/express": "^4.17.13",
    "@types/node": "^18.0.6",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "node-fetch": "^3.2.9",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "unsplash-js": "^7.0.15"
  }

tsconfig.json

{
    "compilerOptions": {
        "target": "es2016",
        "module": "commonjs",
        "rootDir": "./src",
        "outDir": "./bin",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true
    }
}

works fine with node-fetch ^2.6.5 but not working with node-fetch ^3.2.9

-