tsc is trying to resolve relative path modules in the wrong folder - node.js

I'm using npm link to reference a typescript library I'm developing in my test project
Which means that my node_modules looks like this :
node_modules/
| myLib/
| | dist/
| | | subModule/
| | | | index.js
| | | | index.d.ts
| | | index.js
| | | index.d.ts
| | node_modules/
| | src/
| | tsconfig.json
| | package.json
Which implies that when I'm trying to reference my library using import X from "myLib" I have to tell the compiler that the sources are in the /dist forlder, not that the root of myLib.
I solved this by adding a "main": "./dist/index.js" in the package.json of myLib
The problem is when I try to import a path relative to myLib
Like import Y from "myLib/subModule"
This time it doesn't work.
Because instead of looking at node_modules/myLib/dist/subModule tsc is looking at node_modules/myLib/subModules/dist/ which doesn't exist.
How can I make the compiler to look at the right path for subModules ?

You can resolve this by using the "paths" key in the "compilerOptions" in your tsconfig.json. Something like this:
{
"compilerOptions": {
"paths": {
"myLib/*": "node_modules/myLib/dist/*"
}
}
}
Sadly, this is something of a standing issue with how the TypeScript compiler resolves definition files when you have a "types" key in your package.json.

Related

An alias within a node module

I have a private made npm package that has a symbol # I'd like to use as an alias to use in my current project. Is this possible?
Example:
project structure
|-node_modules
|
|-#package_name
|
|-packagefile.js
|-src
|
|-srcfile.js
|
|-webpack.config.js
Now in:
packagefile.js
const v = require('#/srcfile.js')
Can webpack create an alias with the symbol # to lead to the src directory? I have functionality in this package I want to interact with my current project.
In my webpack.config I have:
module.exports = {
...
resolve: {
alias: {
'#': path.resolve(__dirname,'./src'),
}
}
...
}
Which doesn't seem to work.
I think your webpack config is effective for the root folder, not for node_module.
So I guess, The Package should have it own webpack config
like this
|-node_modules
|
|-#package_name
|
|-packagefile.js
|-webpack.config.js
|-src
|
|-srcfile.js
|
|-webpack.config.js

Getting NPM module not found for a local dependency outside my folder

I have a project that look like this:
|-- client
| |-- index.js
| |-- ...
| |-- package.json
| └-- webpack.config.json
|-- lib
| └-- myLocalLibrary
| |-- index.js
| └-- package.json
└-- server
└-- ...
Code from lib is used in both client and server by importing libraries with npm
So client/package.json and server/package.json contain:
{
dependencies: {
...
myLocalLibrary: 'file: ../lib/myLocalLibrary',
...
}
}
The problem is: when I run webpack in the client, it fails to resolve the imports used in myLocalLibrary, because:
a) NPM installs subdependencies for the local library in client/node_modules, not in lib/myLocalLibrary/node_modules
b) Webpack resolved code for myLocalLibrary using its source folder, which is out of the reach of client/node_modules
What should I do in a case like this?
If you are using webpack as a build tool, I think you have to use resolve.alias webpack config. We don't need node_modules in production env if we have use webpack to concat all files to one entry file.
In your webpack config:
module.exports = {
//...
resolve: {
alias: {
myLocalLibrary: path.resolve(__dirname, 'lib/myLocalLibrary'), // where is the webpack.config.json?
}
}
};
https://webpack.js.org/configuration/resolve/

What don't I get about babel with istanbul?

I'm trying to add code coverage to an existing library that transpiles from ES2015. I seem to be doing everything right. My makefile reads
test: .FORCE
export NODE_ENV=test
babel *.js --out-dir lib
nyc mocha
And my babelrc reads
{
"presets": ["es2015"],
"sourceMaps": true,
"env": {
"test": { "plugins": ["istanbul"] }
}
}
while my package.json includes
"nyc": {
"include": [
"**/*.js"
],
"require": [
"babel-register"
],
"sourceMap": false,
"instrument": false
}
And yet, after my tests run, the output simply reads:
----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|----------------|
I've tried what feels like every combination of babel-node, babel-istanbul, and nyc, with various include directives, and I've never gotten it to so much as report a file with 0% coverage, even when using --include-all-sources. What gives? What am I missing?
You need to instrument the code that is getting transpiled. Use this babel plugin: https://github.com/istanbuljs/babel-plugin-istanbul
The package you're using babel-istanbul is a drop-in package meant to be used in replacement of babel.

Trying to run puppet yield "Error: Could not find class"

Firstly, to be honest I'm pretty new to Puppet world. I'm trying to build a puppet script for my server.
Here's how my puppet structures looks like -
.
|-- environments
| `-- example_env
| |-- manifests
| |-- modules
| `-- README.environment
|-- manifests
| |-- node.pp
| `-- site.pp
|-- modules
| |-- nginx
| | `-- manifests
| | `-- nginx.pp
| |-- sudoers
| | |-- files
| | | `-- sudoers
| | `-- manifests
| | `-- sudoers.pp
| `-- users
| `-- manifests
| `-- users.pp
|-- puppet.conf
`-- templates
here how my node.pp and site.pp look like this.
# /etc/puppet/manifests/node.pp
node werain {
include sudoers
}
and
# etc/puppet/manifests/site.pp
import 'node.pp'
and finally my sudoers.pp file look like this.
# /etc/puppet/modules/sudoers/manifests/sudoers.pp
class sudoers {
file { '/etc/sudoers':
mode: '0400',
source: 'puppet:///modules/sudoers/sudoers',
owner: 'root',
group: 'root'
}
}
Any clue what I'm doing wrong.
I'm running the puppet command like this.
puppet apply /etc/puppet/manifests/site.pp --modulepath=/etc/puppet/modules/
my puppet version is 3.8.4
You have a module named sudoers, and in it a class with the same name. Puppet will look for the definition of that class in modules/sudoers/manifests/init.pp, but you have put it in modules/sudoers/manifests/sudoers.pp instead. That latter is where Puppet would look for a class named sudoers::sudoers.

Res.send in wrong directory

I'm exploring Node website with some templating engine, Jade.
But, some of my page error at allocating resource.
My folder hierarchy
base-project
| public
| | css
| | | style.css
| view
| | dynamic.jade
| | landing.jade
While router.get('/landing') linking resource to the right path: base-url/css/style.js, router.get('/user/dynamic') linking resource to wrong path: base-url/user/css/style.js.
I've used app.use(express.static(path.join(__dirname, 'public'))); in my root app.js.
Is there something wrong?

Resources