node_modules无法解析,本地绝对模块可以正常工作

时间:2019-09-20 21:27:53

标签: typescript webpack

所以这一直很痛苦,但是我的baseUrl可以正常工作,并且我的文件可以正确加载,但是,例如,尝试import Chalk from 'chalk时出现此错误。

i 「atl」: Using typescript@3.6.3 from typescript
i 「atl」: Using tsconfig.json from C:/Users/adria/Desktop/backup/Business/repositories/xTouch/tsconfig.json
i 「atl」: Checking started in a separate process...
i 「atl」: Time: 719ms
Hash: 13062a08d1faecf0a32b
Version: webpack 4.40.2
Time: 2119ms
Built at: 09/19/2019 12:50:05 AM
 1 asset
Entrypoint main = xtouch.js
[0] ./src/CommandBase.ts 195 bytes {0} [built]
[1] ./src/xtouch.ts 670 bytes {0} [built]
[2] ./src/XTouchCommandManager.ts 648 bytes {0} [built]
[3] ./src/CommandHandler.ts 1.67 KiB {0} [built]
[4] ./src/CommandEcho.ts 512 bytes {0} [built]
[5] ./src/CommandHelp.ts 552 bytes {0} [built]

ERROR in ./src/xtouch.ts
Module not found: Error: Can't resolve 'chalk' in 'C:\Users\adria\Desktop\backup\Business\repositories\xTouch\src'
 @ ./src/xtouch.ts 4:16-32
error Command failed with exit code 2.

我已经尝试了各种方法,但是它没有用。

// config/webpack.config.ts
const config: Configuration = {
  entry: path.resolve(appSrc, 'xtouch.ts'),
  mode: 'production',
  target: 'node',
  output: {
    path: outDir,
    filename: 'xtouch.js',
  },
  resolve: {
    extensions: ['.ts', '.ts', '.json'],
    modules: ['node_modules', appNodeModules].concat(appSrc || []),
  },
  module: {
    rules: [
      {
        test: /.ts/,
        loader: 'awesome-typescript-loader',
        exclude: /node_modules/,
      },
    ],
  },
};

export default config;

文件结构(用于上下文)

  • config /
    • paths.ts
    • webpack.config.ts
  • node_modules /
  • src /
    • xtouch.ts
    • XTouchCommandManager.ts
  • tsconfig.json
  • package.json

您甚至可以找到公共存储库here。这真的让我感到压力。

1 个答案:

答案 0 :(得分:0)

原来,我有[ '.ts', '.ts' ]而不是[ '.js','.ts'],并且锁定了所有节点模块