11

Problem

I tried to setup path alias in my TS project, but failed to get it run. I encountered two errors when npm start:

  • Module not found: Error: Can't resolve '~/App' in 'D:\work\workbench\templates\template-fe-pure\src' (even though VSC can still looked where is the location of imported files; aka no squiggly line)
  • Resolve error: Cannot find module 'node:fs'

How I do path alias import:
import App from "./App"; -> import App from "~/App";
In tsconfig.json: "baseUrl": ".","paths": {"~/*": ["./src/*"]}, (full file seen below)

Environment

Files

.eslintrc.json

{
  "root": true,
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended",
    "plugin:react-hooks/recommended",
    "plugin:jsx-a11y/recommended",
    "plugin:import/recommended",
    "plugin:import/typescript",
    "plugin:eslint-comments/recommended",
    "plugin:prettier/recommended",
    "prettier"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "sourceType": "module",
    "ecmaVersion": 6,
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "jest": true,
    "node": true
  },
  "settings": {
    "react": {
      "version": "detect"
    },
    "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"],
        "moduleDirectory": ["src", "node_modules"]
      },
      "typescript": {
        "alwaysTryTypes": true,
        "project": "./tsconfig.json"
      }
    }
  },
  "rules": {
    // eslint rules go here ...
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "esnext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "baseUrl": ".",
    "paths": {
      "~/*": ["./src/*"]
    },
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "exclude": ["**/node_modules", "**/.*/"]
}

Stack Trace

ERROR

[eslint] 
src\index.tsx
  Line 1:1:   Resolve error: Cannot find module 'node:fs'
Require stack:
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-import-resolver-typescript\lib\index.cjs
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-module-utils\resolve.js
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-plugin-import\lib\rules\no-unresolved.js
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-plugin-import\lib\index.js
- D:\work\workbench\templates\template-fe-pure\node_modules\@eslint\eslintrc\dist\eslintrc.cjs
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (D:\work\workbench\templates\template-fe-pure\node_modules\eslint-import-resolver-typescript\lib\index.cjs:5:10)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)  import/namespace
  Line 1:1:   Resolve error: Cannot find module 'node:fs'
Require stack:
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-import-resolver-typescript\lib\index.cjs
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-module-utils\resolve.js
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-plugin-import\lib\rules\no-unresolved.js
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-plugin-import\lib\index.js
- D:\work\workbench\templates\template-fe-pure\node_modules\@eslint\eslintrc\dist\eslintrc.cjs
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (D:\work\workbench\templates\template-fe-pure\node_modules\eslint-import-resolver-typescript\lib\index.cjs:5:10)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)  import/no-unresolved
  Line 1:1:   Resolve error: Cannot find module 'node:fs'
Require stack:
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-import-resolver-typescript\lib\index.cjs
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-module-utils\resolve.js
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-plugin-import\lib\rules\no-unresolved.js
- D:\work\workbench\templates\template-fe-pure\node_modules\eslint-plugin-import\lib\index.js
- D:\work\workbench\templates\template-fe-pure\node_modules\@eslint\eslintrc\dist\eslintrc.cjs
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (D:\work\workbench\templates\template-fe-pure\node_modules\eslint-import-resolver-typescript\lib\index.cjs:5:10)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)  import/default
  Line 4:17:  Unable to resolve path to module '~/App'   import/no-unresolved

Search for the keywords to learn more about each error.
1
  • This is probably what cause my error and I suggest anyone having the same problem give this a read. You won't regret it. Commented Jul 14, 2022 at 11:58

2 Answers 2

11

TypeScript support for the new node: protocol was added to @types/[email protected] so you can try installing that. Otherwise, you can try downgrading to [email protected] which does not use the new node: protocol syntax.

Other than that your configuration seems fine.

Sign up to request clarification or add additional context in comments.

3 Comments

I've upgraded Nodejs and it solved the issue (from ~14 to 18)
I see this issue with Node 14.17.0, but not with Node 14.21.3. Using [email protected] worked for me.
I had the problem even though I have Node 16.5.1, but downgrading to [email protected] did help.
2

I had the same problem. I spent several hours fixing it. Finally, I detected the problem in the node version. Please upgrade it to v.16 with this instruction.

4 Comments

Sorry it took a while answering you. But sadly, it does not worked in my situation. I fixed it sloppily by using craco and some "magic" stuff that I don't even understand myself.
I faced the same problem in node 16 :(
@misolo try updating the node version. remove all node packages and reinstall them.
I explained the problem is the node version. What's wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.