I have typescript project with absolute paths set with baseUrl property in tsconfig file to src directory. The problem is that I want to have constants.ts file in the root of the src directory, and when I want to import something from that file, compiler tries to import code from deprecated constants NodeJS module, and I see message below in the IDE:
module "constants"
#deprecated — since v6.3.0 - use constants property exposed by the relevant module instead.
How can I hide this module or create an alias, so that src/constants.ts file would be read first?
Related
I'm trying to create a new NPM package with TypeScript & Node. The package just allows the users to use a data structure I built. I'm trying to figure what is the right entry point (the value of main) should be in my package.json file. As I understand, I'm going to compile the TypeScript files (which are located in src/) into dist/ directory and those files will be actually hosted in NPM.
So should I have index.ts or index.js? If it's index.ts I'll need to compile it as well right? If it's index.js, then I need to point it to dist/? I'm a bit confused about what is the "right"/"convention" way to do it. At the end, I just want users to be able to load the classes I export in my src/mylist.ts file (which being compiled to dist/mylist.js). Do I even need it?
I was reading the How to use the 'main' parameter in package.json? topic, but it only refers to JS project. How should it be in TypeScript?
As typescript is generally compiled before desctibution, you should compile it and use dist/index.js
You will need this do define what import {} from 'my-module' means (i.e. import {} from 'my-module' is threated same as import {} from 'my-module/dist/index.js')
If you don't publish your module and are not using it as a dependency of some other your module you don't need this at all, just make a scripts: { start: "tsx src/index.ts" } or whatever
I want to release a NPM package and I am currently looking for a solution to give users the option to use my rollup bundled ES5 code, or use the ES6 version.
I have a subfolder called lib where I have a index.js and another subfolder inside it called es6 which also has a index.js
In my package.json I have specified the main file as ./lib/index.js
How can I now make it possible to import the ES6 file by using import * as package from "package/es6" or is that not possible at all? If not, how would you import it?
to make my first nodejs program with typescript. maybe i need import or references index.d.ts of #types/node. but it work just after exectue
npm install #types/node and append code like this import { setTimeout } from 'timers'
but way it was referenced.
sorry for my poor english and thanks.
TypeScript has an approximation of NodeJS's module resolution algorithm baked into it so that it can make this scenarios easier.
When you do not specify a value for --module, or "module" in tsconfig.json, it defaults to "commonjs" which in turn defaults the --moduleResolution setting to "node".
In addition to recursively traversing the node_modules directories above and within your working directory looking for files and directories matching the imported name, this also enables automatic resolution of the "packageName" >>> packageName/package.json.main resolution behavior of the NodeJS require function. It also enables the "packageName" >>> packageName/index convention where NodeJS requires a directory as a module via its index file.
In your case, this is how, roughly speaking, it resolves index.d.ts automatically:
When you import from a non-relative module specifier, it looks in node_modules directory adjacent to the working directory.
From there it first looks for an #types subdirectory.
If it finds a directory with the same name as the import, it looks for a package.json file in that directory.
If it finds a package.json with a "main" property it resolves the import to that file.
That file is index.d.ts.
Hint: to see this in action pass the --traceResolution flag to tsc, or set
{
"compilerOptions": {
"traceResolution": true,
// ...
}
}
in tsconfig.json.
Look at the output and you will see its file system walking behavior and each attempt to resolve the import.
When I build a node add-on by creating a standard binding.gyp and running node-gyp build, the addon.node library is created in the subdirectory build/{Release|Debug}/. When I write tests or other javascript code to use this module, I have to give explicit path to the library location
For example,
var addon = require('./build/Release/addon')
However, I would like to do this by only specifying the module name and let node's module search for the library
var addon = require('addon')
How can that be achieved?
I believe the only way to do this is setting require() and having the module in the node_modules folder or having the module in a node_modules folder in one of the parent directories.
Node uses the require() method with no path defined as an indicator to look for the module in the node_modules directory. Unless its an native module.
Its detailed in the docs here.
There's a module for that if you're okay with another dependency.
https://www.npmjs.com/package/bindings
That will search all of the possible build output locations automatically, including debug and release directories.
instead of requireing code relatively, ie starting with ./ or .., i'd like to define a module "globally". For example, take the following package structure:
/src
/index.js
/a.js
/b.js
/lib
/index.js
...
When in src/a.js or src/b.js, to require lib, I would have to do require('../lib') each time. This gets annoying when you start nesting more as you would have to manually resolve ../lib or ../../lib or ../../../lib.
I want to be able to do require('lib'). Can I do this? Or should I just use globals?
Using a non relative path to require your source files is not how node's require is intended to work! Don't try to work around this restriction by placing arbitrary code file in node_modules directory or workaround by changing the NODE_PATH environment variable.
If you want to use require without a path you should extract the required code as a node module and depend on this node module. This leads to better structured code, less complex modules, encapsulated functionality, better testability and easier code reuse.
You can include package dependencies from http or git so there is no requirement to publish node modules you use in npm. Take a look at npm dependencies for more detail.
use module.exports in the index.js file . and place it inside the node_modules folder
if relative path annoy you and you want to use lib always in your application, you can use global variable like this.
var lib = require('./lib');
global.lib = lib;
you can set lib to global variable in your entry point. after then you can access just lib.
but it's pollute global scope. so you have to use carefully.
placing your module in node_modules dont require you to include a path or relative path
EDIT:
if you place a file named package.json inside the module directory, Node will try to parse that file and look for and use the main attribute as a relative path for the entry point. For instance, if your
./myModuleDir/package.json
file looks something like the following, Node will try to load the file with the path
./myModuleDir/lib/myModule.js
:
{
"name" : "myModule",
"main" : "./lib/myModule.js"
}
If that folder does not contain a package definition file named package.json, the package entry point will assume the default value of index.js, and Node will look, in this case, for a file under the path ./myModuleDir/index.js.