Skip to content

Instantly share code, notes, and snippets.

@jkrems
Last active February 13, 2020 02:14
Show Gist options
  • Save jkrems/f97d0e040f82c3c4120ca8d3f2fe2fff to your computer and use it in GitHub Desktop.
Save jkrems/f97d0e040f82c3c4120ca8d3f2fe2fff to your computer and use it in GitHub Desktop.
Node globals handling in Typescript
/node_modules
/input.js
// This should fail unless we target CommonJS.
// It could succeed when targeting CommonJS.
const s: string = __dirname;
{
"name": "demo-node-globals",
"version": "0.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/node": {
"version": "13.7.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.1.tgz",
"integrity": "sha512-Zq8gcQGmn4txQEJeiXo/KiLpon8TzAl0kmKH4zdWctPj05nWwp1ClMdAVEloqrQKfaC48PNLdgN/aVaLqUrluA=="
},
"typescript": {
"version": "3.7.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.5.tgz",
"integrity": "sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw=="
}
}
}
{
"name": "demo-node-globals",
"version": "0.0.0",
"private": true,
"dependencies": {
"@types/node": "^13.7.1",
"typescript": "^3.7.5"
},
"scripts": {
"test": "tsc --module CommonJS input.ts && !(tsc --module ESNext input.ts)"
}
}
{
"compilerOptions": {
"noEmit": true,
"module": "ESNext",
"types": ["node"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment