In order to require
older modules, you write this:
import { createRequire } from 'module';
const require = createRequire(import.meta.url)
This works if I run the file with node my-file.js
. However, I’ve been using the package netlify-cli to run things locally, and when I do that, it logs an error saying that import.meta.url
is undefined
:
The argument ‘filename’ must be a file URL object, file URL string, or absolute path string. Received undefined
If you want to see in more detail, this is all open source:
// const decoded = JSON.parse(Buffer.from(data, "base64").toString("utf-8"))
// console.log('*decoded*', decoded)
// const require = createRequire(decoded)
process.import = {
meta: {
url: '123'
}
}
console.log('*import.meta.url*', import.meta.url)
const require = createRequire(import.meta.url);
const faunadb = require('faunadb')
const xtend = require('xtend')
const sha256 = require('simple-sha256')
// const getMentions = require('./get-mentions')
const upload = require('./upload')
const parallel = require('run-parallel')
import ssc from '@nichoth/ssc'
// const ssc = require('@nichoth/ssc')
What can I pass to createRequire
to make this work?
Is it possible to use import.meta.url
in a lambda function?
Thank you
Hey @nichoth ,
I agree, what you’re doing seems to be valid. Would you be able to open an issue on the CLI repository so that the developers can directly see it? It could be a bug, as it’s definitely not a highly-used way with Functions.
@nichoth and @hrishikesh could this be because the Node runtime for the lambda is running by default on version ‘12’ rather than on 14
?
From what I remember import.meta
is polyfilled with an empty object on v12. It’s been awhile.
Set AWS_LAMBDA_JS_RUNTIME
= “nodejs14.x” in the Netlify UI admin and see if that changes the outcome you are looking for?
I don’t believe that’s the issue. We switched to Node 14 as the default a while back and all sites that don’t have Node 12 explicitly mentioned are now using Node 14.
And a reference to a new github issue —
opened 05:43AM - 09 May 22 UTC
type: bug
### Describe the bug
In order to require older modules, I've been writing this:…
```js
import { createRequire } from 'module';
const require = createRequire(import.meta.url)
```
This works if I run the file with node my-file.js. However, I’ve been using the package [netlify-cli](https://www.npmjs.com/package/netlify-cli) to run things locally, and when I do that, it logs an error saying that `import.meta.url` is undefined:
> The argument ‘filename’ must be a file URL object, file URL string, or absolute path string. Received undefined
If you want to see in more detail, this is all open source:
https://github.com/nichoth/ssc-server/blob/1965d193da893bb98e98bd9962717d65c32a4f83/netlify/functions/profile/profile.js#L14
-------------------------------------------
This is relevant to [this post](https://answers.netlify.com/t/error-because-import-meta-url-is-undefined/56108)
### Steps to reproduce
Either
Use a lambda function that includes `import.meta.url`, and run it with `netlify-cli`
or
clone my example at this commit within git :
https://github.com/nichoth/ssc-server/blob/1965d193da893bb98e98bd9962717d65c32a4f83/netlify/functions/profile/profile.js#L14
Then run `npm start`, which will run `netlify dev`
### Configuration
```toml
[build]
command = "npm run build"
functions = "netlify/functions"
publish = "public"
## more info on configuring this file: https://www.netlify.com/docs/netlify-toml-reference/
[dev]
autoLaunch = false
publish = "public" # Folder with the static content for _redirect file
[template.environment]
FAUNADB_SERVER_SECRET = "faunaDB secret"
CLOUDINARY_API_SECRET = "cloudinary secret"
CLOUDINARY_CLOUD_NAME="cloudinary cloud name"
CLOUDINARY_API_KEY="cloudinary API key"
PUBLIC_KEY = "public key"
SECRET_KEY = "secret key"
[functions]
node_bundler = "esbuild"
[functions.profile]
external_node_modules = ["@nichoth/ssc"]
```
### Environment
```
System:
OS: macOS 12.3.1
CPU: (8) arm64 Apple M1
Memory: 70.31 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
Yarn: 1.22.18 - ~/.nvm/versions/node/v16.13.1/bin/yarn
npm: 8.3.0 - ~/.nvm/versions/node/v16.13.1/bin/npm
npmPackages:
netlify-cli: ^10.3.0 => 10.3.0
npmGlobalPackages:
netlify-cli: 9.16.6
```