javascript - 打字稿找不到模块'./lib'
问题描述
我正在尝试在 Typescript 的服务器端应用程序中使用winston-aws-cloudwatch库。
如果您想重现该问题,我在 GitHub 上有一个SSCCE 设置。这是细节。
索引.ts
import logger from './logger';
logger.info(` logger is up !!`);
记录器.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
}
}
包.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
没有。
在这一点上,我不知道出了什么问题。
解决方案
啊! 因此,经过多次试验和错误,结果发现主要的罪魁祸首是:
"allowJs": true ,
出于某种原因,它正在将某些部分复制node_modules
到./dist
其中一个文件中,正如您在错误中看到的那样:
dist/node_modules/winston-aws-cloudwatch/index.js
现在问题有两个
我不知道是否
node_modules
应该复制到dist
. 我认为这一切都应该过去。这是错误的。一个都不要
node_modules
进去dist
,编译后的文件要参考paths
找出位置node_modules
。第二个问题是当我痛苦地打开和关闭所有
tsconfig
选项并重新运行构建时,我看不到错误消失。原因是,dist
不清除。在无数次重复相同的过程时,我凭着一种晦涩的运气想到了这一点!
总之,好好学习。希望有一天它会帮助某人。
推荐阅读
- wordpress - 无法编辑 WordPress 插件或主题文件
- ruby-on-rails - 无法从 Rails ActionText 输出值
- javascript - TypeError:_firebase2.default.ref 不是函数
- javascript - 如何到达 svg 文档的不同节点(即每个组中的标题、路径)
- datetime - 无法理解融合云指标 api ISO 8601 时间间隔
- java - 我可以在不使用 Java 中的日历库的情况下将秒数添加到当前日期吗?
- sql - OrientDB 查询以从边缘属性中获取总和
- javascript - 陷入回文迭代
- javascript - 使用动态键递归创建嵌套对象
- python-3.x - 我如何制作一个可以接受来自我的 python 程序的用户输入的 sublime text 3 构建系统