3 ms·
$ cat helper.mjs export const sleep = (dur) => new Promise(resolve => setTimeout(resolve, dur)) $ cat main.mjs import { sleep } from './helper.mjs'
by pkilgore 3y ago
$ cat helper.mjs
export const sleep = (dur) => new Promise(resolve => setTimeout(resolve, dur))
$ cat main.mjs
import { sleep } from './helper.mjs'
await sleep(1000)
console.log("Go is great; but weird throwing node under a bus here?")
$ node main.mjs
Go is great; but weird throwing node under a bus here?
- breadchris 3y agoif I were a beginner developer, I now have to have the tribal knowledge of the difference between .js and .mjs. I don't see anyone widely using .mjs to write their code either.
- BillyTheKing 3y ago$ cat helper.mjs export const sleep = (dur) => new Promise(resolve => setTimeout(resolve, dur)) $ cat main.mjs import { sleep } from './helper.js' await sleep(1000) console.log("Go is great; but weird throwing node under a bus here?") $ node main.mjs file://main.mjs:1 import { sleep } from './helper.js' ^^^^^ SyntaxError: Named export 'sleep' not found. The requested module './helper.js' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using: import pkg from './helper.js'; const { sleep } = pkg; 'What is CommonJS?'
- BillyTheKing 3y agothis was a joke comment in the morning.. but actually just turned out to be an issue now. Was installing nanoid in a project using commonJS and typescript.. all jest tests suddenly failed. So I looked into jest.config - did I need to change something in terms of transpilation? or some new babbel config? some other secret flag somewhere? no, because turns out that npm install nanoid Nano ID 5 works only with ESM projects, in tests or Node.js scripts. For CommonJS you need Nano ID 3.x (we still support it): This whole module bit in node has been a total disaster. Incredibly frustrating