How to import a JavaScript file located outside the directory of a function with Node.js?
Closed this issue · 0 comments
simmzl commented
Description
Here is the directory structure of my project:
serverless
├── add
│ ├── func.js
│ ├── func.yaml
│ └── package.json
├── app.yaml
└── utils.js
and utils.js
just like:
exports.testFn = function () {
console.log(1)
}
When I import utils.js file inside add/func.js like:
const fdk = require('@fnproject/fdk')
const utils = require('../utils') // Look here
fdk.handle(function (input) {
let name = 'World'
if (input.name) {
name = input.name
}
console.log('\nInside Node Hello World function')
return { message: 'Hello ' + name }
})
this API /add
will return 502 error.
However, when I move utils.js into the /add/
directory, this API works normally.
serverless
├── add
│ ├── func.js
│ ├── func.yaml
│ ├── package.json
│ └── utils.js
├── app.yaml
const fdk = require('@fnproject/fdk')
const utils = require('./utils') // Look here
fdk.handle(function (input) {
let name = 'World'
if (input.name) {
name = input.name
}
console.log('\nInside Node Hello World function')
return { message: 'Hello ' + name }
})
So, is there any other way to import an external JavaScript file besides installing dependencies via npm i
and moving it to the function directory when needed?
Output of fn version
(CLI command):
Client version is latest version: 0.6.26
Server version: ?
Additional environment details (OSX, Linux, flags, etc.):
MacOS Monterey 12.2.1