[英]Typescript cannot find module './lib'
I am trying to use winston-aws-cloudwatch library in my server side app in Typescript. 我正在尝试在Typescript的服务器端应用程序中使用winston-aws-cloudwatch库。
I have a SSCCE setup on GitHub in case you want to reproduce the issue. 如果您想重现该问题,我在GitHub上有一个SSCCE设置 。 Here is the detail.
这是细节。
index.ts 索引
import logger from './logger';
logger.info(`🚀🚀🚀 logger is up !!`);
logger.ts 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 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 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"
}
}
When I try to run application using npm run watch
I get: 当我尝试使用
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)
If you follow the execution, it seems Typescript was able to resolve the path: 如果执行该命令,则Typescript似乎能够解析该路径:
[TypeScript] ======== Module name './lib/' was successfully resolved to '/Users/nishant/dev/sscce/node_modules/winston-aws-cloudwatch/lib/index.js'. ========
but I see node_modules/winston-aws-cloudwatch
has a lib
directory whereas dist/node_modules/winston-aws-cloudwatch
does not. 但是我看到
node_modules/winston-aws-cloudwatch
有一个lib
目录,而dist/node_modules/winston-aws-cloudwatch
没有。
At this point I am clueless what's going wrong. 在这一点上,我不知道出了什么问题。
Ah! 啊! So, after much trial and error it turned out the main culprit was:
因此,经过反复试验,发现罪魁祸首是:
"allowJs": true ,
For some reason it was copying some parts of node_modules
to ./dist
one of those files was as you can see in the error: 由于某种原因,它正在将
node_modules
某些部分复制到./dist
其中一个文件是您在错误中看到的:
dist/node_modules/winston-aws-cloudwatch/index.js
Now the problem was two fold 现在问题是两个方面
I did not know whether node_modules
should be copied to dist
. 我不知道是否应该将
node_modules
复制到dist
。 I thought all of it should go. 我认为所有这些都应该去。 Which is WRONG .
这是错误的 。
None of node_modules
should go in dist
, the compiled files shall refer paths
to figure out the location of node_modules
. node_modules
应该放在dist
,编译文件应引用paths
以找出node_modules
的位置。
The second problem was I could not see error go away when, in the fit of agony, I was switching on and off all the tsconfig
options and rerunning the build. 第二个问题是,由于痛苦,我打开和关闭所有
tsconfig
选项并重新运行构建时,看不到错误消失。 The reason was, dist
DOES NOT CLEAR OFF . 原因是,
dist
无法清除 。 I figured this by an obscure luck while repeating the same process umpteenth time! 我在第十一次重复相同过程的时候,运气不佳。
Anyway, good learning. 无论如何,很好的学习。 Hope some of it would help someone someday.
希望有一天能对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.