0

I do not understand the purpose of using "/" when requiring a package in Nodejs. For example for loading the Microsoft / Contributors Node V8 Driver for Node.js for SQL Server it needs to be required like this as per the documentation:

const sql = require('mssql/msnodesqlv8');

But these are two different node modules. What is the above require doing?

2 Answers 2

1

In your case, require is just importing a specific file. mssql/msnodesqlv8 means require msnodesqlv8 from mssql package. File is here https://github.com/tediousjs/node-mssql/blob/master/msnodesqlv8.js

On the other hand, everything before the / can be the package scope. When someone publishes a package, they need to give it a name, the package can be either user-scoped or organization-scoped. This helps to create a package with the same name as a package created by another user or organization without conflict.

More info:

I am not sure if scopes require a @ character though.

Sign up to request clarification or add additional context in comments.

Comments

1

You can dive it from the document step by step:

This driver is not part of the default package and must be installed separately by npm install msnodesqlv8@^2

  1. Above says msnodesqlv8 is a driver, but to use this driver:

To use this driver, use this require syntax: const sql = require('mssql/msnodesqlv8').

  1. Above will reference the file: mssql/msnodesqlv8:
module.exports = require('./lib/msnodesqlv8')
  1. So look at the file mssql/lib/msnodesqlv8/index.js
const base = require('../base')
const ConnectionPool = require('./connection-pool')
const Transaction = require('./transaction')
const Request = require('./request')
  1. Then the file ./connection-pool.js finally required msnodesqlv8 package:
const msnodesql = require('msnodesqlv8')
... ...
  1. Above require('msnodesqlv8') will lookup and use the package you just installed.

It's like a chain, you grab the start(mssql/msnodesqlv8), but you got the end(msnodesqlv8) finally, the have similar names, but have different means.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.