打字稿找不到模块' ./ lib'

时间:2018-06-19 08:11:50

标签: javascript typescript tsc

我正在尝试在Typescript中的服务器端应用程序中使用winston-aws-cloudwatch库。

我有一个SSCCE setup on GitHub,以防你想重现这个问题。这是详细信息。

index.ts

import logger  from './logger';
logger.info(` logger is up !!`);

logger.ts

import winston from 'winston';
import CloudWatchTransport from 'winston-aws-cloudwatch';

const logger = winston.createLogger();
logger.add(new CloudWatchTransport({logGroupName:'my-api', logStreamName: 'lstream'}));
logger.level = 'silly';

export default logger;

tsconfig.json

{
    "compilerOptions": {
    "target": "es2017" ,
    "module": "commonjs" ,
    "lib": [
        "es2017",
        "esnext.asynciterable"
    ] ,
    "allowJs": true ,
    "sourceMap": true ,
    "outDir": "./dist" ,    
    "noUnusedLocals": true ,
    "noUnusedParameters": true ,
    "noImplicitReturns": true ,
    "noFallthroughCasesInSwitch": true ,
    "moduleResolution": "node" ,
    "baseUrl": "./" ,
    "paths": {
        "*": ["node_modules/*", "src/types/*"]
    } ,
    "allowSyntheticDefaultImports": true ,
    "esModuleInterop": true 
    }
}

的package.json

{
  "name": "winston-aws",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "ts-node index.js",
    "watch-node": "nodemon dist/index.js",
    "watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"yellow.bold,cyan.bold\" \"npm run watch-ts\" \"npm run watch-node\"",
    "watch-test": "npm run test -- --watchAll",
    "build-ts": "tsc",
    "watch-ts": "tsc --traceResolution -w"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-cli": "^6.26.0",
    "winston": "^3.0.0",
    "winston-aws-cloudwatch": "^2.0.0"
  },
  "devDependencies": {
    "@types/node": "^10.3.3",
    "babel-preset-env": "^1.7.0",
    "concurrently": "^3.5.1",
    "nodemon": "^1.17.5",
    "ts-node": "^6.1.1",
    "typescript": "^2.9.2"
  }
}

当我尝试使用npm run watch运行应用程序时,我得到:

[Node] internal/modules/cjs/loader.js:596
[Node]     throw err;
[Node]     ^
[Node]
[Node] Error: Cannot find module './lib/'
[Node]     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
[Node]     at Function.Module._load (internal/modules/cjs/loader.js:520:25)
[Node]     at Module.require (internal/modules/cjs/loader.js:650:17)
[Node]     at require (internal/modules/cjs/helpers.js:20:18)
[Node]     at Object.<anonymous> (/Users/nishant/dev/sscce/dist/node_modules/winston-aws-cloudwatch/index.js:1:80)
[Node]     at Module._compile (internal/modules/cjs/loader.js:702:30)
[Node]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
[Node]     at Module.load (internal/modules/cjs/loader.js:612:32)
[Node]     at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
[Node]     at Function.Module._load (internal/modules/cjs/loader.js:543:3)

如果您按照执行,似乎Typescript能够解析路径:

[TypeScript] ======== Module name './lib/' was successfully resolved to '/Users/nishant/dev/sscce/node_modules/winston-aws-cloudwatch/lib/index.js'. ========

但我看到node_modules/winston-aws-cloudwatch有一个lib目录,而dist/node_modules/winston-aws-cloudwatch没有。

此时我无能为力。

1 个答案:

答案 0 :(得分:0)

啊!因此,经过反复试验,发现罪魁祸首是:

 "allowJs": true ,

由于某种原因,它正在将node_modules的某些部分复制到./dist其中一个文件,正如您在错误中看到的那样:

dist/node_modules/winston-aws-cloudwatch/index.js

现在问题出在两个方面

  1. 我不知道是否应将node_modules复制到dist。我认为所有这些都应该去。这是错误

    node_modules都不应该进入dist,编译后的文件应引用paths来找出node_modules的位置。

  2. 第二个问题是,由于痛苦,我打开和关闭所有tsconfig选项并重新运行构建时,看不到错误消失。原因是, dist无法清除。我很奇怪地发现了这一点,同时又重复了相同的过程!

无论如何,很好的学习。希望有一天能对某人有所帮助。