简体   繁体   English

auth0-js 导入失败 - 使用 typescript + react + nextjs

[英]auth0-js import fails - using typescript + react + nextjs

I'm trying to use auth0-js with a client side project built in react+typescript+nextjs and no matter what I do I keep running into this error:我正在尝试将 auth0-js 与内置于 react+typescript+nextjs 的客户端项目一起使用,无论我做什么,我都会遇到此错误:

Module not found: Error: Can't resolve 'auth0-js' in '/usr/src/app/components/Auth'

I'm attempting to initialize auth0-js like this我正在尝试像这样初始化 auth0-js

import { WebAuth } from 'auth0-js';

const auth = new WebAuth({
  domain: process.env.NEXT_AUTH0_DOMAIN,
  clientID: process.env.NEXT_AUTH0_CLIENTID
});

and I've already installed @types/auth0-js as well because typescript.我也已经安装了@types/auth0-js,因为 typescript。 As far as I can tell, this seems to be some odd configuration with Typescript.据我所知,这似乎是 Typescript 的一些奇怪配置。 I've verified multiple times that the package is in my node-modules folder and I've uninstalled/reinstalled multiple times as well.我已经多次验证 package 位于我的节点模块文件夹中,并且我也多次卸载/重新安装。

I'm not really sure what to do next so if anyone has questions or recommendations, I'd greatly appreciate that!我不确定下一步该怎么做,所以如果有人有问题或建议,我将不胜感激!

Here's my package.json file as well这也是我的 package.json 文件

{
  "name": "br-client",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "test": "jest",
    "storybook": "start-storybook -p 6006 --quiet",
    "build-storybook": "build-storybook",
    "storybook:clean": "rm -rf node_modules/.cache/storybook",
    "json-server": "json-server -p 5000 --host 0.0.0.0 --watch db.json"
  },
  "dependencies": {
    "@fortawesome/fontawesome-svg-core": "^1.2.34",
    "@fortawesome/free-brands-svg-icons": "^5.15.2",
    "@fortawesome/free-regular-svg-icons": "^5.15.2",
    "@fortawesome/free-solid-svg-icons": "^5.15.2",
    "@fortawesome/react-fontawesome": "^0.1.14",
    "@tailwindcss/forms": "^0.2.1",
    "@tailwindcss/postcss7-compat": "^2.0.2",
    "@types/auth0-js": "^9.14.2",
    "@types/lodash-es": "^4.17.4",
    "@types/react-dom": "^17.0.0",
    "@types/react-modal": "^3.12.0",
    "auth0-js": "^9.14.3",
    "autoprefixer": "^9",
    "classnames": "^2.2.6",
    "date-fns": "^2.16.1",
    "firebase": "^8.2.4",
    "lodash-es": "^4.17.20",
    "next": "10.0.5",
    "next-transpile-modules": "^6.0.0",
    "postcss": "^7",
    "rc-slider": "^9.7.1",
    "react": "17.0.1",
    "react-datepicker": "^3.4.1",
    "react-dom": "17.0.1",
    "react-functional-select": "^2.9.5",
    "react-modal": "^3.12.1",
    "react-query": "^3.5.16",
    "react-window": "^1.8.6",
    "recoil": "^0.1.2",
    "styled-components": "^5.2.1",
    "styled-jsx-plugin-postcss": "^4.0.0",
    "tailwindcss": "npm:@tailwindcss/postcss7-compat"
  },
  "devDependencies": {
    "@babel/core": "^7.12.10",
    "@next/bundle-analyzer": "^10.0.5",
    "@storybook/addon-actions": "^6.1.15",
    "@storybook/addon-essentials": "^6.1.15",
    "@storybook/addon-links": "^6.1.15",
    "@storybook/react": "^6.1.15",
    "@testing-library/jest-dom": "^5.11.8",
    "@testing-library/react": "^11.2.3",
    "@types/node": "^14.14.20",
    "@types/react": "^17.0.0",
    "babel-jest": "^26.6.3",
    "babel-loader": "^8.2.2",
    "husky": ">=4",
    "import-sort-style-module": "^6.0.0",
    "jest": "^26.6.3",
    "json-server": "^0.16.3",
    "lint-staged": ">=10",
    "prettier": "^2.2.1",
    "prettier-plugin-import-sort": "^0.0.6",
    "tsconfig-paths-webpack-plugin": "^3.3.0",
    "typescript": "^4.1.3"
  },
  "importSort": {
    ".js, .jsx, .ts, .tsx": {
      "style": "module",
      "parser": "typescript"
    }
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "pre-push": "tsc --noEmit"
    }
  },
  "lint-staged": {
    "*.{js,jsx,ts,tsx,css,scss,md,json}": "prettier --write"
  }
}

and here's my tsconfig.json这是我的 tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": ".",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "paths": {
      "@api/*": ["api/*"],
      "@atoms/*": ["atoms/*"],
      "@components/*": ["components/*"],
      "@hooks/*": ["hooks/*"],
      "@pages/*": ["pages/*"],
      "@root/*": ["./*"],
      // TODO: Rename this because it could conflict with
      // definitely-typed definitions :facepalm:
      "@typedefs/*": ["typedefs/*"],
      "@utils/*": ["utils/*"]
    },
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": false,
    "target": "es5"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

Thanks in advance!提前致谢!

I believe you are using the wrong package.我相信你用错了 package。

Auth0.js is a "Client Side JavaScript toolkit for Auth0 API." Auth0.js“用于 Auth0 API 的客户端 JavaScript 工具包”。 and "After installing the auth0-js module, you'll need bundle it up along with all of its dependencies."和“安装 auth0-js 模块后,您需要将其与其所有依赖项捆绑在一起。” TLDR: It is meant to be used in the browser . TLDR:它旨在用于浏览器

What you are looking for is auth0/nextjs-auth0 , find it here .您正在寻找的是auth0/nextjs-auth0 ,在这里找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM