pang
02/12/2021, 5:30 PMhaneefmubarak
02/12/2021, 9:06 PMGreg Brimble | Cloudflare Pages
02/12/2021, 11:24 PMjson
{
"result": null,
"success": false,
"errors": [
{
"code": 10021,
"message": "Uncaught ReferenceError: MyDurableObjectClass is not defined\n at line 0\n"
}
],
"messages": []
}
or Uncaught ReferenceError: default is not defined\n at line 0\n
when not using DO.
"problem with the script" here means that there is a reference to an undefined global. When a script gets uploaded to Cloudflare, you're obviously executing it, to try and import the modules (MyDurableObjectClass or default) which we export in the script. This error message tells us that something went wrong, but not where the problem in our script actually is. If I include a console.log(process.env.NODE_ENV)
or console.log(setImmediate)
somewhere in my script, all I get back is the ReferenceError. If I was entirely writing the script myself, that's not really a problem, because I would know that those globals don't exist. However, a lot of external library packages do things such as:
if (process.env.NODE_ENV === "production") {} else {}
or
const someFunc = setImmediate || setTimeout
And the upload process just completely fails if you try to include them.
To work around it, I'm using a Rollup plugin, @rollup/plugin-replace
to replace these global references with "production"
or undefined
or similar, but to first identify which variables I need to swap out, I have to manually search through the built script (100,000s of LOC) to find them, because the error message on upload doesn't tell me. Is there any way this message could be improved to instead throw a ReferenceError for the missing global?Greg Brimble | Cloudflare Pages
02/12/2021, 11:24 PMtypescript
console.log(someMadeUpGlobal)
export default {
fetch: async (request) => new Response("Hello")
}
gives "ReferenceError: default is not defined" rather than "ReferenceError: someMadeUpGlobal" is not defined.Greg Brimble | Cloudflare Pages
02/12/2021, 11:24 PMElectroid
02/12/2021, 11:46 PMGreg Brimble | Cloudflare Pages
02/12/2021, 11:49 PMGreg Brimble | Cloudflare Pages
02/12/2021, 11:49 PMElectroid
02/12/2021, 11:53 PMpreserveModules
to true
, and the file structure of your project will be preserved.Electroid
02/12/2021, 11:54 PMGreg Brimble | Cloudflare Pages
02/12/2021, 11:54 PMGreg Brimble | Cloudflare Pages
02/12/2021, 11:56 PMElectroid
02/12/2021, 11:59 PMElectroid
02/13/2021, 12:00 AMis-odd
requires is-buffer
! Like.. huh?Greg Brimble | Cloudflare Pages
02/13/2021, 12:15 AMjavascript
import { terser } from 'rollup-plugin-terser'
import replace from '@rollup/plugin-replace'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import typescript from '@rollup/plugin-typescript'
import json from '@rollup/plugin-json'
import nodePolyfills from 'rollup-plugin-node-polyfills'
import nodeGlobals from 'rollup-plugin-node-globals'
import dynamicImportVars from '@rollup/plugin-dynamic-import-vars'
export default {
input: './src/index.ts',
output: {
exports: 'named',
format: 'es',
file: './dist/index.mjs',
sourcemap: false,
},
treeshake: {},
plugins: [
// replace({
// 'process.env.NODE_ENV': JSON.stringify('production'),
// 'process.nextTick': undefined,
// setImmediate: undefined,
// process: '({})',
// }),
typescript(),
nodeResolve({ browser: true }),
commonjs({
transformMixedEsModules: true,
}),
json(),
nodePolyfills(),
nodeGlobals(),
dynamicImportVars({ warnOnError: true }),
// terser({
// ecma: 'ESNext',
// output: {
// comments: false,
// },
// }),
],
}
Gives a brand new error that I've never seen before:
json
{
"result": null,
"success": false,
"errors": [
{
"code": 10021,
"message": "Uncaught Error: No such module.\n at line 0\n"
}
],
"messages": []
}
Uncommenting the replace
plugin then deploys successfully as before. Haven't got time to dig into finding exactly what's missing from that config you gave me right now, but I will do later 🙂 Thanks for your help @UserElectroid
02/13/2021, 12:16 AMGreg Brimble | Cloudflare Pages
02/13/2021, 12:16 AMGreg Brimble | Cloudflare Pages
02/13/2021, 12:19 AMGreg Brimble | Cloudflare Pages
02/13/2021, 12:19 AMElectroid
02/13/2021, 12:24 AMGreg Brimble | Cloudflare Pages
02/13/2021, 12:26 AMElectroid
02/13/2021, 12:26 AMmjs
Greg Brimble | Cloudflare Pages
02/13/2021, 12:27 AM./dist/index.mjs
.jed
02/15/2021, 3:03 AMkenton
02/15/2021, 3:07 AMjed
02/15/2021, 3:17 AMjed
02/15/2021, 3:17 AMjed
02/15/2021, 3:18 AMkenton
02/15/2021, 3:19 AMkenton
02/15/2021, 3:20 AM