Cannot transpile node.js app from es6 to es5 - javascript

I am using node.js to develop a service, I have been using es6, I am not quite sure if there is a need to use babel in this case to transpile the javascript es6 to es5 since the code is never executed from the browser directly.
Also one thing that was wondering me is when should I use babel to transpile nodejs es6 to es5? I tried to instal babel to do the transpiling just out of curiosity and running: npm run build I get the following error:
Error: EPERM: operation not permitted, scandir 'C:\Documents and
Settings'

That error seems like a Windows problem, not JS problem.
Unless you want to support servers with Node below version, I think, 4.0, I see no point in transpiling.

Node supports ES6 syntax natively in version 6.4 and above. Generally stable LTS version (currently v10.15.3) will support most current ES version (refer to https://nodejs.org/).
For specific functionality you can refer to https://node.green/ to cross reference with your node version.
As for the error, it is an npm bug that was fixed in v5.6 (https://github.com/npm/npm/issues/17747).
You can update your npm using npm install npm#latest -g.

Related

Error when Installing dependencies on a React project (node)

Hello I am currently stuck with a task that is given to me. The problem is that I am unable to install dependencies when running npm install which means that I am unable to run the website locally which is my main goal
There are many versions of this question in the internet but I am not really sure what the main cause of this therefore the title of this question
you can view my error logs here
https://www.codepile.net/pile/4qL4NOq0
I have also tried some solutions that I have seen on the internet such as:
reinstalling node
deleting node_modules and package-lock.json and re-npm install (this is when I have installed the node_modules but still unable to run the website locally)
installing grpc globally
The version of grpc that you are installing (1.23.3) does not support the version of Node.js that you are using (16). You will need to use a newer version of grpc (the latest is 1.24.11) or an older version of Node to get that to work.
Please note that the grpc package has been deprecated in favor of the #grpc/grpc-js package. It is recommended that you use that one instead if possible.

Need NPM Module that depends on babel 7, but my project uses babel 6

I am writing an NPM module that contains some React components and has #babel/core 7 as a dependency. I am testing out this module locally by linking it (via npm link) to an existing project and trying to import components from the module. At runtime though, I get the following error:
Module build failed: Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of #babel/core, it is likely that something in your build process is loading the wrong version.
My existing project and all of its dependencies are a bit older and still use babel 6. Is there any way for me to get around this without upgrading to babel 7? Thanks!

Is there any way to convert a Node project to Deno?

I want to convert a Node.js project to Deno. Is there any guide available?
My current project has lots of NPM files and it's already in TypeScript.
Any tips?
Deno and Node.js APIs are not compatible, of course you will be able to reuse all javascript/typescript code but you'll need to refactor or add polyfills.
To ease migration Deno provides a Node Compatibility library, std/node, which still needs a lot of work.
Fortunately require is one of the already supported polyfills
import { createRequire } from "https://deno.land/std/node/module.ts";
const require = createRequire(import.meta.url);
// Loads native module polyfill.
const path = require("path");
// Visits node_modules.
const leftPad = require("left-pad");
console.log(leftPad('5', 5, '0'))
If you run that example:
npm i left-pad
deno run --allow-read node.js
// 00005
As you can see left-pad was loaded correctly from node_modules/. So for NPM packages that do not rely on Node.js API, you can easily require them using std/node.
Here's a list of all supported modules
Right now, for the packages that rely heavily on Node.js API, the best thing you can do is rewrite them using Deno API.
As the project matures there will be easier ways to convert a Node.js project to Deno.
IMO for big projects working perfectly on Node.js it's not worth it to migrate them. Deno & Node.js can live together it's not one or the other. Build new projects on Deno if you prefer instead of migrating old ones.
Check out Denoify, it's a build tool that does what you want. You don't have to write the port the tool do it for you, it is all detailed in the docs.
Disclosure: I am the author.
Starting from Deno v1.15 there is Node.js compatibility mode which is unstable and comes with some caveats. There is also an issue in the repo tracking the progress of the Node Compat mode.
Quoting from the docs
Node.js compability mode
Starting with v1.15 Deno provides Node compatiblity mode that makes it
possible to run a subset of programs authored for Node.js directly in
Deno. Compatiblity mode can be activated by passing --compat flag in
CLI.
⚠️ Using compatiblity mode currently requires the --unstable flag. If
you intend to use CJS modules, the --allow-read flag is needed as
well.
⚠️ Package management is currently out of scope for Node.js
compatiblity mode. For the time being we suggest to keep using your
current solution (npm, yarn, pnpm).
Please checkout the docs for
Example
How it works?
Module Resolution
Node.js built-in modules
Typescript support
denoify demo:
#! /bin/sh
cd $(mktemp -d)
npm init -y
sed -i 's|"main": "index.js"|"main": "src/index.ts"|' package.json
mkdir src
cat >src/index.ts <<'EOF'
import { writeSync } from 'fs'
writeSync(1, "hello world\n")
EOF
echo '{"compilerOptions": { "outDir": "out" }}' >tsconfig.json
npm install denoify
npx denoify
deno run --allow-env=NODE_DEBUG deno_out/index.ts
input: src/index.ts
import { writeSync } from 'fs'
writeSync(1, "hello world\n")
output: deno_out/index.ts
import { writeSync } from 'https://deno.land/std#0.159.0/node/fs.ts'
writeSync(1, "hello world\n")

JS: nvmrc vs package.json engines?

I am trying to lock node and npm version in my javascript project to ensure other developers have those specific versions when building bundles to commit. I just added this to my package.json:
"engineStrict" : true,
"engines": {
"node" : "10.10.0",
"npm" : "6.5.0"
},
Will this enforce those versions definitively? I am unfamiliar with locking down versions since I am used to be the sole developer on frontend projects or inheriting projects that have had this set up.
Alternatively, is there a benefit of also adding an .nvmrc file that specifies the same version or is that redundant if I'm using engines?
Enforcing Node.js version
engineStrict is deprecated since npm v3, but you can set engine-strict=true in your .npmrc file. If you have engines set in package.json, an error will be thrown when someone installs on an unsupported Node.js version.
.nvmrc for developer convenience
To make it easier for other developers to use a supported Node.js version, you can add a .nvmrc file. Now other developers can run nvm use to automatically use a supported version.

Side affects installing node modules and changing Node.js version?

Are there any issues or side affects to installing node modules under a particular Node.js version, changing the version of Node.js (eg. with NVM or a general node upgrade), and using the previously installed node modules?
Yes there are! If I install a module tested against a certain version of node and thereafter I update my node version, I can’t guarantee that the module will still work as expected.
There is an optional field engines that can be set in the package.json; from here, the module publisher can specify the version of node needed for his module to work, e.g.
{ "engines" : { "node" : ">=4 <6" } }
This will send a warning message during the package installation if your node version is not supported.

Categories

Resources