I am using an Angular app and need to include a script in my package.json.
The script is written in Typescript and can be found at 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);
This is what my package.json looks like:
"scripts": {
"generate-blog-metadata" : "node src/scripts/generate-blog-metadata.ts"
},
However, when I try to run the script using:
npm run generate-blog-metadata
I encounter an error:
/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)
It seems like my script is unable to locate the node_modules directory.
This issue might be related to a webpack configuration problem. Here is my webpack config file:
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'),
{}
)
]
}