How do I reliably reference "node_modules" from package.json scripts? - npm

The postinstall stage for my package.json uses tsc. I'd like to make it so such a global dependency is never expected of the user, and the solution is to replace it with ./node_modules/typescript/bin/tsc.
Unfortunately, this only works if node_modules is a subfolder of the one I'm currently in. So what I would like to do is use some form of env variable which probably exists but I can not find to do this:
$ROOT_NODE_PACKAGE/node_modules/typescript/bin/tsc $CURRENTLY_INSTALLING_PACKAGE
How do I do this?

Related

How to specify another directory or name for the .env file in Nuxt

I don't want to follow the convention of putting my environment variable (.env file) at the root directory of my Nuxt project.
How can I achieve another directory or even name for it without using the #nuxtjs/dotenv module? (I know this one is already baked into Nuxt since v2.13 and hence, not needed anymore).
Looking at this answer: https://github.com/nuxt/nuxt.js/issues/8331#issuecomment-727533175
You can specify a different location and name if you'd like, with the following
yarn dev --dotenv variables/.env_file
With the following structure
Of course, this will work with yarn generate and yarn build.
I got no luck with the --dotenv flag, so I'm just using replacing .env with the desired contents upon build or serve, like cp .env.xxx .env && yarn dev.
Not the prettiest, but good enough.
In your case the command would be cp variables/.env_file .env && yarn dev.

jest ignore all node_modules except 1 package

I'm using jest for doing testing and currently I can ignore node_modules. The issue, however, is that theres 1 package in node_modules that I want to get transpiled with babel. Right now my ignored patterns look like this:
testPathIgnorePatterns: ['./node_modules/'],
if my package is called 'my-package', how can I make it so testPathIgnorePatterns doesn't ignore that inside node_modules?
TL;DR - use this config key with this regex:
transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
This is a 2 part answer.
Part 1 - the regex
The testPathIgnorePatterns key takes an array of strings/regex, so I can pass a regex that will match all node_modules except the one I want with something like this:
// one way to do it
testpathIgnorePatterns: ['/node_modules\/(?!my-package).+\.js$']
// another way to do it - this regex seems simpler to me
testpathIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
this basically says, ignore all node_modules except my-package. However, the thing to note here is, testPathIgnorePatterns does not determine whether a module should be transpiled by babel or not. This key is for jest to know where your test files are. Basically, using this key, you're telling jest 'dont look in node_modules for files with the .test.js extension', because you don't want to run the tests of imported node_module packages.
So, in reality, you want to ignore this setting completely because jest will automatically default it to ['/node_modules/'], or if you wanted to add other paths to ignore, you'd need:
testpathIgnorePatterns: ['other/file/path', '/node_modules/']
Part 2 - where to use the regex
Given the context from part 1, the correct place to put the regex to allow jest to transpile specific packages from node_modules is: transformIgnorePatterns. So it would look like this:
transformIgnorePatterns: ['/node_modules\/(?!my-package)(.*)']
this way, jest will not transpile any node_modules packages except 'my-package'. I believe the default value for both of the keys mentioned above is 'node_modules' - meaning, node_modules is automatically ignored in both the testPath and the transform
Jest docs for these configuration settings

How to let npm use sass which comes from local libsass-dev(installed by apt) instead of downloading its own package?

I have a libsass-dev on my system. But npm will download its own one, which is annoying. How to let npm makes use of libsass-dev?
You can try specifying the absolute path to your module, assuming is outside the node_modules folder.
You can try first the require.resolve('/path/to/my/Module') and if it finds it will return the path to the module, then do the require function.
Or You can create your own folder inside node_modules and require the full folder, something like require('./relativeOrAbsPath/To/My/node_modules/myModule'). The default file is index.js or You can mention in the package.json file using the main property to specify another file name.
Also you can update your modules path using this npm module https://www.npmjs.com/package/app-module-path but this looks first on your node_modules folder. However on prior version 1.x is starting for the bottom of the module.paths list.

NPM : Create an NPM package that adds files and folders to the project root directory

I've created a web app template that I use frequently for many different projects.
I would like to create an NPM package for it so that it's easier to install for new projects, separate the template from the project files, separate the template dependencies from the project dependencies, and allow easier updating of the template across all projects.
The issue I have is that I need some files/folders to be installed in the root directory (i.e. where package.json is saved). Most can go in the node_modules folder however I have some files that must be placed in the root directory.
For example, the template uses Next.js with a custom _app.js file. This must be in the root directory in a folder named pages. I also have various config files that must be in the root directory.
Can this be done with NPM, or does everything need to be installed in the node_modules folder? I'm having trouble finding anything on SO or Google that answers this, so if you happen to know a guide online on how to do this or can outline things I should search for it would be much appreciated.
With pure npm, everything has to go to the node_modules folder, so you can't solve your issue this way.
Maybe going with a templating tool such as grunt init or yeoman could be a solution here, although – unfortunately – you'll then lose some of the benefits of being able to install a package via npm.
Another option might be to use GitHub template repositories, which have just been introduced recently.
Last but not least one option might also be to just have the files' contents in the npm package, but create the pages/_app.js manually, but inside of it simply require the file contents from an npm module, and that's it. This at least helps to have the content portable, but of course it still asks you to setup the file and folder structure on your own.
Sorry that I don't have a better answer, but I hope it helps anyway.
PS: One "solution" might also be to use the postinstall step in an npm module's package.json file to create folder structure, copy files to where they should be and so on, but at least to me this feels more like a clumsy workaround than like a real solution.

Intellij/Webstorm Yarn - Cannot find Package unless on root

I am trying to add packages via yarn inside of intellij. I can get it to install the package fine, and I can even get it to move the packages to my own custom folder via --modules-folder "ExternalLibs".
The issue I am having is, unless I allow yarn to install on the root and under the node_modules folder, it won't recognize that there is a package.
Is there a way to point the package.json to look in the custom path?
You can try setting NODE_PATH environment variable pointing to your folder location in Node.js run configuration template: Run | Edit Configurations..., expand Templates node, select Node.js configuration, specify NODE_PATH in Environment variables field
Please see https://youtrack.jetbrains.com/issue/WEB-19476#focus=streamItem-27-2819977.0-0
Note that, though the modules in require() calls will actually be resolved, you will still see warnings about non-installed packages due to WEB-25792; you have to disable JavaScript | General | Missing module dependency inspection to get rid of the warnings