我的Angular应用中的ts脚本在node_modules中找不到模块

时间:2019-08-29 16:09:49

标签: angular typescript webpack

我有一个Angular应用。 我想在package.json中添加一个脚本。

该脚本使用Typescript编写,其路径为src / scripts / generate-blog-metadata.ts。

const { promisify } = require('util');
const { resolve, join } = require('path');
const {fs} = require('fs');
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);

我的package.json:

  "scripts": {
    "generate-blog-metadata" : "node src/scripts/generate-blog-metadata.ts"
  },

当我运行脚本时:

npm run generate-blog-metadata

我收到错误消息:

/Users/pom/workspace/signalement-app/src/scripts/generate-blog-metadata.ts:6
const readdir = promisify(fs.readdir);
                             ^

TypeError: Cannot read property 'readdir' of undefined
    at Object.<anonymous> (/Users/john/workspace/signalement-app/src/scripts/generate-blog-metadata.js:6:30)

我了解的是我运行脚本的方式使他无法找到node_modules。

可能是webpack配置问题,所以这是配置文件:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: {
    server: './server.ts',
  },
  target: 'node',
  resolve: {extensions: ['.ts', '.js']},
  externals: [/(node_modules|main\..*\.js)/,],
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, 'dist'),
    filename: '[name].js'
  },
  module: {
    rules: [
      {test: /\.ts$/, loader: 'ts-loader'}
    ]
  },
  optimization: {
    minimize: false
  },
  plugins: [
    new webpack.ContextReplacementPlugin(
      // fixes WARNING Critical dependency: the request of a dependency is an expression
      /(.+)?angular(\\|\/)core(.+)?/,
      path.join(__dirname, 'src'), // location of your src
      {} // a map of your routes
    ),
    new webpack.ContextReplacementPlugin(
      // fixes WARNING Critical dependency: the request of a dependency is an expression
      /(.+)?express(\\|\/)(.+)?/,
      path.join(__dirname, 'src'),
      {}
    )
  ]
}

1 个答案:

答案 0 :(得分:0)

我认为您应该将扩展名generate-blog-metadata.js更改为generate-blog-metadata.ts