执行此行时:
import stats from `./${process.env.STATS}`
报告以下错误:
解析错误:意外的令牌“
可以使用以下表达式成功加载模块:
const stats = require(`./${process.env.STATS}`);
import
语句似乎需要一个常规字符串,因为它适用于语句:
import stats from './statsdir'
其中'./statsdir'
是process.env.STATS
的值。
为什么会出现错误?
答案 0 :(得分:10)
为什么会出现错误?
您似乎自己找到了答案:
<div class="entry-content"> <h2>Overview</h2> <p style="margin-top:-30px">Despite the wealth of information on the internet, installation guides for openCV are far and few between. Whilst we have used openCV packages in previous projects they have always been wrapped in an addon format, which obviously makes it easier to use but, for a forthcoming project we needed the ability to access the library directly. Briefly, openCV is a library of functions which mainly focuses on image analysis, processing and evaluation. In lamen’s terms, it allows computers to process and understand images and other forms of visual data.</p> <p>In this post we will explain how we managed to work around the mysterious installation process, and provide a simple set of instructions that will enable you to install, build and use the openCV libraries and binaries on your system.</p> <p>First you will need:</p> <ul> <li>Mac OSX Yosemite 10.10</li> <li><a href="https://itunes.apple.com/gb/app/xcode/id497799835?mt=12" target="_blank">XCode</a> </li> <li>Command Line Tools (This is done from inside XCode)</li> <li><a href="http://www.cmake.org/download/" target="_blank">CMake</a> </li> </ul> <p>Ready … lets begin! </p> <h2>Building OpenCV</h2> <h2>Update:</h2> <p style="margin-top:-30px"> Having rebuilt openCV using the XCode again the makefile for some reason is not generated, will try and find out why. For now use the Terminal method as outlined below. <br> <strong>Attention: </strong>Steps 2 a and b document methods of installing both the static and shared libraries. </p> <p><strong>Step 1:</strong> <br>Download <a href="http://opencv.org/downloads.html" target="_blank">openCV</a> and unzip it somewhere on your computer. Create two new folders inside of the openCV directory, one called StaticLibs and the other SharedLibs.</p> <p><strong>Step 2a: Build the Static Libraries with Terminal.</strong> <br>To build the libraries in Terminal. <br> <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.17.51.png"> <img class="aligncenter size-full wp-image-1118" alt="Screen Shot 2014-10-23 at 10.17.51" src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.17.51.png" width="1920" height="1201"> </a> </p> <ul> <li>Open CMake.</li> <li style="margin-top: 5px;">Click Browse Source and navigate to your openCV folder.</li> <li style="margin-top: 5px;">Click Browse Build and navigate to your StaticLib Folder.</li> <li style="margin-top: 5px;">Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.</li> </ul> <p> <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.24.01.png"> <img class="aligncenter size-full wp-image-1118" alt="Screen Shot 2014-10-23 at 10.24.01" src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.24.01.png" width="1920" height="1201"> </a> <br>You will need to uncheck and add to the following options.</p> <ul> <li>Uncheck <code>BUILD_SHARED_LIBS</code> </li> <li>Uncheck <code>BUILD_TESTS</code> </li> <li>Add an SDK path to <code>CMAKE_OSX_SYSROOT</code>, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.</li> <li>Add x86_64 to <code>CMAKE_OSX_ARCHITECTURES</code>, this tells it to compile against the current system</li> <li>Uncheck <code>WITH_1394</code> </li> <li>Uncheck <code>WITH_FFMPEG</code> </li> </ul> <p>Click Configure again, then Click Generate.</p> <p>When the application has finished generating, Open Terminal and type the following commands.</p> <pre>- cd <path/to/your/opencv/staticlibs/folder/> - make (This will take awhile) - sudo make install</pre> <p>Enter your password. <br>This will install the static libraries on your computer.</p> <p><strong>Step 2c: Build the Shared Libraries with Terminal.</strong> </p> <ul> <li>Open CMake.</li> <li style="margin-top: 5px;">Click Browse Source and navigate to your openCV folder.</li> <li style="margin-top: 5px;">Click Browse Build and navigate to your SharedLib Folder.</li> <li style="margin-top: 5px;">Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.</li> </ul> <p>You will need to uncheck and add to the following options.</p> <ul> <li>Check <code>BUILD_SHARED_LIBS</code> </li> <li>Uncheck <code>BUILD_TESTS</code> </li> <li>Add an SDK path to <code>CMAKE_OSX_SYSROOT</code>, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.</li> <li>Add x86_64 to <code>CMAKE_OSX_ARCHITECTURES</code>, this tells it to compile against the current system</li> <li>Uncheck <code>WITH_1394</code> </li> <li>Uncheck <code>WITH_FFMPEG</code> </li> </ul> <p>Click Configure again, then Click Generate.</p> <p>When the application has finished generating, Open Terminal.</p> <pre>- cd <path/to/your/opencv/SharedLibs/folder/> - make (This will take awhile) - sudo make install</pre> <p>Enter your password. <br>This will install the shared libraries on your computer.</p> <h2>Make an Application </h2> <p style="margin-top:-30px"> This is a very basic example, but the similar principles can be applied to other code. <br>For this post, lets make an application that shows two images, one normal and one that has been put through a blur filter. <br> <strong> Step 1: </strong> </p> <ul> <li>Create a new folder somewhere on the computer.</li> <li>Inside the folder, create a CMakeLists.txt file then create a BlurImage.cpp file.</li> <li>Then add an image file.</li> </ul> <p style="text-align: center;">For this example I’ll use this fruity number. <br> <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/fruits.jpg"> <img class="aligncenter wp-image-1180" alt="fruits" src="http://blogs.wcode.org/wp-content/uploads/2014/10/fruits.jpg" width="307" height="288"> </a> </p> <p><strong>Step 2:</strong> </p> <p>Open the BlurImage.cpp in your favourite text editor and add the following text.</p> <pre>#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" using namespace std; using namespace cv; Mat src; Mat dst; char window_name1[] = "Unprocessed Image"; char window_name2[] = "Processed Image"; int main( int argc, char** argv ) { /// Load the source image src = imread( argv[1], 1 ); namedWindow( window_name1, WINDOW_AUTOSIZE ); imshow("Unprocessed Image",src); dst = src.clone(); GaussianBlur( src, dst, Size( 15, 15 ), 0, 0 ); namedWindow( window_name2, WINDOW_AUTOSIZE ); imshow("Processed Image",dst); waitKey(); return 0; }</pre> <p>Save the file.</p> <p><strong>Step 3: </strong> </p> <p>Open the CMakeLists.txt file then add the following text.</p> <pre>cmake_minimum_required(VERSION 2.8) project( BlurImage ) find_package( OpenCV ) include_directories( ${OpenCV_INCLUDE_DIRS} ) add_executable( BlurImage BlurImage.cpp ) target_link_libraries( BlurImage ${OpenCV_LIBS} ) </pre> <p>Save the File.</p> <p><strong>Step 4: </strong> <br>Open Terminal and navigate to your applications directory.</p> <pre>- cd <path/to/application/folder> - /Applications/CMake.app/Contents/bin/cmake . - make </pre> <p>This will generate both the makefile and the executable file.</p> <p>Then type <code>./BlurImage image.jpg </code> </p> <p> <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.44.56.png"> <img src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.44.56.png" alt="Screen Shot 2014-10-23 at 13.44.56" width="1920" height="1201" class="aligncenter size-full wp-image-1184"> </a> </p> <p> <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.45.52.png"> <img src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.45.52.png" alt="Screen Shot 2014-10-23 at 13.45.52" width="1920" height="1196" class="aligncenter size-full wp-image-1185"> </a> </p> <p>Yay … it works all you need to do is add your own .cpp file and alter the .txt file. Then follow the above commands.</p> <p>For more detailed examples go to the <a href="http://docs.opencv.org/doc/tutorials/tutorials.html" style="color: #920;" target="_blank">openCV Tutorial Page</a>, or check out the sample folder inside the opencv folder.</p> <p>Huge thank you to Daniel Shiffman, whose guide put us on the right track : <a style="color: #920;" href="http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/" target="_blank">http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/</a> </p> </div>
语句似乎需要一个常规字符串
完全。 import
需要字符串 literal 。它的导入位置不能动态。