Home > Net >  What does the tilde (~) mean in Node.js's "require"?
What does the tilde (~) mean in Node.js's "require"?

Time:09-13

I'm reading through the source code for the game "Screeps" (trying to reverse-engineer how it executes user-provided scripts in a safe way, to use in games of my own), and I've come across a line I don't understand.

Line 10 in this file reads:

    driver = require('~runtime-driver');

What does the tilde mean here? This doesn't seem to refer to a local file or a separate package, so where is it locating the driver?

The engine runs on node, and here are the dependencies from package.json:

  "dependencies": {
    "@screeps/pathfinding": "^0.4.16",
    "bulk-require": "^0.2.1",
    "cross-env": "^5.2.0",
    "lodash": "3.10.1",
    "q": "^1.0.1"
  },
  "devDependencies": {
    "babel-plugin-transform-es2015-destructuring": "^6.23.0",
    "babel-plugin-transform-strict-mode": "^6.24.1",
    "babel-preset-es2015": "^6.24.1",
    "cross-env": "^5.2.0",
    "gulp": "^3.9.1",
    "gulp-babel": "^6.1.2",
    "gulp-plumber": "^1.1.0",
    "gulp-sourcemaps": "^2.6.0",
    "gulp-traceur": "^0.17.2",
    "gulp-watch": "^4.3.11",
    "jasmine": "^3.3.0"
  },

(Reading this, could it be a babel thing?)

Thanks!

CodePudding user response:

It's a Webpack thing.

For this project, what ~runtime-driver resolves to is defined here:

resolve: {
  alias: {
    '~runtime-driver': require.resolve('./lib/runtime/runtime-driver')
  }
}
  • Related