gpt4 book ai didi

javascript - Webpacked lambda 抛出 ERR_INVALID_ARG_TYPE(name, 'string' , value);当跑的时候

转载 作者:行者123 更新时间:2023-12-02 22:33:13 25 4
gpt4 key购买 nike

尝试创建使用 webpack 捆绑的 JavaScript lambda。我当前的index.js 的结构如下:

const AWS = require('aws-sdk');
const fs = require('fs');

exports.handler = async (event) => {
...
}

我的 webpack.config.js 看起来像:

const CopyPlugin = require('copy-webpack-plugin');
const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, './build'),
filename: 'index.js',
libraryTarget: "commonjs"
},
mode: 'development',
target: "node",
plugins: [
new CopyPlugin([
{ from: './deployment.xsd', to: './deployment.xsd'}
])
],
externals: [ nodeExternals() ]
};

Webpack 运行良好并返回一个令人满意的结果,如下所示:

Hash: d5110295ceffea09db88
Version: webpack 4.41.2
Time: 72ms
Built at: 11/12/2019 4:12:47 PM
Asset Size Chunks Chunk Names
deployment.xsd 24.4 KiB [emitted]
index.js 7.08 KiB main [emitted] main
Entrypoint main = index.js
[./index.js] 2.16 KiB {main} [built]
[aws-sdk] external "aws-sdk" 42 bytes {main} [built]
[fs] external "fs" 42 bytes {main} [built]
[libxmljs] external "libxmljs" 42 bytes {main} [built]

然后我使用 Maven 将其打包成 zip 文件。

但是,如果我尝试使用 node-lambda run -H 'build/index.js' -j test/test.json 在本地运行它,则会收到以下错误:

/.../node_modules/continuation-local-storage/context.js:55
throw exception;
^

TypeError: handler is not a function
at /.../node_modules/node-lambda/lib/main.js:124:22
at Namespace.run (/.../node_modules/continuation-local-storage/context.js:48:5)
at Lambda._runHandler (/.../node_modules/node-lambda/lib/main.js:122:15)
at Lambda.run (/.../node_modules/node-lambda/lib/main.js:83:10)
at Command.<anonymous> (/.../node_modules/node-lambda/bin/node-lambda:143:27)
at Command.listener (/.../node_modules/commander/index.js:315:8)
at Command.emit (events.js:203:13)
at Command.EventEmitter.emit (domain.js:471:20)
at Command.parseArgs (/.../node_modules/commander/index.js:651:12)
at Command.parse (/.../node_modules/commander/index.js:474:21) {
'error@context': [Object: null prototype] {
segment: Segment {
trace_id: '1-5dcada48-a2ce6baf341247ac09d71c53',
id: 'e75d048edf0d1b8d',
start_time: 1573575240.157,
name: 'annotations',
in_progress: true,
counter: 0,
service: [Object],
aws: [Object]
}
}
}

设法通过使用node-lambda run -H 'build/index.handler解决此问题,似乎该行上的点后面的位是处理程序名称,而不是文件扩展名。

在 AWS 上,我收到以下错误,该错误是对我使用的尚未打包的库的提示:

{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'libxmljs'",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'libxmljs'",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:45:30)",
" at Module._compile (internal/modules/cjs/loader.js:778:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
" at Module.load (internal/modules/cjs/loader.js:653:32)",
" at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
" at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
" at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)",
" at startup (internal/bootstrap/node.js:283:19)"
]
}

这里有什么错误?来自 build/index.js 的片段

(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./index.js");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./index.js":
/*!******************!*\
!*** ./index.js ***!
\******************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

eval("/* WEBPACK VAR INJECTION */(function(__dirname) {const AWS = __webpack_require__(/*! aws-sdk */ \"aws-sdk\");\nconst libxmljs = __webpack_require__(/*! libxmljs */ \"libxmljs\");\nconst fs = __webpack_require__(/*! fs */ \"fs\");\n\nexports.handler = async (event) => {

我们尝试过的另一种方法是从 webpack.config.js 中删除这一行,这会更改错误,并且编译后的 index.js 会更大,因为它实际上包含了依赖项,但随后我们在本地和 AWS 上都看到此错误:

internal/validators.js:107
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
at validateString (internal/validators.js:107:11)
at dirname (path.js:1121:5)
at Function.getRoot (webpack:///./node_modules/bindings/bindings.js?:154:13)
at bindings (webpack:///./node_modules/bindings/bindings.js?:60:32)
at eval (webpack:///./node_modules/libxmljs/lib/bindings.js?:1:92)
at Object../node_modules/libxmljs/lib/bindings.js (/.../build/index.js:8971:1)
at __webpack_require__ (/.../build/index.js:20:30)
at eval (webpack:///./node_modules/libxmljs/index.js?:4:16)
at Object../node_modules/libxmljs/index.js (/.../build/index.js:8960:1)
at __webpack_require__ (/.../build/index.js:20:30)

最佳答案

libxmljs 库是用 C 编写的。而 webpack 无法将其打包到 js 文件中以在浏览器中执行。

您看到此错误是因为 libxmljs 未打包。如果你想使用 webpack,你必须找到这个库的纯 JS 版本并使用它。

但是,我不喜欢使用 webpack 构建 lambda 函数。 Lamda 支持的每种语言都必须管理外部依赖项。而AWS对此提供了更好的支持。因此,压缩所有 Nodejs 内容并将它们上传到 Lambda。在大多数情况下就足够了。

此外,您还可以利用 Lambda Layers,https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html 。通过层,您的node_modules可以位于单独的层中。

此外,您可能需要在 Amazon Linux 计算机/另一台兼容计算机 ( https://github.com/kelektiv/node.bcrypt.js/issues/505 ) 中安装 npm 模块。

关于javascript - Webpacked lambda 抛出 ERR_INVALID_ARG_TYPE(name, 'string' , value);当跑的时候,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com