简体   繁体   中英

Unable to find module 'path-to-regexp'

I am trying to use the library path-to-regexp in my typescript project but for some reason, it isn't allowing me to import it. I used npm install path-to-regexp --save with and without the --save to try to get it working but that didn't change anything.

My package.json says that I do have it installed ( "path-to-regexp": "^6.1.0" ) so it confuses me why vscode would say that it can't find it.

Looking in the node_modules folder I do have path-to-regex installed there. I even tried deleting node_modules and package-lock.json and then running npm i but that didn't change anything.

I am trying to import it with es2015 modules as import * as pathToRegexp from 'path-to-regexp'

I found the problem. The structure of the module causes problems with typescript's import system have two different dist folders.

│   History.md
│   LICENSE
│   package.json
│   Readme.md
│
├───dist
│       index.d.ts
│       index.js
│       index.js.map
│       index.spec.d.ts
│       index.spec.js
│       index.spec.js.map
│
└───dist.es2015
        index.js
        index.js.map
        index.spec.js
        index.spec.js.map

All I had to do was choose one of them with the typescript config:

  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "path-to-regexp": ["node_modules/path-to-regexp/dist/index"]
    }
  }

EDIT 2.0!!!!! The easy fix is just to set the compiler option "moduleResolution": "node"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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