我使用这个模板https://github.com/sudokar/nx-serverless创建nx monorepo与无服务器框架,我没有修改任何配置,所以你可以看看它作为参考。它工作得很好,但当我尝试从node_modules导入任何东西时,我在使用webpack
构建期间得到一个错误这是一个错误的示例
ERROR in ../../node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js 112:15-30
Module not found: Error: Can't resolve 'nock' in 'C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gyplibutil'
resolve 'nock' in 'C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gyplibutil'
Parsed request is a module
using description file: C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gyppackage.json (relative path: ./lib/util)
resolve as module
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gyplibutilnode_modules doesn't exist or is not a directory
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gyplibnode_modules doesn't exist or is not a directory
looking for modules in C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gypnode_modules
single file module
using description file: C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gyppackage.json (relative path: ./node_modules/nock)
no extension
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gypnode_modulesnock doesn't exist
.mjs
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gypnode_modulesnock.mjs doesn't exist
.json
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gypnode_modulesnock.json doesn't exist
.ts
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gypnode_modulesnock.ts doesn't exist
.js
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gypnode_modulesnock.js doesn't exist
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode-pre-gypnode_modulesnock doesn't exist
C:UserskktamDesktopDevnx-serverless-t73node_modules@mapboxnode_modules doesn't exist or is not a directory
C:UserskktamDesktopDevnx-serverless-t73node_modulesnode_modules doesn't exist or is not a directory
looking for modules in C:UserskktamDesktopDevnx-serverless-t73node_modules
single file module
using description file: C:UserskktamDesktopDevnx-serverless-t73package.json (relative path: ./node_modules/nock)
no extension
C:UserskktamDesktopDevnx-serverless-t73node_modulesnock doesn't exist
.mjs
C:UserskktamDesktopDevnx-serverless-t73node_modulesnock.mjs doesn't exist
.json
C:UserskktamDesktopDevnx-serverless-t73node_modulesnock.json doesn't exist
.ts
C:UserskktamDesktopDevnx-serverless-t73node_modulesnock.ts doesn't exist
.js
C:UserskktamDesktopDevnx-serverless-t73node_modulesnock.js doesn't exist
C:UserskktamDesktopDevnx-serverless-t73node_modulesnock doesn't exist
C:UserskktamDesktopDevnode_modules doesn't exist or is not a directory
C:UserskktamDesktopnode_modules doesn't exist or is not a directory
C:Userskktamnode_modules doesn't exist or is not a directory
C:Usersnode_modules doesn't exist or is not a directory
C:node_modules doesn't exist or is not a directory
@ ../../node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js 15:21-62
@ ../../node_modules/bcrypt/bcrypt.js 3:17-48
@ ./src/handlers/register/register-handler.ts 14:17-34
webpack配置
import * as path from 'path';
import * as slsw from 'serverless-webpack';
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import * as webpack from 'webpack';
export const baseWebpackConfig = (
directory: string
): webpack.Configuration => ({
context: directory,
mode: 'production',
entry: slsw.lib.entries,
devtool: 'inline-cheap-module-source-map',
optimization: {
minimize: true,
minimizer: [
() => {
return () => {
return {
terserOptions: {
format: {
comments: false,
},
},
extractComments: false,
};
};
},
],
},
resolve: {
extensions: ['.mjs', '.json', '.ts', '.js'],
symlinks: false,
cacheWithContext: false,
plugins: [
new TsconfigPathsPlugin({
configFile: 'tsconfig.app.json',
}),
],
},
output: {
libraryTarget: 'commonjs',
path: path.join(directory, '.webpack'),
filename: '[name].js',
},
target: 'node',
externals: [],
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /.(tsx?)$/,
loader: 'ts-loader',
exclude: [
[
path.resolve(directory, 'node_modules'),
path.resolve(directory, '.serverless'),
path.resolve(directory, '.webpack'),
],
],
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
},
],
},
plugins: [],
});
我所做的唯一的事情得到这个错误是处理程序中的import { hash } from 'bcrypt';
,部署或本地服务与无服务器脱机都不工作,因为这个webpack错误
您的问题与nx无关。
这里的问题在于使用bcrypt。
或者,您可以使用bcryptjs代替bcrypt