We have recently converted the Node Server in the agent layer of the project from the old JS to TS, summarizing some of the potholes encountered in the process.

  • The node of the esm
  • Babel – node and the typescript

1. Esm in Node

The commonJS module is used in node by default.

const b = require('./b.js')
console.log(b)
Copy the code

With node versions upgraded, esModule has been supported in recent versions, and earlier versions passed

 node --experimental-modules app.js
Copy the code

To support running the ES Module directly in Node. Later versions have natural support for running the ES Module directly.

 node  app.mjs
Copy the code

To run ESM directly in a Node project, you can specify the file’s suffix as MJS, or you can specify no suffix but set type in package.json to module. The rules here are:

  • The type field is generated to define how package.json files and.js files and files without extensions are handled in the root directory of the package.json file. Values of ‘Moduel’ are treated as ES modules; A value of ‘commonJS’ is treated as a CommonJS module

  • If pacakage.json does not define a type field, node defaults to commonJs

  • The node official recommendation package developer explicitly specifies the value of the type field in package.json

Are.mjs files treated as es modules and.cjs files as commonJs modules, regardless of the value of the type field in package.json

So if you want to run app.js directly (without changing the suffix), you must set type=module in package.json.

Babel-node and typescript

To run ts directly in node, we can use ts-node, and after babal7+, babel-node can also run TS directly.

Of course, you can do this without running TS directly, but in my project, during the Startup of Node Server, it is recommended to run TS directly in the development environment. If you compile TS ->js and run Node.xx.js directly in your development environment, this method is also recommended.

Back to this article, my project needs to run TS directly in the production environment, with the option of TS-Node and babel-node7+. In order to use other ecological configurations of Babel, I used to upgrade the Babel of the old project. From 6.x to 7.x to enable babel-node commands to run node index.ts startup files directly.

(1) upgrade babel7 +

Babel6 to Babel7 can be upgraded directly with the official tools. Unlike Babel6, babel7 puts the declaration of related tools under @babel. We can use the babel-upgrade tool to upgrade babel6.x in our project in one click

npx babel-upgrade
Copy the code

In my project I need Babel to compile react & typescript, and the configuration of.babelrc needs to add presets:

 "presets": [
    "@babel/preset-react"["@babel/preset-env",
      {
        "targets": {
          "node": "8.11.3"}}]."@babel/preset-typescript"
  ]

Copy the code

(1) run the Babel – node

Babel-node runs the. Ts file directly without specifying type=module in package.json.


  "scripts": {
    "dev": "nodemon --watch ./src/server --exec babel-node ./src/server/index.ts --extensions \".ts\"",},Copy the code