Using Win10 Pro / VS2015 with a 'website' project (not asp.net, basic website)
When attempt to save/reload gulpfile.js I receive the error message (from Task Runner Explorer/output)
SyntaxError: Use of const in strict mode.
In the present case it is choking on 'gulp-changed'
I have looked through the answers and comments available:
SyntaxError: Use of const in strict mode
SyntaxError: Use of const in strict mode?
I have updated my version of node to the latest: 6.10.30
I have cleaned the cache (npm cache clean -f)
I have used 'which node' to determine that node is pointing to the installation of the latest install.
"n" does not want to install on Windows (?)
In my case the environment path, $(PATH) already include the global install of node but I needed to move it ahead of the $(DevEnvDir) paths so that it would get priority.
(added after correct answer provided, thanks #baao)
VS seems to install (and then use) an old version of node, which is why the task runner is breaking the build.
Go to Tools > Options > Projects and Solutions > External Web Tools and add the correct path to your node version (find the path with which node).
Credits to: https://github.com/sindresorhus/gulp-imagemin/issues/178#issuecomment-218131138
Related
I tried so but I have a 'require is not defined' error. I can't find information about that, can someone enlighten the noob in me please?
It's possible, but you have to be careful. Trying to require() a package means that node will try to locate its files in your file system. A chrome extension only has access to the files you declare in the manifest, not your filesystem.
To get around this, use a module bundler like Webpack, which will generate a single javascript file containing all code for all packages included through require(). You will have to generate a separate module for each component of your chrome extension (e.g. one for the background page, one for content scripts, one for the popup) and declare each generated module in your manifest.
To avoid trying to setup your build system to make using require() possible, I suggest starting with a boilerplate project. You can check out my extension to see how I do it.
An updated answer for 2022
Short answer: yes, you can require/import packages. Rather than going through the tedious work of setting up & configuring a bundler like Webpack on your own (especially if you have no experience with them), there are now build tools you can use to create the boilerplate "scaffolding" for a Chrome extension:
Extension CLI -- this one is well-documented and you can also reference the source code of some Chrome extensions that have used this tool (READ: learn how others have set up their code).
Chrome Extension CLI
Benefits of using them:
New projects are initiated with a default project file structure. Super helpful.
They support modern Javascript (ES6, ES2021), so modules work fine.
They already have bundlers integrated and pre-configured (Webpack in both above cases I think). You therefore don't need to install and configure any on your own.
You can use npm as normal to install any packages/dependencies you need.
Then of course, let the official documentation for Chrome Extensions guide you through the rest.
It's not possible to require node modules directly within a chrome extension. However, it is possible to bundle node applications and packages into the browser for use with your extensions. See here for more: Is it possible to develop Google Chrome extensions using node.js?
Yes, It is possible with esm npm packages.
require is commonjs module loader.
Browser doesn't support commonjs modules system
so that this error showed.
Method 1:
Run npm init -y and add "type" :"module" in your package.json.
create path.js file
add this line in path.js
const fullPath = await import.meta.resolve("npm-pkg-name");
const path = fullPath?.match(/(/node_modules.*)/)[0];
console.log(path);
add this line inside package.json
"path": "node --experimental-import-meta-resolve path.js",
Copy console output text. Replace package name with this copied path.
Method 2:
Install other npm package to find and replace
npm packages' virtual path to real path so that chrome browser will find it.
Install Path-fixxer
Add this line in path.js
import setAllPkgPath from "path-fixxer";
setAllPkgPath();
then run command : npm run path.
Now open browser to test it.
I'm trying to convert a project that compiles just fine on MacOS and Linux (Raspbian in particular) so that it will compile on Windows. (Full code here: https://github.com/kshetline/rpi-acu-rite-temperature)
After much grief (Like somehow the Node 12 I'd installed going away and getting replaced by Node 8 without my knowledge, and on Windows C++ long and int are both 32-bit? Really!?) I've finally made the code to compile using both node-gyp and Visual Studio 2019, but the Visual Studio set-up is fixed to a specific version of Node in a way that I don't like.
Here's my CppProperties.json file:
{
"configurations": [
{
"inheritEnvironments": [
"msvc_x86"
],
"name": "x86-Debug",
"includePath": [
"${env.INCLUDE}",
"${env.LOCALAPPDATA}\\node-gyp\\Cache\\12.16.1\\include\\node\\**",
"${workspaceRoot}\\**"
],
"defines": [
"WIN32",
"_DEBUG",
"NAPI_CPP_EXCEPTIONS",
"UNICODE",
"_UNICODE",
"USE_FAKE_PIGPIO"
],
"intelliSenseMode": "windows-msvc-x86"
}
]
}
I don't want to have to put a specific version of Node in my include path, but I don't know how to make this work without it. I can't even make it work for a specific major version of Node by using 12.* or 12.** -- wildcards don't seem to work for me there.
I see no Windows environment variable that would let me use something like, say, ${env.NODE_VERSION}, and AFAIK there's no VS pre-defined variable that would handle this either.
I could use my own environment variable, but that would have to be manually updated, which is what I'm trying to avoid.
Does Visual Studio have a way that I could script an update, like somehow doing the command node --version, stripping off the leading letter 'v' from the output, and then putting the result into an environment variable?
Any other possible solutions?
Update:
I changed the set-up of my Visual Studio project by starting with a generic command-line app, and the configuration works totally differently, with no CppProperties.json file at all, but the issue is still basically the same -- I now have $(LOCALAPPDATA)\node-gyp\Cache\12.16.1\include\node entered into a config dialog instead.
An addon using node-addon-api must, at compile time, target a precise version of node.
That's because N-API is itself "versioned", (with a single digit macro, NAPI_VERSION, currently from 1 to 5, plus the special value NAPI_VERSION_EXPERIMENTAL=2147483647)
As you can see, for example, in the N-API Version Matrix the version 4 is available in node v10.16.0 but NOT in some older v10 releases.
The addon is "retro-linked" (I don't know the name of that technique) to node.exe, and that link is a "delay load" one. That trick allows the addon to use the N-APIfunctions that are in the host executable, even if it's not "node.exe" (hence the use of win_delay_load_hook.cc).
As for CppProperties.json file, it is my understanding that it's only used by Intellisence and is not related to headers used at compile time.
You can force node-gyp to use a specific version of node, unrelated to the current version installed on the machine, with the --target option to the configure command.
Another useful option is --devdir, which allows to get rid of the infamous (afaic) %localappdata%\node-gyp\Cache root directory.
All the node-gyp command options are here
Please, dear node/node_gyp/N-API experts reading this, don't hesitate to signal/fix errors in that answer.
i want to install newrelic on my project but i'm receiving from de console:
node_modules\#newrelic\native-metrics\build\bind ing.sln.metaproj : error MSB4057: The target "/t:native_metrics" does not exist in the project.
in another machine works perfectly. i really would appreciate if some one could help with that, i already tried this from newrelic https://discuss.newrelic.com/t/newrelic-native-metrics-does-not-install-on-windows-10-from-corrupt-vcxproj/54625/3
This is most likely due to the version of node-gyp. A change was introduced into node-gyp
version 3.7.0 that prepends /t: to the build target on Windows machines. The ./lib/pre-build.js file that New Relic uses to bootstrap the build also prepends /t: to the target which results in a value of /t:/t:native_metrics being passed to MSBuild, which isn't valid. You can fix this by installing an older version of node-gyp. Depending on how npm is set up on your machine you may also have to set the npm_config_node_gyp environment variable to the path of the older version of node-gyp so it gets picked up by the New Relic bootstrapper.
The Wekan code that be built by Meteor cannot run in node 0.10.4x ,
Meteor will create the code for new version node, like const or lambda expression.
How to ask Meteor to build the code that can be run in node 0.10.4x ?
Error 1:
/home/wekan/output/bundle/programs/server/packages/modules.js:33433
const Boom = require(‘boom’);
^^^^^
SyntaxError: Use of const in strict mode.
at /home/wekan/output/bundle/programs/server/boot.js:292:30
at Array.forEach (native)
at Function..each..forEach (/home/wekan/output/bundle/programs/server/node_modules/underscore/underscore.js:79:11)
at /home/wekan/output/bundle/programs/server/boot.js:133:
Error 2:
/home/wekan/output/bundle/programs/server/packages/modules.js:33571
credentialsFunc(attributes.id, (err, credentials) => {
^Preformatted text
SyntaxError: Unexpected token >
at /home/wekan/output/bundle/programs/server/boot.js:292:30
at Array.forEach (native)
at Function..each..forEach (/home/wekan/output/bundle/programs/server/node_modules/underscore/underscore.js:79:11)
at /home/wekan/output/bundle/programs/server/boot.js:133:5
According to the Wekan documentation at https://github.com/wekan/wekan/wiki/Install-and-Update
If you haven't already, you need to install Node.js, given that we
need node version 4.8.4, make sure to either use the custom packages
(the ones of your OS are likely too old) or install the correct
version from the Node.js website.
Node 0.10.4 is pretty old already, I don't know why you are still using it. You can use something like nvm if you want to manage different versions of node on your machine.
It seems, you're using really old version of wekan that needs such old version of node.
Unfortunately, babel transpiler (Meteor is using it to transpile JS code) has dropped support for Node 0.10 and 0.12 (discussed here).
I'd suggest you to update your wekan version or use nvm to install and use different version of node simultaneously.
I'm working with node.js, and in one of my js files I'm using const in "strict mode". When trying to run it, I'm getting an error:
SyntaxError: Use of const in strict mode.
What is the best practice to do this?
Edit:
'use strict'
const MAX_IMAGE_SIZE = 1024*1024; // 1 MB
The const and let are part of ECMAScript 2015 (a.k.a. ES6 and Harmony), and was not enabled by default in Node.js 0.10 or 0.12. Since Node.js 4.x, “All shipping [ES2015] features, which V8 considers stable, are turned on by default on Node.js and do NOT require any kind of runtime flag.”. Node.js docs has an overview of what ES2015 features are enabled by default, and which who require a runtime flag. So by upgrading to Node.js 4.x or newer the error should disappear.
To enable some of the ECMAScript 2015 features (including const and let) in Node.js 0.10 and 0.12; start your node program with a harmony flag, otherwise you will get a syntax error. For example:
node --harmony app.js
It all depends on which side your strict js is located. I would recommend using strict mode with const declarations on your server side and start the server with the harmony flag. For the client side, you should use Babel or similar tool to convert ES2015 to ES5, since not all client browsers support the const declarations.
If this is happening in nodejs, it is due to the older version of nodejs. Update node by using,
1) Clear NPM's cache:
sudo npm cache clean -f
2) Install a little helper called 'n'
sudo npm install -g n
3) Install latest stable NodeJS version
sudo n stable
Update nodejs instructions taken from, https://stackoverflow.com/a/19584407/698072
Usually this error occurs when the version of node against which the code is being executed is older than expected. (i.e. 0.12 or older).
if you are using nvm than please ensure that you have the right version of node being used. You can check the compatibility on node.green for const under strict mode
I found a similar issue on another post and posted my answer there in detail
One important step after you update your node is to link your node binary to the latest installed node version
sudo ln -sf /usr/local/n/versions/node/6.0.0/bin/node /usr/bin/node
This is probably not the solution for everyone, but it was for me.
If you are using NVM, you might not have enabled the right version of node for the code you are running. After you reboot, your default version of node changes back to the system default.
Was running into this when working with react-native which had been working fine. Just use nvm to use the right version of node to solve this problem.
Since the time the question was asked, the draft for the const keyword is already a living standard as part of ECMAScript 2015. Also the current version of Node.js supports const declarations without the --harmony flag.
With the above said you can now run node app.js, with app.js:
'use strict';
const MB = 1024 * 1024;
...
getting both the syntax sugar and the benefits of strict mode.
I had a similar issue recently and ended up in this Q&A. This is not the solution the OP was looking for but may help others with a similar issue.
I'm using PM2 to run a project and in a given staging server I had a really old version of Node, NPM and PM2. I updated everything, however, I kept keeping the same error:
SyntaxError: Use of const in strict mode.
I tried to stop and start the application several times. Also tried to update everything again. Nothing worked. Until I noticed a warning when I ran pm2 start:
>>>> In-memory PM2 is out-of-date, do:
>>>> $ pm2 update
In memory PM2 version: 0.15.10
Local PM2 version: 3.2.9
Gotcha! After running pm2 update, I finally was able to get the application running as expected. No "const in strict mode" errors anymore.
I was using pm2 to start and maintain the node processes.
From the CLI it worked perfectly.
which node
/usr/local/bin/node
node -v
v10.15.0
However, I set up a cronjob and I got the syntax error.
Then wrote a cronjob to check which node and node -v and surprisingly, it was a different path and version.
which node
/usr/bin/node
node -v
v0.10.48
To fix the cronjob I had to use the flag --interpreter for pm2, like so:
pm2 start dist/server.js --interpreter=/usr/local/bin/node
The use of const in strict mode is available with the release of Chrome 41.
Currently, Chrome 41 Beta is already released and supports it.
cd /
npm install -g nave
nave use 6.11.1
node app.js
const is not supported by ECMAScript. So after you specify strict mode, you get syntax error. You need to use var instead of const if you want your code to be compatible with all browsers. I know, not the ideal solution, but it is what it is. There are ways to create read-only properties in JavaScript (see Can Read-Only Properties be Implemented in Pure JavaScript?) but I think it might be overkill depending on your scenario.
Below is browser compatibility note from MDN:
Browser compatibility
The current implementation of const is a Mozilla-specific extension
and is not part of ECMAScript 5. It is supported in Firefox & Chrome
(V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable
with const in these browsers, you can still change its value later. It
is not supported in Internet Explorer 6-10, but is included in
Internet Explorer 11. The const keyword currently declares the
constant in the function scope (like variables declared with var).
Firefox, at least since version 13, throws a TypeError if you
redeclare a constant. None of the major browsers produce any notices
or errors if you assign another value to a constant. The return value
of such an operation is that of the new value assigned, but the
reassignment is unsuccessful only in Firefox and Chrome (at least
since version 20).
const is going to be defined by ECMAScript 6, but with different
semantics. Similar to variables declared with the let statement,
constants declared with const will be block-scoped.