I have a Node program that primarily uses CommonJS, causing my JS files to begin with several require statements. To transition into TypeScript gradually, I decided to rename these JS files to TS. However, I encountered the following errors:
https://i.sstatic.net/wwXOn.png
Here is an example of the code:
const RSVP = require('rsvp');
const moment = require('moment');
const firebase = require('universal-firebase');
const email = require('universal-sendgrid');
const sms = require('universal-twilio');
const merge = require('merge');
const typeOf = require('type-of');
const promising = require('promising-help');
const _ = require('lodash');
const up = require('./upload');
const audience = require('./audiences');
const message = require('./messages');
The locally referenced modules like upload
, audiences
, and messages
probably define most (if not all) of the same modules such as lodash
. It appears that the namespace scope might not be respected between modules, but the reason for this is unclear.
I am also uncertain whether using ES6 import
syntax would successfully transpile into ES5 as a "require" module format in CommonJS (since it involves Node 0.10.x).
As additional context, here is my tsconfig.json
:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"removeComments": true,
"sourceMap": true,
"outDir": "./dist",
"watch": true
},
"compileOnSave": true
}
Note: I have come across instances where people received the error "cannot redeclare block-scoped variable", but none of those discussions seemed completely relevant to my situation. Since I am relatively new to TypeScript, there's a possibility that I'm making a rookie mistake.
Additionally, I observed some examples of an unusual combination of CommonJS and ECMAScript module formats:
import up = require('./upload');
This differs from my usual approach:
const up = require('./upload');
However, when using the "import" keyword, it raises an issue stating that upload.ts
is not recognized as a module: